I'm using `on_user_turn_completed` method to say a...
# ask-ai
c
I'm using
on_user_turn_completed
method to say a few filler words to the user by making use of the
session.say
method, with
add_to_chat_ctx
as False. However, I want the said transcript to be available in the end. How to achieve this?
t
Thanks for reaching out, Bharath. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you make the transcript of
session.say
with
add_to_chat_ctx=False
available at the end, you can gently capture and save these messages yourself. Here’s a simple way to do it: 1. Manual Transcript Buffer: Before you call
session.say
, try storing the text in a separate buffer. This way, you’ll have it ready to include in your final transcript. 2. Turn Context Integration: You can also use
turn_ctx.add_message()
to manually add those excluded messages into the turn context, helping keep everything together. 3. Shutdown Callback: Another helpful tip is to register a shutdown callback with
ctx.add_shutdown_callback()
. This lets you save the complete transcript, including the messages that were excluded, when your session wraps up. If you’d like, I’m happy to provide detailed code examples or walk you through any of these steps—just let me know! Sources: Agent speech and audio | LiveKit docs | Session recording and transcripts | LiveKit docs | tests/test_agent_session.py | Session recording and transcripts | LiveKit Docs
c
But if I add message with
turn_ctx.add_message()
, it the messages will be sent to further llm calls right? I don't want that to happen.
t
Thanks for reaching out! To add messages to the transcript without including them in the LLM context, the best approach is to keep using
session.say()
with
add_to_chat_ctx=False
. This way, your message will be spoken and recorded in the transcript, but it won’t be part of the LLM’s input context for future responses. On the other hand, using
turn_ctx.add_message()
would add the message to the LLM context, so it might not fit what you’re looking for. If you’d like, I’m here to help with more details or examples—just let me know! Sources: Agent speech and audio | LiveKit docs | agents/src/llm/chat_context.test.ts | livekit-agents/livekit/agents/voice/generation.py | Pipeline nodes and hooks | LiveKit docs | agent_extensions/agent_extensions/rag/handler.py
c
Can you provide code example for: 1. Manual Transcript Buffer: Before you call
session.say
, try storing the text in a separate buffer. This way, you’ll have it ready to include in your final transcript. approach that you mentioned before
t
I'd love to share a helpful code example for the Manual Transcript Buffer approach with you:
Copy code
class TranscriptManager:
    def __init__(self):
        self.excluded_transcripts = []  # Buffer to store excluded messages

    def capture_excluded_say(self, text):
        """Call this before session.say() with add_to_chat_ctx=False"""
        self.excluded_transcripts.append({
            "timestamp": time.time(),
            "role": "assistant",
            "content": text,
            "excluded_from_context": True
        })

    def get_full_transcript(self, session):
        """Combine chat history with excluded transcripts"""
        chat_history = session.history.to_dict() if session else {}
        return {
            "chat_history": chat_history,
            "excluded_transcripts": self.excluded_transcripts
        }

# Usage
transcript_manager = TranscriptManager()

# Before calling session.say()
text_to_say = "Let me think about that..."
transcript_manager.capture_excluded_say(text_to_say)
session.say(text_to_say, add_to_chat_ctx=False)

# Later, retrieve the full transcript
full_transcript = transcript_manager.get_full_transcript(session)
print(full_transcript)
This method helps make sure that even the messages excluded from the LLM context are safely captured and included in your final transcript. If you have any questions or need a hand with anything else, please feel free to ask—I’m here to help!