agents icon indicating copy to clipboard operation
agents copied to clipboard

Google Gemini Live realtime: tool-call result is lost when update_tools() restarts the session (and generation-completion handling breaks newer Live preview models)

Open areebkhan-tech opened this issue 4 months ago β€’ 7 comments

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_turn responses that arrive with no active generation are mishandled (double-counted / attached to the wrong generation).

Expected Behavior

  1. 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.
  2. 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 and generate_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

areebkhan-tech avatar Jul 18 '26 12:07 areebkhan-tech

I'd like to work on this, will raise a PR shortly for this.

ByteMaster-1 avatar Jul 18 '26 12:07 ByteMaster-1

Hi @chenghao-mou @longcw, can you pls review this https://github.com/livekit/agents/pull/7153 fix

Darshak03 avatar Sep 07 '26 10:09 Darshak03

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 avatar Sep 09 '26 05:09 Darshak03

@Darshak03 thanks for the pr, I left a comment there, otherwise the fix looks good to me.

longcw avatar Sep 09 '26 07:09 longcw

fixed in #6000

@Darshak03 please feel free to reopen an issue if there are cases not handled in #6000

longcw avatar Sep 09 '26 09:09 longcw

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!

Darshak03 avatar Sep 09 '26 20:09 Darshak03

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.

Darshak03 avatar Sep 12 '26 11:09 Darshak03