fix(google): handle late-arriving toolCalls in Gemini realtime API #937
+20
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes tool call loss that occurs when Gemini sends tool calls in separate WebSocket messages after signaling turn completion.
When using Gemini 2.5 Flash live native audio 12-2025 with dynamic thinking, tool calls may arrive in separate WebSocket messages after turnComplete. Previously, when turnComplete arrived, all channels including functionChannel were closed and the generation was marked done. If a toolCall then arrived in a subsequent message, it would trigger a new generation with a new functionChannel - but Agent Activity was still reading from the old (now closed) channel, causing the tool call to be lost.
This fix ensures tool calls are written to the correct functionChannel and not lost:
Changes Made
markCurrentGenerationDone()to accept optionalkeepFunctionChannelOpenparameter, allowing channels to remain open for late-arriving tool callsonReceiveMessage()to only create new generations forserverContentor standalonetoolCallmessages (when functionChannel is already closed)handleToolCall()to properly close the functionChannel after writing tool calls and detect closed channelsstartNewGeneration()to close orphaned functionChannels from previous turns that didn't receive tool callsPre-Review Checklist
Testing
restaurant_agent.tsandrealtime_agent.tswork properly (for major changes) - specifically related to realtime agent tool handlingAdditional Notes
Note to reviewers: Please ensure the pre-review checklist is completed before starting your review.