agents icon indicating copy to clipboard operation
agents copied to clipboard

Gemini realtime: audio trailing a tool call starts an empty generation and truncates the reply

Open Darshak03 opened this issue 2 months ago • 1 comments

Bug Description

When a tool call ends a Gemini Live turn, the server can keep streaming model_turn audio belonging to that finished turn. _handle_tool_calls has already finalized the generation, so those frames hit the not self._current_generation or self._current_generation._done branch in _recv_task, and _is_new_generation() returns True for any model_turn:

if (sc := resp.server_content) and (
    sc.model_turn
    or (sc.output_transcription and sc.output_transcription.text)
    ...
):
    return True

A single trailing audio frame therefore opens a second generation for a turn that is already over. From a live call:

input_transcription(finished, "My Monster Jam device is not working.")
  -> new generation started: GR_e618ac79ab58
  ... model_turn audio + output_transcription stream the full reply ...
tool_call  Get_Products
  -> generation done: GR_e618ac79ab58
     output_text='I completely understand how frustrating that must be.
                  Please give me just a moment while I pull up your details.'
output_transcription(finished=True)          # no .text, correctly ignored
model_turn(inline_data=audio/pcm)            # trailing frame from the finished turn
  -> new generation started: GR_ffb4da1884e7
generation_complete=True
turn_complete=True, usage_metadata(total_token_count=1230)
  -> generation done: GR_ffb4da1884e7  output_text=''   (lived 0.5 ms)

Three things go wrong as a result:

  1. The assistant message is truncated in the chat context. The spurious generation interrupts the playing one, so the item is committed as content: ['I completely'], interrupted: True, even though the plugin holds the full sentence in output_text.
  2. _SegmentSynchronizerImpl.playback_finished called before text/audio input is done is logged, the synchronizer having been cut off mid-turn.
  3. Usage is attributed to the wrong generation. usage_metadata arrives with turn_complete, by which point _current_generation is the empty one, so the turn's tokens are recorded against a generation that produced nothing.

_is_new_generation() already carries a comment about this class of event — it excludes empty transcriptions and generation_complete for exactly this reason — but model_turn is not excluded.

Expected Behavior

Audio that belongs to a turn already ended by a tool call does not start a new generation. The assistant message keeps its full text, the turn is not marked interrupted, and the usage for the turn is attributed to the generation that produced it.

Reproduction Steps

1. Run an AgentSession with a Gemini realtime model and at least one function tool.
2. Ask something that makes the model speak and then call the tool in the same turn
   (e.g. an acknowledgement followed by a lookup: "My device is not working").
3. Enable LK_GOOGLE_DEBUG=1 and watch the generation logs.
4. Observe a second "new generation started" immediately after "generation done",
   triggered by a model_turn frame, closing with output_text=''.
5. Observe the committed assistant message truncated to the first word or two with
   interrupted=True, and the turn's usage_metadata attached to the empty generation.

Operating System

macOS

Models Used

Gemini Live (RealtimeModel) with function tools; seen on a native-audio Live model with output_audio_transcription enabled

Package Versions

livekit-agents==1.8.0
livekit-plugins-google==1.8.0
google-genai==2.20.0

Session/Room/Call IDs

No response

Proposed Solution

# remember that a tool call ended the turn; the server may keep streaming parts of it
self._turn_ended_by_tool_call = False

# _handle_tool_calls: set it alongside the existing _mark_current_generation_done()
# _start_new_generation and turn_complete: clear it
# generate_reply: clear it, so a reply requested right after a tool call is not suppressed

def _is_new_generation(self, resp: types.LiveServerMessage) -> bool:
    ...
    if (sc := resp.server_content) and (
        (sc.model_turn and not self._turn_ended_by_tool_call)
        or ...
    ):
        return True

Additional Context

The trailing content itself is already handled safely — push_text guards on text_ch.closed and the audio branch guards on audio_ch.closed, both dropping with a warning. The only damage comes from _start_new_generation() being called, so suppressing that is sufficient.

Darshak03 avatar Sep 09 '26 20:09 Darshak03

Thanks for the report! This looks like the same root cause as #5742, so I'm closing it as a duplicate to keep the discussion in one place. Please follow #5742 for updates, and if your case differs (different provider, version, or repro), add the details there and we'll reopen if needed.

davidzhao avatar Oct 10 '26 21:10 davidzhao