Repository navigation
Python: Set the assistant role on streamed Gemini updates - #9491
Open
Aditya Kumar (Aditya-XR) wants to merge 1 commit into
Open
Aditya Kumar (Aditya-XR) wants to merge 1 commit into
Aditya Kumar (Aditya-XR) wants to merge 1 commit into
Conversation
GeminiChatClient built streaming ChatResponseUpdates without a role. After the function-invocation layer adds a tool-role update, the next role-less text was appended to the tool message, and the Gemini serializer then dropped it, so the answer was missing from the next turn. The non-streaming path already uses the assistant role, and the other providers set it on every streamed update.
Aditya Kumar (Aditya-XR)
requested review from
SergeyMenshykh,
Tao Chen (TaoChenOSU),
Eduard van Valkenburg (eavanvalkenburg),
Giles Odigwe (giles17),
Jose Alvarez (jpalvarezl),
Evan Mattson (moonbox3),
Roger Barreto (rogerbarreto),
sophia-ramsey and
westey (westey-m)
as code owners
October 18, 2026 07:50
Aditya Kumar (Aditya-XR)
deployed
to
github-app-auth
October 18, 2026 07:50 — with
GitHub Actions
Active
Aditya Kumar (Aditya-XR)
deployed
to
github-app-auth
October 18, 2026 07:50 — with
GitHub Actions
Active
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation & Context
With
stream=True,GeminiChatClientcreates everyChatResponseUpdatewithout a role. After a tool call, the function-invocation layer adds the tool result as arole="tool"update, and the answer that Gemini streams next is appended to that tool message. The Gemini request builder keeps onlyfunction_resultcontent from tool messages, so the answer is dropped from the history and Gemini never sees it on the next turn. Without streaming the same agent gives the expectedassistant,tool,assistantmessages.Description & Review Guide
What are the major changes?
_process_chunknow builds each streamed update withrole="assistant", like the non-streaming path and the Anthropic, Bedrock, Mistral and Ollama clients:The docstring of
_process_chunknow says it returns an assistant update. Two new tests intest_gemini_client.pycover the change:test_streaming_updates_have_assistant_rolechecks that each streamed update has the assistant role. Without the fix the roles are[None, None].test_streaming_answer_after_tool_call_stays_in_historystreams a tool call followed by an answer throughGeminiChatClientwith function invocation. It checks that the final response isassistant,tool(only the function result),assistant(the answer), and that the answer is sent back as amodelturn on the next request. Without the fix the answer is inside the tool message and is missing from the next request.What is the impact of these changes?
Streaming Gemini agents that call tools keep their final answer as an assistant message, so the answer stays in the session history and is sent back to Gemini on later turns. Streams without tool calls are unchanged in practice: the first message of a response already defaulted to the assistant role. Consumers that read
update.rolenow get"assistant"instead ofNone.What do you want reviewers to focus on?
The role is set on every chunk, including chunks that carry only usage or a finish reason, and chunks with hosted tool calls and results (code execution). Non-streaming already puts all of these in one assistant message, so streaming now matches it.
Related Issue
Fixes #9490
This is a one-line correction that makes streaming match the non-streaming path and the other providers (the same fix as #4329 for Anthropic), so I opened it together with the issue.
AI Assistance
AI assistance details: I used Claude Code as an assistant to help find the bug and to draft the fix, the tests, the linked issue and this description. I did the final review myself: I read and understood every change, checked the repro and test results, and made the final decisions on what to submit.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.