straight-jelly-92105
11/01/2025, 5:31 PMtall-airline-16756
11/01/2025, 5:31 PMtall-airline-16756
11/01/2025, 5:32 PMsession.history and separate user transcripts from LLM-generated content. Here are some friendly ways to approach this:
• Accessing Full Conversation History: You can use session.history to see all the messages exchanged during your session. Each message has a role field, which helps you identify where it came from.
• Filtering for User Transcripts: To get only the user messages, just filter where role == "user". Here’s a simple example:
# Extract user messages
user_transcripts = [item for item in session.history if item.role == "user"]
for message in user_transcripts:
print(f"User said: {message.content}")
• Real-Time Access: If you want to grab user input as it’s being transcribed, you can listen for the user_input_transcribed event. Here’s how:
def on_user_input_transcribed(event):
if event.is_final:
print(f"Final user transcript: {event.transcript}")
session.on("user_input_transcribed", on_user_input_transcribed)
• Post-Session Persistence: To save user transcripts after the session ends, you can use add_shutdown_callback. Here’s a handy example:
def save_user_transcripts():
user_messages = [item for item in session.history if item.role == "user"]
with open("user_transcripts.json", "w") as f:
import json
json.dump([{"content": m.content, "timestamp": m.timestamp} for m in user_messages], f)
session.add_shutdown_callback(save_user_transcripts)
I hope these methods help you separate user transcripts from the LLM-generated content smoothly. If you have any questions or want to explore this further, just let me know—I’m here to help!
Sources: Session recording and transcripts | LiveKit Docs | Workflows | LiveKit Docs | examples/voice_agents/realtime_load_chat_history.pystraight-jelly-92105
11/01/2025, 5:34 PMtall-airline-16756
11/01/2025, 5:35 PMconversation_item_added event to filter messages by role (user or assistant) and print them directly. Here's an example for you:
def handle_conversation_item(event):
item = event.item
if item.role == "user":
print(f"User: {item.text_content}")
elif item.role == "assistant":
print(f"Agent: {item.text_content}")
• Print LLM Context: To access and print the LLM context before processing, you can override the llm_node method. Here's a simple example:
async def llm_node(self, chat_ctx, tools, model_settings):
for item in chat_ctx.items:
print(f"[{item.role.upper()}]: {item.text_content}")
return await Agent.default.llm_node(self, chat_ctx, tools, model_settings)
• Real-Time User Input: If you'd like to print user input as it's being transcribed, use the user_input_transcribed event like this:
def handle_user_transcript(event):
if event.is_final:
print(f"User (Final): {event.transcript}")
These approaches should help you separate and print user, agent, and LLM context either in real-time or after the session. If anything feels unclear or you want to dive deeper into any part, please don't hesitate to ask—I'm here to help!
Sources: Session recording and transcripts | LiveKit Docs | Events and error handling | LiveKit docs | Understanding Transcript Timing in Voice Conversations | complex-agents/note-taking-assistant/agent.pystraight-jelly-92105
11/01/2025, 5:37 PMconversation_item_added event to filter messages by role (user or assistant) and print them directly. Here's an example for you:
def handle_conversation_item(event):
item = event.item
if item.role == "user":
print(f"User: {item.text_content}")
elif item.role == "assistant":
print(f"Agent: {item.text_content}")tall-airline-16756
11/01/2025, 5:37 PMconversation_item_added event:
from livekit.agents import Agent, AgentSession, JobContext, AutoSubscribe
from livekit.agents.voice import ConversationItemAddedEvent
class TranscriptAgent(Agent):
def __init__(self):
super().__init__(
instructions="You are a helpful assistant."
)
async def entrypoint(ctx: JobContext):
await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
agent = TranscriptAgent()
session = AgentSession()
# Event handler for separating user and agent transcripts
@session.on("conversation_item_added")
def handle_conversation_item(event: ConversationItemAddedEvent):
item = event.item
if item.role == "user":
print(f"User: {item.text_content}")
elif item.role == "assistant":
print(f"Agent: {item.text_content}")
await session.start(agent=agent, room=ctx.room)
Just place this code in your entry function, and it will print user and agent transcripts separately in real-time. If you have any questions or need more help, please feel free to ask—I’m here to support you!