fix: fix tool-calls finish reason conversion in streaming mode for newer gemini models like gemini-3.5-flash - #2399
Merged
aabchoo merged 3 commits intoJul 20, 2026
Conversation
Signed-off-by: yxia216 <yxia216@bloomberg.net>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2399 +/- ##
==========================================
+ Coverage 84.84% 84.85% +0.01%
==========================================
Files 151 151
Lines 22086 22090 +4
==========================================
+ Hits 18739 18745 +6
+ Misses 2215 2214 -1
+ Partials 1132 1131 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Signed-off-by: yxia216 <yxia216@bloomberg.net>
aabchoo
approved these changes
Jul 20, 2026
Contributor
|
/retest |
aabchoo
enabled auto-merge (squash)
July 20, 2026 19:45
Hritik003
pushed a commit
to Hritik003/ai-gateway
that referenced
this pull request
Jul 27, 2026
…wer gemini models like gemini-3.5-flash (envoyproxy#2399) **Description** ***Problem*** When the model returns a tool call, OpenAI expects the finish reason to be `tool_calls` while Gemini returns `STOP`. We convert it via ``` if len(toolCalls) > 0 { return openai.ChatCompletionChoicesFinishReasonToolCalls } ``` This was correct for streaming mode because the finish reason and the tool calls are in the same chunk in the previous Gemini models. However, for newer Gemini models like gemini-3.5-flash, they are split into **two** chunks. The `functionCall` chunk has **no** `finishReason`, and a **trailing** chunk carries `finishReason: STOP` with an empty text part and **no** `functionCall`: ```jsonc // raw GCP chunk 1 — has the tool call, no finishReason {"candidates":[{"content":{"parts":[{"functionCall":{"name":"get_current_weather","args":{"...":"..."}}, "thoughtSignature":"..."}]}}], "usageMetadata":{"trafficType":"ON_DEMAND"}} // raw GCP chunk 2 — terminal STOP, empty text, no functionCall {"candidates":[{"content":{"parts":[{"text":""}]},"finishReason":"STOP"}], "usageMetadata":{"promptTokenCount":107,"candidatesTokenCount":31,"thoughtsTokenCount":148,"...":"..."}} ``` In this case, the conversion would be invalid. Thus, we need to remember whether the model returned a tool call during the streaming mode. --------- Signed-off-by: yxia216 <yxia216@bloomberg.net>
3 tasks
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.
Description
Problem
When the model returns a tool call, OpenAI expects the finish reason to be
tool_callswhile Gemini returnsSTOP. We convert it viaThis was correct for streaming mode because the finish reason and the tool calls are in the same chunk in the previous Gemini models. However, for newer Gemini models like gemini-3.5-flash, they are split into two chunks. The
functionCallchunk hasno
finishReason, and a trailing chunk carriesfinishReason: STOPwith an emptytext part and no
functionCall:In this case, the conversion would be invalid. Thus, we need to remember whether the model returned a tool call during the streaming mode.