Skip to content

fix(responses): finalize stream on [DONE] without finish_reason#1087

Open
taoluo wants to merge 1 commit intorouter-for-me:devfrom
taoluo:main
Open

fix(responses): finalize stream on [DONE] without finish_reason#1087
taoluo wants to merge 1 commit intorouter-for-me:devfrom
taoluo:main

Conversation

@taoluo
Copy link

@taoluo taoluo commented Jan 18, 2026

fix #1085
Caveat: the patch is generated solely by github copilot, please help review it.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @taoluo, 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 enhances the robustness of streaming response handling by ensuring that the response stream is properly finalized when a [DONE] signal is received, regardless of whether a finish_reason is explicitly provided. This change centralizes the finalization process and prevents duplicate completion events, leading to more consistent and reliable stream termination for various OpenAI-compatible API providers.

Highlights

  • Stream Finalization Logic Refactor: The logic for finalizing a streaming response, including emitting various 'done' events and the 'response.completed' event, has been extracted into a new finalize function. This centralizes the finalization process.
  • Handling [DONE] without finish_reason: The system now correctly finalizes the stream upon receiving the [DONE] terminator, even if the finish_reason field is not present in the upstream response. This mirrors behavior seen in other response translators (e.g., openai_claude_response.go).
  • Preventing Duplicate Finalization: A new boolean field CompletedSent has been added to the oaiToResponsesState struct to ensure that the finalization logic is executed only once per stream, preventing redundant 'completed' events.
  • New Test Cases: Dedicated test cases have been added to verify that both reasoning and text content streams are correctly finalized when the [DONE] signal is received without an explicit finish_reason.

🧠 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 addresses an issue where streaming responses were not finalized correctly upon receiving a [DONE] message without a finish_reason. The fix involves refactoring the finalization logic into a new finalize function, which is now called in both scenarios. This is a good improvement for robustness. The PR also includes relevant test cases to verify the fix. My review focuses on improving code quality by suggesting the replacement of custom bubble sort implementations with the more efficient and idiomatic sort.Ints from Go's standard library.

Comment on lines +88 to +94
for i := 0; i < len(idxs); i++ {
for j := i + 1; j < len(idxs); j++ {
if idxs[j] < idxs[i] {
idxs[i], idxs[j] = idxs[j], idxs[i]
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This bubble sort implementation is not idiomatic Go. For sorting, it's better to use the standard library's sort.Ints function, which is more efficient (typically IntroSort) and improves code readability. This would require importing the sort package.

This same sorting logic is repeated in multiple places within this function. Applying this change consistently will improve the overall code quality.

			sort.Ints(idxs)

Comment on lines +148 to +154
for i := 0; i < len(idxs); i++ {
for j := i + 1; j < len(idxs); j++ {
if idxs[j] < idxs[i] {
idxs[i], idxs[j] = idxs[j], idxs[i]
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Similar to the previous comment, this bubble sort can be replaced with sort.Ints(idxs) for better performance and readability. Remember to import the sort package.

			sort.Ints(idxs)

Comment on lines +266 to +272
for i := 0; i < len(midxs); i++ {
for j := i + 1; j < len(midxs); j++ {
if midxs[j] < midxs[i] {
midxs[i], midxs[j] = midxs[j], midxs[i]
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This is another instance of a bubble sort. Please replace it with sort.Ints(midxs) from the standard sort package.

			sort.Ints(midxs)

Comment on lines +290 to +296
for i := 0; i < len(idxs); i++ {
for j := i + 1; j < len(idxs); j++ {
if idxs[j] < idxs[i] {
idxs[i], idxs[j] = idxs[j], idxs[i]
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This bubble sort can be replaced with sort.Ints(idxs). The comment // small-N sort without extra imports is noted, but using the standard library's sort package is generally preferred for maintainability and performance, even for small N.

			sort.Ints(idxs)

@luispater luispater changed the base branch from main to dev January 31, 2026 23:06
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.

Streaming Response Translation Fails to Emit Completion Events on [DONE] Marker

1 participant