cuddly-photographer-93072
07/04/2025, 2:46 PMtall-airline-16756
07/04/2025, 2:46 PMtall-airline-16756
07/04/2025, 2:46 PMcuddly-photographer-93072
07/04/2025, 2:48 PMtall-airline-16756
07/04/2025, 2:48 PMagent.py
, I've got a super handy way to help you out using the ChatContext
object from LiveKit! Let me walk you through some friendly examples that'll make managing your chat context a breeze. 1. Printing the entire context as a dictionary: python from livekit.agents.llm import ChatContext chat_ctx = ChatContext() # After adding messages to chat_ctx print(chat_ctx.to_dict()) # Prints the entire context as a dictionary print(chat_ctx.items) # Prints the items in the context
2. Logging user messages when they are committed to the chat context: python @agent.on("user_speech_committed") def on_user_speech_committed(msg: llm.ChatMessage): # Convert string lists to strings, drop images if isinstance(msg.content, list): msg.content = "\n".join( "[image]" if isinstance(x, llm.ChatImage) else x for x in msg ) log_queue.put_nowait(f"[{datetime.now()}] USER:\n{msg.content}\n\n")
Want to see a full example in action? Check out the detailed code in the LiveKit GitHub Example. It's a great resource to help you get started! Sources: VoicePipelineAgent | LiveKit Docs | GitHub::livekit::agents | tests/test_chat_ctx.py