How do I get the messages within the Agent class
# ask-ai
a
How do I get the messages within the Agent class
t
Thanks for reaching out, Shajahan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to access messages inside the Agent class — here's how to do that. • Use the agent's
chat_ctx
property; it contains the conversation items. • Iterate `self.chat_ctx.items`; each item may be a
ChatMessage
,
FunctionCall
, or
FunctionCallOutput
. Example:
Copy code
async def my_method(self):
    for item in self.chat_ctx.items:
        if hasattr(item, 'content'):
            print(f"Message: {item.content}")
• For the full session history (including across handoffs), use
self.session.history
. Would you like an example tailored to a specific language or a sample that filters by message role (user/system/assistant)? Sources: Agents v0.x migration guide - Node.js | LiveKit Docs
a
I want it as a list of dict
t
You're asking for the Agent's messages as a list of dicts. Use
self.session.history.to_dict()
which returns the full conversation as serializable dictionaries. Example:
Copy code
async def my_method(self):
    # returns a list[dict] with keys like 'role' and 'text_content'
    messages_as_dicts = self.session.history.to_dict()

    # optional: filter or transform
    simplified = [
        {"role": m.get("role"), "content": m.get("text_content")}
        for m in messages_as_dicts
    ]

    return simplified
Would you like the dicts to include timestamps, message IDs, or function-call details? Sources: Observability data hooks | LiveKit docs | Agents and handoffs | LiveKit docs