Give me a command to get the chat context messages...
# ask-ai
a
Give me a command to get the chat context messages using session.history but excluding system instructions
t
Thanks for reaching out, Hiren. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! If you'd like to get chat context messages using
session.history
but want to exclude system instructions, you can easily do this by using the
copy()
method from the
ChatContext
class with the
exclude_instructions=True
parameter. Let me show you an example:
Copy code
# Get chat history excluding system instructions
filtered_history = session.history.copy(exclude_instructions=True)

# Iterate through the filtered messages
for item in filtered_history.items:
    if item.type == "message":
        print(f"{item.role}: {item.content}")
This way, you'll filter out messages coming from roles like "system" or "developer," while keeping all the user and assistant messages intact. I hope this helps! If you have any more questions, feel free to ask. Sources: examples/voice_agents/session_close_callback.py | agents/src/llm/chat_context.test.ts | Workflows | LiveKit Docs | Agent-assisted warm transfer | LiveKit Docs
a
why content is coming as an array.. instead of the string?
t
I'm glad you asked about the
content
field in
session.history
messages! It’s designed as an array to comfortably support multimodal content, which means a single message can include different parts like text and images all together. Here’s how it works: • Text parts come through as simple strings. • Image parts show up as objects containing important details like type and URL. This setup really helps create rich, flexible interactions, especially when you’re working with both text and visuals. If you’d like to handle the content as one single string, you can easily combine the array elements like this:
Copy code
# Convert array content to a single string
if isinstance(msg.content, list):
    msg.content = "\n".join(
        "[image]" if isinstance(x, dict) and x.get("type") == "image_url" else x
        for x in msg.content
    )
This way, you keep everything compatible while still enjoying the benefits of multimodal content. If you have any more questions or want to dive deeper, I’m here to help! Sources: tools/server/themes/wild/index.html | VoicePipelineAgent | LiveKit Docs | Working with the MultimodalAgent class | LiveKit Docs
a
what If I only want text message and want to ignore all the others
t
I'd be happy to help you filter only the text messages from
session.history
while skipping other content types. You can do this by checking if the content is a string. Here's a simple example for you:
Copy code
# Filter and process only text messages
for item in session.history.items:
    if isinstance(item.content, list):
        for content in item.content:
            if isinstance(content, str):
                print(f"Text: {content}")
This way, you'll make sure to process only the text parts of the messages and gently skip over other types like images or metadata. If you have any questions or need further assistance, feel free to ask! Sources: Events and error handling | LiveKit Docs | Agent-assisted warm transfer | LiveKit Docs
a
I want the whole object list but only content's value to be replaced from List to the text content like you've just provided
t
Let's walk through how you can replace the
content
field's value from a list to text content while keeping the entire object list intact. You can do this by iterating through
session.history.items
and updating the
content
field as needed. Here's a simple example to guide you:
Copy code
# Replace list content with text while keeping the object list intact
for item in session.history.items:
    if isinstance(item.content, list):
        # Convert list to a single string
        item.content = "\n".join(
            "[image]" if isinstance(x, dict) and x.get("type") == "image_url" else str(x)
            for x in item.content
        )
This approach will help transform the
content
field into a more readable text format, while still preserving the rest of the object structure. Also, if you'd like these changes to be saved across agent turns, don't forget to call
update_chat_ctx()
to sync the modifications back to the active chat context. Sources: Workflows | LiveKit Docs | VoicePipelineAgent | LiveKit Docs | VoicePipelineAgent | LiveKit Docs | Working with the MultimodalAgent class | LiveKit Docs | Working with the MultimodalAgent class | LiveKit Docs