-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(translator): stabilize tool_call finish_reason #865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @wxyzh, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on enhancing the robustness and accuracy of Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces several improvements to stabilize finish_reason handling in streaming responses, particularly for tool call scenarios across various translators. The changes correctly prioritize response.stop_reason, delay setting the finish_reason to prevent it from being overridden by STOP chunks in tool call flows, and introduce whitelisting to filter finish_reason values for both OpenAI and Claude formats. Additionally, some logic has been simplified by replacing switch statements with if conditions, which enhances code readability.
The implementation is solid, but I've identified a recurring logical error in the OpenAI translators where a check for an uppercase "STOP" is performed on a variable that has already been converted to lowercase. I've left comments with suggestions to fix this. Overall, these changes are a good step towards making the translators more robust.
| template, _ = sjson.Set(template, "choices.0.native_finish_reason", "tool_calls") | ||
| } else if finishReason != "" && (*param).(*convertCliResponseToOpenAIChatParams).FunctionIndex == 0 { | ||
| // Only pass through specific finish reasons | ||
| if finishReason == "max_tokens" || finishReason == "STOP" || finishReason == "stop" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The finishReason variable is converted to lowercase on line 92. Therefore, the condition finishReason == "STOP" will always evaluate to false and is redundant.
| if finishReason == "max_tokens" || finishReason == "STOP" || finishReason == "stop" { | |
| if finishReason == "max_tokens" || finishReason == "stop" { |
| template, _ = sjson.Set(template, "choices.0.native_finish_reason", "tool_calls") | ||
| } else if finishReason != "" && (*param).(*convertCliResponseToOpenAIChatParams).FunctionIndex == 0 { | ||
| // Only pass through specific finish reasons | ||
| if finishReason == "max_tokens" || finishReason == "STOP" || finishReason == "stop" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The finishReason variable is converted to lowercase on line 90. Therefore, the condition finishReason == "STOP" will always evaluate to false and is redundant.
| if finishReason == "max_tokens" || finishReason == "STOP" || finishReason == "stop" { | |
| if finishReason == "max_tokens" || finishReason == "stop" { |
| template, _ = sjson.Set(template, "choices.0.native_finish_reason", "tool_calls") | ||
| } else if finishReason != "" && (*param).(*convertGeminiResponseToOpenAIChatParams).FunctionIndex == 0 { | ||
| // Only pass through specific finish reasons | ||
| if finishReason == "max_tokens" || finishReason == "STOP" || finishReason == "stop" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The finishReason variable is converted to lowercase on line 94. Therefore, the condition finishReason == "STOP" will always evaluate to false and is redundant.
| if finishReason == "max_tokens" || finishReason == "STOP" || finishReason == "stop" { | |
| if finishReason == "max_tokens" || finishReason == "stop" { |
Co-authored-by: Amp <[email protected]> Amp-Thread-ID: https://ampcode.com/threads/T-019b8d07-bb78-775f-acea-61caa9f26dc7
a51ffab to
7847813
Compare
This PR stabilizes finish_reason handling in streaming responses, particularly for tool call scenarios.
Issues Fixed:
Tool call finish_reason being overridden by subsequent STOP chunks
Missing finish_reason in streaming responses causing client confusion
Unfiltered upstream finish_reason values passed through to clients
Changes
Commit 1: Fix stop_reason mapping in streaming responses
Author: @HsnSaboor
Fix Codex → Claude to prioritize upstream stop_reason
Fix Gemini/Gemini CLI → Claude to handle MAX_TOKENS correctly
Commit 2: Stabilize streaming finish_reason mapping
Delay finish_reason setting to prevent STOP chunk override in tool call flows
Simplify switch statements in Antigravity → Claude translator
Add FunctionIndex == 0 condition to control finish_reason emission
Commit 3: Add finish_reason whitelist filter
Whitelist filter for OpenAI format: stop/STOP/max_tokens/tool_calls
Whitelist filter for Claude format: end_turn/max_tokens/stop/tool_use
Prevent invalid upstream values from breaking client behavior
Affected Translators
OpenAI Format:
Gemini → OpenAI
Gemini CLI → OpenAI
Antigravity → OpenAI
Claude Format:
Codex → Claude
Gemini → Claude
Gemini CLI → Claude
Antigravity → Claude