what is correct way to access the list of `ChatMe...
# ask-ai
c
what is correct way to access the list of
ChatMessage
objects from the
AgentSession.history
object? The log proves that simply accessing the
.messages
attribute is not correct. It might be a method call like
history.get_messages()
or the
ChatContext
object itself might be iterable (e.g.,
for msg in history:
).
t
Thanks for reaching out, Mohamed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The correct way to access a list of
ChatMessage
objects from an
AgentSession.history
(which is a
ChatContext
instance) is to use the
.items
property, not
.messages
. The
.items
attribute contains all chat items, including messages and other types (like function calls), so you should filter for the specific type you want:
Copy code
chat_items = session.history.items  # List of all ChatItem objects
chat_messages = [item for item in chat_items if item.type == "message"]
• There is no
.messages
attribute or
get_messages()
method. • If you use direct iteration, you need to filter for message types explicitly. For more details on working with
ChatContext
and filtering, see the above pattern and the available properties and methods such as
get_by_id
and
copy
. Sources: livekit-agents/livekit/agents/llm/chat_context.py