How can i access transcript in real time on my AI ...
# ask-ai
w
How can i access transcript in real time on my AI agent in python??
Copy code
session = AgentSession(
        llm=google.beta.realtime.RealtimeModel(
            model=model,
            voice=voice,
            temperature=temperature,
            instructions=instructions,
        ),
    )
t
Thanks for reaching out, Rahul. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to get real-time transcripts in your AI agent using Python, you can use the
AgentSession
and listen for transcription events. Here’s a quick example to get you started:
Copy code
from livekit.agents import AgentSession
from livekit.plugins import deepgram  # or other STT providers

async def entrypoint(ctx):
    session = AgentSession(
        stt=deepgram.STT(),  # Initialize STT provider
    )

    @session.on("user_input_transcribed")
    def on_transcript(transcript):
        if transcript.is_final:
            print(f"Final transcript: {transcript.transcript}")
        else:
            print(f"Interim transcript: {transcript.transcript}")

    await session.start(agent=MyAgent(), room=ctx.room)
This setup listens to the
user_input_transcribed
event so you can catch both interim and final transcriptions as they happen. If you want to dive deeper or have a specific scenario in mind, just give me a shout—I’m here to help! Sources: Transcriptions | LiveKit Docs | Text and transcriptions | LiveKit Docs | examples/voice_agents/timed_agent_transcript.py | Session recording and transcripts | LiveKit Docs | Transcriptions | LiveKit Docs
w
Thanks, it worked, how can i access system transcript as well?
t
Hey! If you want to check out both system and user transcripts, you can use the
session.history
property. It holds the entire conversation history, including messages from both sides. Here's a quick example:
Copy code
# Access current history
history = session.history

# Iterate through the history
for item in history:
    print(f"{item.role}: {item.text}")
Also, if you'd like to save the history once the session wraps up, you can do that with a shutdown callback like this:
Copy code
# Save after session ends
ctx.add_shutdown_callback(lambda: save_history(session.history))
If you have any questions or a particular scenario you want help with, just give me a shout! Sources: complex-agents/teleprompter/cartesia-ink.py | Session recording and transcripts | LiveKit Docs | main.py | Text and transcriptions | LiveKit Docs | Text and transcriptions | LiveKit Docs
w
TypeError: 'ChatContext' object is not iterable
t
Hey there! That error
TypeError: 'ChatContext' object is not iterable
pops up because the
ChatContext
object itself can’t be looped over directly. Instead, you want to use its
items
property, which holds the list of chat messages. Here’s a simple way to go through the conversation history:
Copy code
from livekit.agents.llm import ChatContext, ChatMessage

# Assuming chat_ctx is your ChatContext instance
for item in chat_ctx.items:  # Use .items to access the list
    if isinstance(item, ChatMessage):
        print(f"{item.role}: {item.text_content}")
If anything’s still unclear or you want a hand with this, just give me a shout—I’m here to help! Sources: tests/test_chat_ctx.py
w
What does
conversation_item_added
event do ??
t
Hey there! Just so you know, the
conversation_item_added
event pops up whenever something new gets added to the chat history during an agent session. This covers all the finalized messages from both the user and the agent—whether it’s text, audio, images, or even function call results. When this event happens, you’ll get a
ConversationItemAddedEvent
object that includes a super important property: • item: This is a
ChatMessage
instance that holds all the juicy details like who sent it (say, "user" or "assistant"), the text, any media attached, and if the message got interrupted. It’s pretty handy if you want to keep an eye on things in real-time, log conversations, or send messages straight to the frontend as they get officially saved in the session history. Sources: Events and error handling | LiveKit Docs | Events and error handling | LiveKit Docs | Session recording and transcripts | LiveKit Docs | Session recording and transcripts | LiveKit Docs | Text and transcriptions | LiveKit Docs
w
let's say on each call of this event i execute the function that takes 3-4 seconds, during processing of message1, message2 also generated, what will happen then??? 1. Can i loose any message here?? 2. Can order of message receiving change, depending upon if message 1 processing taking time, message 2 processing might start
t
Thanks for your questions! Here’s a quick rundown: 1. Message Loss: Don’t worry—your messages won’t get lost. The
conversation_item_added
event only fires once a message is safely saved in the chat history, so it’s all secure and accounted for. 2. Message Order: The messages will stay in the right order. Even if one event takes a bit longer to process, the system makes sure that every message is added to the history exactly where it belongs. So, while there might be some delays in handling the events, the messages themselves remain safe and in the correct sequence. Hope that helps clear things up! Sources: Text and transcriptions | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_session.py | plugins/openai/src/realtime/realtime_model.ts | Events and error handling | LiveKit Docs | Events and error handling | LiveKit Docs