```@session.on("conversation_item_added") def on_c...
# ask-ai
b
Copy code
@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}")
where to add this code in my agent.py file
t
Thanks for reaching out, Aman. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Add the
@session.on("conversation_item_added")
event handler right after you create the
AgentSession
but before you call
session.start()
in your
agent.py
file. Here’s a friendly example to help you set it up:
Copy code
from livekit.agents import ConversationItemAddedEvent

async def entrypoint(ctx: JobContext):
    # 1. Create the session
    session = AgentSession(
        llm=openai.LLM(model="gpt-4o-mini"),
        # ... other session configuration
    )

    # 2. Add event handlers here (after session creation, before session.start)
    @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 check out all types of content:
        for content in event.item.content:
            if isinstance(content, str):
                print(f" - text: {content}")
            elif isinstance(content, ImageContent):
                print(f" - image: {content.image}")
            elif isinstance(content, AudioContent):
                print(f" - audio: {content.frame}, transcript: {content.transcript}")

    # 3. Start the session
    await session.start(
        agent=MyAgent(),
        room=ctx.room,
        # ... room options
    )
Don’t forget to import the event types you need:
Copy code
from livekit.agents import ConversationItemAddedEvent
from livekit.agents.llm import ImageContent, AudioContent
Sources: Events and error handling | LiveKit Docs