How do I get the timestamp on the transcript when ...
# ask-ai
l
How do I get the timestamp on the transcript when using AgentSession.history() ?
t
Thanks for reaching out, David. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you with this! The
AgentSession.history()
method doesn’t directly provide timestamps for transcript entries, but there are a few ways you can get those timestamps: • Enable TTS-aligned transcription: By setting
use_tts_aligned_transcript=True
in your
AgentSession
configuration, you’ll receive
TimedString
objects that include
start_time
and
end_time
for each word or sentence. This works well with certain TTS providers like Cartesia and ElevenLabs. • Use event-based timestamps: You can listen for events like
conversation_item_added
or
user_input_transcribed
. These events come with a
created_at
timestamp showing exactly when the item was added to the history. • Manual timestamping: Another option is to add timestamps yourself using
datetime.now()
when you handle transcription events such as
user_input_transcribed
. If you’d like, I can provide more details or examples on how to set up any of these methods—just let me know! I’m here to help you every step of the way. Sources: Session recording and transcripts | LiveKit Docs | Text and transcriptions | LiveKit Docs | Text and transcriptions | LiveKit Docs | tests/test_agent_session.py | pipeline-stt/transcriber.py
l
so the sessionAgent does not support timestamps at the moment ?
t
You're absolutely right, the
AgentSession.history()
method doesn’t directly give timestamps for transcript entries. But don’t worry—you can still get those timestamps by using other methods, like enabling TTS-aligned transcription, working with event-based timestamps, or even adding timestamps manually during transcription events. If you'd like, I’d be happy to walk you through how to set any of these up. Just let me know!
l
so if I enable tTTS-aligned transcription, how do I get the timestamps after ?
t
I’m glad you’re exploring the TTS-aligned transcription feature! When you enable it by setting
use_tts_aligned_transcript=True
in your
AgentSession
configuration, you’ll be able to access timestamps through the
transcription_node
. This node works with
TimedString
objects, which conveniently include
start_time
and
end_time
attributes for each word or sentence. Here’s a friendly example to help you get started:
Copy code
async def transcription_node(
    self, text: AsyncIterable[str | TimedString], model_settings: ModelSettings
) -> AsyncGenerator[str | TimedString, None]:
    async for chunk in text:
        if isinstance(chunk, TimedString):
            print(f"Text: {chunk.text}, Start: {chunk.start_time}, End: {chunk.end_time}")
        yield chunk
This setup will let you easily extract and use timestamps for each part of your transcript. If you have any questions or want to dive deeper into this, just let me know—I’m here to help!
l
will this work both for SST and TTS ? or just TTS ?
t
The TTS-aligned transcription using
TimedString
objects is designed especially for TTS (Text-to-Speech) situations. When it comes to SST (Speech-to-Text), you’d want to use event-based timestamps, like those from the
user_input_transcribed
event, or you can add timestamps manually during transcription events. If you’d like, I’m here to help clarify anything or assist you further with SST-related timestamping. Just let me know!
l
what about the exclude_timestamp options on the to_dict ?
t
I'm happy to help you with the
exclude_timestamp
option! This option is available in the
to_dict
method of the
ChatContext
class, which you can access through the
AgentSession.history
property. It lets you exclude the
created_at
timestamp field when you’re serializing chat items, which can be really handy. Here’s a simple example in Python to show you how it works:
Copy code
# Access the chat context from the session and exclude timestamps
chat_dict = agent_session.history.to_dict(exclude_timestamp=True)
And if you’re working with JavaScript or TypeScript, you can use the
toJSON
method with the
excludeTimestamp
option like this:
Copy code
// Access the chat context and exclude timestamps
const chatDict = agentSession.history.toJSON({ excludeTimestamp: true });
If anything is unclear or if you’d like me to walk you through it step-by-step, please don’t hesitate to ask. I’m here to help! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | agents/src/voice/agent_activity.ts