Google Gemini Live realtime: tool-call result is lost when update_tools() restarts the session (and generation-completion handling breaks newer Live preview models)
Bug Description
In livekit-plugins-google realtime (livekit/plugins/google/realtime/realtime_api.py),
the RealtimeSession has two related problems:
1. Tool result dropped across a session restart.
When something restarts the underlying Gemini Live websocket mid-turn β most
commonly update_tools() setting _session_should_close β a function-tool
result produced during that restart window is sent with _send_client_event(...)
on the old, closing session. It is never replayed to the new session after
reconnect, so the model never receives the LiveClientToolResponse. The turn
then stalls: the model is still waiting on a function response it will never get,
and the conversation hangs.
2. Generation-completion handling assumes older Gemini Live turn semantics.
The receive loop and generate_reply() assume (a) only models advertising
mutable_chat_context may generate replies, and (b) every model_turn event
maps to an active generation. Newer Gemini Live preview models stream
generation / model_turn events with different completion timing, so with them:
generate_reply()is rejected outright, and- stray
model_turnresponses that arrive with no active generation are mishandled (double-counted / attached to the wrong generation).
Expected Behavior
- A pending tool result must survive a session restart: it should be buffered
and replayed to the reconnected session (via
session.send_tool_response(...)) so the model receives the function response and continues the turn instead of hanging. - The realtime session should track generation-completion explicitly (not infer
it from model name /
mutable_chat_context), so newer Gemini Live models are handled correctly andgenerate_reply()works for Live models that support it.
Reproduction Steps
1. Create an `AgentSession` with a Google realtime model
(`gemini-live-2.5-flash-preview`, realtime=True).
2. Register a `@function_tool`.
3. During a turn where the model invokes the tool, trigger `update_tools()`
(e.g. a dynamic tool change or agent handoff) so the Live session restarts
right around when the tool result is produced.
4. Observe: the model never receives the tool result β the turn produces no
follow-up response and hangs.
Expected: the model receives the result and replies normally.
Operating System
macOS
Models Used
No response
Package Versions
livekit==1.1.13
livekit-agents==1.6.5
livekit-plugins-google==1.6.5
livekit-api (latest)
Session/Room/Call IDs
No response
Proposed Solution
- Add a `_pending_tool_result` buffer on the RealtimeSession. If a tool result is
produced while the session is restarting (`_session_should_close` set), stash it
instead of sending it to the dying socket. On reconnect in `_main_task`, replay
it with `session.send_tool_response(function_responses=...)` before resuming, and
nudge the model to continue.
- Replace name/`mutable_chat_context`-based assumptions with an explicit
`_generation_completed` state so `model_turn`/generation events are handled
correctly for newer Live models, and allow `generate_reply()` for Live models
that support it.
A proof-of-concept patch demonstrating both fixes exists in a community fork
(livekit-google-plugin) β but it hardcodes model-name string checks
(`== "gemini-3.1-flash-live-preview"`) which should be replaced with
capability/behavior detection before merging upstream.
Additional Context
The two issues share one code path (the tool-result send block and the
_main_task reconnect handler), so they're best fixed together. The tool-result
replay fix is model-agnostic and valuable on its own, independent of any specific
Gemini Live model.
Screenshots and Recordings
No response
I'd like to work on this, will raise a PR shortly for this.
Hi @chenghao-mou @longcw, can you pls review this https://github.com/livekit/agents/pull/7153 fix
Hi @chenghao-mou @longcw, could you please review this fix and consider merging it
Weβve been maintaining this fix on our end since 20 April 2026. Currently, whenever we need to upgrade the LiveKit Agent, we also have to re-apply the latest fixes/changes related to the Google plugin on our side.
To avoid having to maintain and re-apply these changes manually with every LiveKit Agent upgrade, it would be great if you could review the fix and, if possible, merge it upstream.
This would make future upgrades much easier for us and help avoid any additional maintenance on our end.
Thanks for taking a look!
@Darshak03 thanks for the pr, I left a comment there, otherwise the fix looks good to me.
fixed in #6000
@Darshak03 please feel free to reopen an issue if there are cases not handled in #6000
Hi @longcw, Thanks for that fix
I wanted to point out a few issues fixes that also weβre currently maintaining on our end:
- https://github.com/livekit/agents/issues/7189
- https://github.com/livekit/agents/issues/7192
- https://github.com/livekit/agents/issues/7195
If possible, could you please take a look
Thanks again for your help!
Hi @longcw,
Whenever you have a moment, could you please take a look at the problem and the approach in each? It would really help us β all three are patches we're carrying on our own fork today, so landing them upstream means we can finally drop it.