Skip to content

Conversation

@wxyzh
Copy link

@wxyzh wxyzh commented Jan 5, 2026

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

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 finish_reason and stop_reason handling across various API translators. The changes ensure that streaming responses consistently provide correct termination reasons, especially in tool call contexts, by preventing overrides, filling in missing values, and filtering out invalid ones. This leads to more predictable and reliable client interactions with the translated API responses.

Highlights

  • Stabilized Finish Reason Handling: Addressed issues where finish_reason was being incorrectly overridden by subsequent STOP chunks in streaming responses, particularly for tool call scenarios.
  • Resolved Missing Finish Reasons: Fixed instances where finish_reason was missing in streaming responses, which previously caused confusion for client applications.
  • Implemented Finish Reason Whitelisting: Introduced whitelist filters for finish_reason values to prevent invalid upstream values from being passed through to clients, ensuring consistent and expected behavior across different API formats (OpenAI and Claude).
  • Improved Stop Reason Mapping: Enhanced the mapping logic for stop_reason in streaming responses, prioritizing upstream values and correctly handling MAX_TOKENS for various translators (Codex, Gemini, Gemini CLI, Antigravity).
  • Simplified Translator Logic: Refactored and simplified switch statements in several translators (e.g., Antigravity to Claude) and delayed finish_reason setting to prevent premature overrides.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The finishReason variable is converted to lowercase on line 92. Therefore, the condition finishReason == "STOP" will always evaluate to false and is redundant.

Suggested change
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" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The finishReason variable is converted to lowercase on line 90. Therefore, the condition finishReason == "STOP" will always evaluate to false and is redundant.

Suggested change
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" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The finishReason variable is converted to lowercase on line 94. Therefore, the condition finishReason == "STOP" will always evaluate to false and is redundant.

Suggested change
if finishReason == "max_tokens" || finishReason == "STOP" || finishReason == "stop" {
if finishReason == "max_tokens" || finishReason == "stop" {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants