busy-breakfast-32623
08/19/2025, 8:07 AM@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 filetall-airline-16756
08/19/2025, 8:07 AMtall-airline-16756
08/19/2025, 8:07 AM@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:
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:
from livekit.agents import ConversationItemAddedEvent
from livekit.agents.llm import ImageContent, AudioContent
Sources: Events and error handling | LiveKit Docs