Skip to content

Fix Copilot codex model Responses API translation for Claude Code#233

Merged
luispater merged 1 commit intorouter-for-me:mainfrom
ultraplan-bit:fix/copilot-codex-responses-translation
Feb 16, 2026
Merged

Fix Copilot codex model Responses API translation for Claude Code#233
luispater merged 1 commit intorouter-for-me:mainfrom
ultraplan-bit:fix/copilot-codex-responses-translation

Conversation

@ultraplan-bit
Copy link

  • Add response.function_call_arguments.delta handler for tool call parameters
  • Rewrite normalizeGitHubCopilotResponsesInput to produce structured input array (message/function_call/function_call_output) instead of flattened text, fixing infinite loop in multi-turn tool-use conversations
  • Skip flattenAssistantContent for messages containing tool_use blocks, preventing function_call items from being destroyed
  • Add reasoning/thinking stream & non-stream support
  • Fix stop_reason mapping (max_tokens/stop) and cached token reporting
  • Update test to match new array-based input format

- Add response.function_call_arguments.delta handler for tool call parameters
- Rewrite normalizeGitHubCopilotResponsesInput to produce structured input
  array (message/function_call/function_call_output) instead of flattened
  text, fixing infinite loop in multi-turn tool-use conversations
- Skip flattenAssistantContent for messages containing tool_use blocks,
  preventing function_call items from being destroyed
- Add reasoning/thinking stream & non-stream support
- Fix stop_reason mapping (max_tokens/stop) and cached token reporting
- Update test to match new array-based input format

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gemini-code-assist
Copy link

Summary of Changes

Hello @ultraplan-bit, 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 significantly enhances the translation layer for Copilot Codex model responses to the Claude API, focusing on improving the fidelity and functionality of complex conversational interactions. The changes ensure that multi-turn tool-use scenarios are handled robustly, reasoning processes are accurately conveyed, and token usage is reported with greater precision, leading to a more reliable and feature-rich integration.

Highlights

  • Structured Input for Multi-Turn Tool-Use: The normalizeGitHubCopilotResponsesInput function was rewritten to convert Claude messages into a structured input array, preserving conversation context, roles, tool calls, and tool results. This change specifically addresses and fixes infinite loops encountered in multi-turn tool-use conversations.
  • Tool Call Argument Handling: A new handler was added for response.function_call_arguments.delta events, ensuring that tool call parameters are correctly processed and translated in streaming responses from the Copilot Codex model.
  • Reasoning/Thinking Support: Support for 'reasoning' or 'thinking' blocks has been integrated for both non-streaming and streaming responses, allowing the model's internal thought processes to be represented in the Claude API format.
  • Improved Stop Reason and Token Reporting: The mapping for stop_reason now correctly propagates max_tokens and stop reasons. Additionally, token reporting has been enhanced to accurately account for and subtract cached tokens from the input token count.
  • Preservation of Non-Text Content: The flattenAssistantContent function was updated to skip flattening content that contains non-text blocks (e.g., tool_use), preventing the unintended destruction of function_call items.

🧠 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
  • internal/runtime/executor/github_copilot_executor.go
    • Modified flattenAssistantContent to skip flattening content with non-text blocks like tool_use.
    • Rewrote normalizeGitHubCopilotResponsesInput to transform Claude messages into a structured input array, handling system messages, user/assistant content (text, image), tool calls, and tool results.
    • Added ReasoningActive and ReasoningIndex fields to githubCopilotResponsesStreamState for stream processing.
    • Implemented logic in translateGitHubCopilotResponsesNonStreamToClaude to translate 'reasoning' items into 'thinking' blocks.
    • Updated token usage calculation in non-streaming translation to subtract cached tokens and added cache_read_input_tokens.
    • Corrected stop_reason mapping in non-streaming translation to include max_tokens and stop.
    • Added stream event handlers for response.reasoning_summary_part.added, response.reasoning_summary_text.delta, and response.reasoning_summary_part.done to support streaming reasoning blocks.
    • Introduced a new stream event handler for response.function_call_arguments.delta to correctly process tool call arguments.
    • Adjusted token usage calculation and stop_reason mapping in streaming translation to handle cached tokens and specific stop reasons.
  • internal/runtime/executor/github_copilot_executor_test.go
    • Updated TestNormalizeGitHubCopilotResponsesInput_MissingInputExtractedFromSystemAndMessages to assert that the input is now an array and that messages and system fields are removed after normalization.
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

@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

The pull request significantly improves the translation of GitHub Copilot Codex Responses API for Claude Code. It addresses a critical issue where multi-turn tool-use conversations would fail due to flattened input text, by implementing a structured input array that preserves conversation history (roles, tool calls, and results). Additionally, it adds support for reasoning/thinking blocks in both streaming and non-streaming modes, fixes token reporting for cached tokens, and improves stop reason mapping. My feedback focuses on ensuring the structured input for images follows the expected API schema and refining the stop reason mapping for full Claude compatibility.

Comment on lines +705 to +706
part := `{"type":"input_image","image_url":""}`
part, _ = sjson.Set(part, "image_url", fmt.Sprintf("data:%s;base64,%s", mediaType, data))

Choose a reason for hiding this comment

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

high

The structure for input_image in the OpenAI Responses API (which Copilot Codex follows) expects image_url to be an object containing a url field, rather than a direct string. Providing it as a string may cause the upstream API to reject the request.

							part := `{"type":"input_image","image_url":{"url":""}}`\n							part, _ = sjson.Set(part, "image_url.url", fmt.Sprintf("data:%s;base64,%s", mediaType, data))

Comment on lines +994 to +995
} else if sr := root.Get("stop_reason").String(); sr == "max_tokens" || sr == "stop" {
out, _ = sjson.Set(out, "stop_reason", sr)

Choose a reason for hiding this comment

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

medium

For Claude-compatible responses, the stop_reason should be mapped to Claude's expected enum values. While max_tokens is valid, OpenAI's stop should be mapped to Claude's end_turn to ensure clients (like Claude Code) correctly interpret the completion.

	} else if sr := root.Get("stop_reason").String(); sr == "max_tokens" || sr == "stop" {\n		if sr == "stop" { sr = "end_turn" }\n		out, _ = sjson.Set(out, "stop_reason", sr)

Comment on lines +1192 to +1193
} else if sr := gjson.GetBytes(payload, "response.stop_reason").String(); sr == "max_tokens" || sr == "stop" {
stopReason = sr

Choose a reason for hiding this comment

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

medium

Similar to the non-streaming case, the stop reason from the upstream API should be mapped to end_turn for Claude compatibility in the streaming response.

			} else if sr := gjson.GetBytes(payload, "response.stop_reason").String(); sr == "max_tokens" || sr == "stop" {\n				if sr == "stop" { stopReason = "end_turn" } else { stopReason = sr }

@luispater luispater merged commit 8fc0b08 into router-for-me:main Feb 16, 2026
2 checks passed
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