enough-match-43171
12/01/2025, 1:18 PMtall-airline-16756
12/01/2025, 1:19 PMtall-airline-16756
12/01/2025, 1:19 PMsession.history (a ChatContext).
• Convert to a dict for inspection/serialization:
history_dict = session.history.to_dict()
2) Save a transcript when the session ends
• Register a shutdown callback and write session.history.to_dict() to a file:
from datetime import datetime
import json
def entrypoint(ctx: JobContext):
async def write_transcript():
current_date = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"/tmp/transcript_{ctx.room.name}_{current_date}.json"
with open(filename, "w") as f:
json.dump(session.history.to_dict(), f, indent=2)
print(f"Transcript for {ctx.room.name} saved to {filename}")
ctx.add_shutdown_callback(write_transcript)
3) Get messages in real time as they're added
• Listen for the conversation_item_added event:
@session.on("conversation_item_added")
def on_conversation_item_added(event: ConversationItemAddedEvent):
print(f"Message from {event.item.role}: {event.item.text_content}")
Notes
• session.history is available only inside an active AgentSession.
• If you’re not using AgentSession, you must store messages yourself (e.g., on your server or via a text stream).
Would you like a short example showing how to initialize an AgentSession around this code, or do you need help exporting history to a database or a particular file format?
Sources: Observability data hooks | LiveKit docs | Events and error handling | LiveKit docs | Text and transcriptions | LiveKit docs