how do you update the conversation history on gemi...
# ask-ai
m
how do you update the conversation history on gemini real time agent to provide it with context from previous conversations?
t
Thanks for reaching out, Pablo. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you update the conversation history on a Gemini real-time agent and provide context from previous chats. Here are a few ways you can do this: • Initialize Agent with Preloaded Chat Context: When you create a new agent, you can pass a
llm.ChatContext
object containing earlier conversation messages through the
chat_ctx
parameter. This way, your agent starts off with a meaningful context. • Update Context of a Running Agent: If your session is already active, you can use the
update_chat_ctx()
method to dynamically refresh the current context. This updates both the local and server-side contexts seamlessly. • Reuse Context from Previous Sessions: Once a session ends, you can grab the conversation history using
session.chat_ctx_copy()
and then pass it along to a new agent instance to keep the conversation flowing smoothly. Just a quick note: Gemini Realtime doesn’t support removing messages from the chat context. The context works in an additive way, so you can add or replace the full context but can’t delete individual messages. If you’d like, I’m here to provide more details or walk you through some examples—just let me know! Sources: Agent Session & Activity Management | livekit/livekit_composite | DeepWiki | agent/main.py | plugins/google/src/beta/realtime/realtime_api.ts | Agents v0.x migration guide - Python | LiveKit Docs | complex-agents/personal_shopper/README.md
m
sorry, it doesn’t help. i’m using typescript library and it’s not working
My AI says: the text→voice context feature is not feasible with the current LiveKit Agents SDK architecture because: 1. The session must be started before we receive the context data 2. Modifying the agent/session after it starts corrupts Gemini’s internal state 3. The SDK doesn’t provide a safe API to update context dynamically
t
To update the conversation history on a Gemini real-time agent using the TypeScript library, here’s a simple step-by-step guide to help you through the process: 1. Initialize the RealtimeModel: Start by creating a new instance of the model with the configuration that fits your needs. 2. Create a RealtimeSession: Use your model instance to open a session. 3. Prepare the Updated Chat Context: Utilize
llm.ChatContext.fromMessages
to build a new chat context with the messages you want to include. 4. Update the Chat Context: Then, call
session.updateChatCtx(updatedChatContext)
to apply your new context. This will update both the local and server-side context seamlessly. Here’s an example to make it clearer:
Copy code
import { RealtimeModel, RealtimeSession } from '@livekit/agents';
import * as llm from '@livekit/agents/llm';

const model = new RealtimeModel({
  instructions: "You are a helpful assistant.",
  model: 'gemini-2.0-flash-live-001',
  voice: 'Puck',
  temperature: 0.8,
});

const session: RealtimeSession = model.session();

const updatedChatContext = llm.ChatContext.fromMessages([
  llm.ChatMessage.create({ role: 'user', content: 'Hello, I want to ask about weather.' }),
  llm.ChatMessage.create({ role: 'assistant', content: 'I can help with that.' }),
]);

await session.updateChatCtx(updatedChatContext);
Just a quick note: Gemini Realtime doesn’t currently support removing messages from the chat context, so be sure to keep the full conversation history when you update. If you run into any issues or have questions along the way, please don’t hesitate to reach out—I’m here to help you! Sources: plugins/google/src/beta/realtime/realtime_api.ts | examples/voice_agents/README.md | agent/main.py
m
The TypeScript types might not have
fromMessages
. Let me check if it’s a constructor or different method:
The error shows
Cannot read properties of undefined (reading 'push')
- the
ChatContext
doesn’t have a
messages
array initialized. And it’s still timing out, which means even attempting to call
updateChatCtx
is breaking the session. The fundamental issue: We’re trying to update the chat context AFTER the session has started and after the greeting has been sent. This corrupts Gemini’s state.