when is conversation_item_added called?
# ask-ai
c
when is conversation_item_added called?
t
Thanks for reaching out, roni. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The
conversation_item_added
event is triggered when an item is committed to the chat history. This event applies to both user and agent items. It includes a single property: • `item`: ChatMessage - represents the message that was added to the conversation. You can handle this event by registering a callback function. Here's an example:
Copy code
from livekit.agents import ConversationItemAddedEvent
from livekit.agents.llm import ImageContent, AudioContent

@session.on("conversation_item_added")
def on_conversation_item_added(event: ConversationItemAddedEvent):
    print(f"Conversation item added from {event.item.role}: {event.item.text_content}. interrupted: {event.item.interrupted}")
    # to iterate over all types of content:
    for content in event.item.content:
        if isinstance(content, str):
            print(f" - text: {content}")
        elif isinstance(content, ImageContent):
            # image is either a rtc.VideoFrame or URL to the image
            print(f" - image: {content.image}")
        elif isinstance(content, AudioContent):
            # frame is a list[rtc.AudioFrame]
            print(f" - audio: {content.frame}, transcript: {content.transcript}")
This allows you to process various types of content in the conversation item, such as text, images, and audio. Sources: Events and error handling | LiveKit Docs