Fix duplicate/empty tool_use blocks in OpenAI->Claude streaming translation#1579
Fix duplicate/empty tool_use blocks in OpenAI->Claude streaming translation#1579lightsofapollo wants to merge 1 commit intorouter-for-me:mainfrom
Conversation
…ders - Emit exactly one tool_use content block per tool call in streaming mode - Canonicalize returned tool names to match declared request tool names (case-insensitive) - Add regression tests for duplicate/empty-name tool_use starts and non-stream name canonicalization
Summary of ChangesHello @lightsofapollo, 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 addresses a critical bug in the OpenAI to Claude response translation path that could lead to broken tool execution in Claude clients. The issue stemmed from OpenAI-compatible providers streaming 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. Changelog
Activity
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.
Code Review
This pull request effectively fixes a bug in the OpenAI to Claude streaming translation for tool calls. The changes correctly handle fragmented deltas by ensuring tool_use blocks are started only once and by canonicalizing tool names. The addition of regression tests for both streaming and non-streaming modes is a great way to ensure the fix is robust. I have one suggestion to refactor some duplicated code for better maintainability.
| if !accumulator.Started { | ||
| if strings.TrimSpace(accumulator.Name) == "" { | ||
| delete(param.ToolCallBlockIndexes, index) | ||
| continue | ||
| } | ||
| if accumulator.ID == "" { | ||
| accumulator.ID = fmt.Sprintf("call_%d", index) | ||
| } | ||
| contentBlockStartJSON := `{"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"","name":"","input":{}}}` | ||
| contentBlockStartJSON, _ = sjson.Set(contentBlockStartJSON, "index", blockIndex) | ||
| contentBlockStartJSON, _ = sjson.Set(contentBlockStartJSON, "content_block.id", accumulator.ID) | ||
| contentBlockStartJSON, _ = sjson.Set(contentBlockStartJSON, "content_block.name", accumulator.Name) | ||
| results = append(results, "event: content_block_start\ndata: "+contentBlockStartJSON+"\n\n") | ||
| accumulator.Started = true | ||
| } |
There was a problem hiding this comment.
Summary
This fixes a response-translation bug in the OpenAI->Claude path that could break tool execution in Claude clients.
Problem
Some OpenAI-compatible providers stream
tool_callsin fragmented deltas:function.nameappears once, then appears again as empty in later chunksThe previous translator could emit multiple
content_block_startevents for the same tool call, including an empty tool name, which can cause client-side tool execution failures.Changes
tool_usecontent_block_startexactly once per tool call in streaming mode.function.namedeltas after start.bash->Bash.Testing
tool_usestart is emittedtool_useblocksgo test ./...Context / Risk