Skip to content

fix(relayconvert): keep Responses custom tools on Claude and Gemini upstreams - #7636

Merged
Calcium-Ion merged 6 commits into
mainfrom
fix/responses-custom-tools-claude-gemini
Oct 1, 2026
Merged

Calcium-Ion merged 6 commits into
mainfrom
fix/responses-custom-tools-claude-gemini

Conversation

@Calcium-Ion

Copy link
Copy Markdown
Member

🔗 关联任务 / Related Issue

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)
  • ✨ 新功能 (New feature)
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

📝 变更描述 / Description

Codex 以 Responses 协议发送的 exec / apply_patch(type:"custom")工具,在上游为 Claude Messages 或 Gemini generateContent 时会被整体丢弃:Claude 把定义当作 unsupported_hosted_tool,Gemini 的预处理把定义和 custom_tool_call 历史一起删掉,强制 tool_choice 也被拒绝。结果是模型不知道自己打过哪些 patch,或退回去调用不存在的工具。

改动全部在 relaykit/relayconvert/:

  • 三个目标共用同一套 custom 工具选择逻辑(原 Chat 专用 helper 改成协议无关)。custom 工具作为参数为 {input: string} 的 function 发送,grammar 写进 description;Gemini 的声明走现有的参数清洗。
  • tool_choice {"type":"custom"} 在 Claude 转成 {"type":"tool"},在 Gemini 转成 ANY + allowedFunctionNames,仅当该工具确实被发出时才转换。
  • 历史中的 custom_tool_call 在 Claude / Gemini 一律回放为 {"input": 原始字符串},不再被 ObjectValue 解析成 JSON;custom_tool_call_output 与调用配对。
  • 删除 to_gemini_chat_req_preprocess.go:ExtractRequest 在转换器运行前已清空顶层 tools,它的工具过滤是死代码,历史过滤由转换取代。
  • 请求侧对三个目标都记录 ResponsesToolState。响应侧不改:Claude / Gemini → Responses 本来就经过 Chat → Responses 那一步,复用现有还原逻辑。
  • 包含 fix(relaykit): omit unnamed function calls from Responses output #7219:空名 function call 不再输出到 Responses,流式在拿到有效 name 之前不 announce。

host 侧只改写了 relay/channel/gemini/adaptor_responses_test.go 中断言旧丢弃行为的测试。

📸 运行证明 / Proof of Work

cd relaykit && GOWORK=off go build ./... && GOWORK=off go vet ./relayconvert/... && GOWORK=off go test ./... -count=1
# 全部 ok(dto、relayconvert、convmeta、claude_messages、oai_chat、oai_responses、toolconv、kitutil、reasoning)

go build ./... && go test ./relay/... -count=1
# 18 个包 ok,无 FAIL

grep -rn "PrepareOpenAIResponsesRequest\|custom_tool_omitted\|OpenAIChatCustomToolNames\|openAIChatCustomTool" relaykit/
# 无输出

gofmt -l relaykit/ relay/channel/gemini/
# 仅 relaykit/dto/usage_merge_test.go,与 main 相同,本 PR 未触碰

新增 / 调整的测试:

  • internal/toolconv/policy_test.go:Claude、Gemini 的定义、tool_choice、重名诊断;Strict 策略下报 ConversionLossError。
  • request_registry_test.go:三个目标都记录 ResponsesToolState,无 custom 工具的重试清空;Claude / Gemini 历史里 "{\"a\":1}"、"[1,2]"、普通文本都保持原串;Gemini 的 functionResponse.name 由 callNames 补齐,call_id 属于 custom 调用的 function_call_output 不再被删。
  • response_registry_test.go:Chat、Claude、Gemini 非流式还原为 custom_tool_call;Claude 和 Gemini 流式事件序列 output_item.added → custom_tool_call_input.delta → custom_tool_call_input.done → output_item.done。
  • 变异检查:Claude attach / choice、Gemini attach / schema 清洗 / choice、两侧历史原串、Gemini call / output case、状态记录,这 10 处单独回退都会有测试失败。

未验证:没有对真实 Claude / Gemini 上游和真实 Codex 做端到端探测。

✅ 提交前检查项 / Checklist

  • 人工确认: 无论描述是否由 AI 生成,我已审阅并精炼全文,仅保留可复核的要点,并声明对其准确性与完整性负责。PR 正文及后续评论均未直接粘贴未经人工过滤的大段 AI 文本。
  • 非重复提交: 我已搜索现有的 Issues 与 PRs,确认不是重复提交。
  • 新功能关联 Issue: 若此 PR 标记为 New feature,我已关联对应 Issue;若尚无 Issue,我已先自行创建。
  • 事前沟通: 若改动较大或涉及方向性变更,已在关联 Issue 中与维护者沟通并达成一致。
  • 功能范围: 本 PR 不是 Coding Plan、逆向渠道、第三方封装接口,也不是对 Codex 渠道类型的改动。
  • 非透传: 本 PR 不是针对开启透传后的转发行为;透传会直接发送内容,不会经过 new-api 的逻辑处理。
  • 范围聚焦: 本 PR 为一项聚焦改动,未包含无关代码。
  • 本地验证: 已按变更路径实际验证,并写明命令与观察结果。仅声明 go build 通过或测试通过,不视为有效证明。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

🤖 Generated with Claude Code

495696116 and others added 6 commits October 1, 2026 14:14
A Chat Completions tool call without a function name cannot become a
valid Responses function_call item, so the non-stream conversion now
skips it instead of emitting an item with an empty name.
A streamed tool call is no longer announced while it has no function
name, whether or not custom tools are in play. A call whose name never
arrives is left out of the stream and the completed response. A call
ID that arrives after the first fragment becomes the item ID as long
as the item has not been announced yet.
The two standalone stream tests repeated what the nameless-tool hold
test already covers. Their distinct checks now live in its subtests:
a held function call without custom tools, its late call ID becoming
the item ID, and calls whose name never arrives leaving no event,
output index or sequence number behind, with and without custom tools.
…pstreams

Responses requests sent to Claude Messages dropped Codex custom tools
such as exec and apply_patch as unsupported hosted tools, and Gemini
omitted their definitions and stripped custom_tool_call history before
conversion. A forced custom tool_choice was rejected on both targets,
and Claude parsed JSON-looking custom tool input from history.

Claude and Gemini now get the same treatment as Chat Completions: each
custom tool is sent as a function that takes one string input, with
the grammar kept in the description, and a forced custom tool_choice
becomes the matching named tool choice when that tool was sent. Gemini
declarations go through the usual parameter cleanup.

custom_tool_call history replays the raw input as {"input": ...} on
both targets, and its outputs pair with the call as tool results. The
custom tool selection is shared by all three targets and recorded for
each of them, so the existing Chat to Responses step restores
custom_tool_call items from Claude and Gemini responses as well.
…responses

Claude and Gemini responses reach the Responses format through the
Chat to Responses step, but only the Chat source had a restore test.
The test is now table-driven over all three sources and checks the
non-stream output item and the exact streamed custom tool events.
@coderabbitai

coderabbitai Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 33e3a694-c9dc-45c9-94ec-af59de7a71ba

📥 Commits

Reviewing files that changed from the base of the PR and between 56758ed and 9655969.

📒 Files selected for processing (13)
  • relay/channel/gemini/adaptor_responses_test.go
  • relaykit/relayconvert/convmeta/meta.go
  • relaykit/relayconvert/internal/oai_chat/to_oai_responses_resp.go
  • relaykit/relayconvert/internal/oai_chat/to_oai_responses_resp_test.go
  • relaykit/relayconvert/internal/oai_chat/to_oai_responses_stream_resp.go
  • relaykit/relayconvert/internal/oai_responses/to_claude_messages_req.go
  • relaykit/relayconvert/internal/oai_responses/to_gemini_chat_req.go
  • relaykit/relayconvert/internal/oai_responses/to_gemini_chat_req_preprocess.go
  • relaykit/relayconvert/internal/toolconv/encode.go
  • relaykit/relayconvert/internal/toolconv/policy_test.go
  • relaykit/relayconvert/request_registry.go
  • relaykit/relayconvert/request_registry_test.go
  • relaykit/relayconvert/response_registry_test.go
 _____________________________________________________________________
< RabbitShergill is my Punjabi cousin. He's a bit musically inclined. >
 ---------------------------------------------------------------------
  \
   \   (\__/)
       (•ㅅ•)
       /   づ
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Calcium-Ion
Calcium-Ion merged commit 1a4166d into main Oct 1, 2026
2 of 3 checks passed
Javabcd pushed a commit to Javabcd/new-api that referenced this pull request Oct 9, 2026
…pstreams (QuantumNous#7636)

* fix(relayconvert): drop unnamed function calls from Responses output

A Chat Completions tool call without a function name cannot become a
valid Responses function_call item, so the non-stream conversion now
skips it instead of emitting an item with an empty name.

* fix(relayconvert): hold streamed tool calls until a name arrives

A streamed tool call is no longer announced while it has no function
name, whether or not custom tools are in play. A call whose name never
arrives is left out of the stream and the completed response. A call
ID that arrives after the first fragment becomes the item ID as long
as the item has not been announced yet.

* test(relayconvert): fold unnamed stream tool cases into the hold test

The two standalone stream tests repeated what the nameless-tool hold
test already covers. Their distinct checks now live in its subtests:
a held function call without custom tools, its late call ID becoming
the item ID, and calls whose name never arrives leaving no event,
output index or sequence number behind, with and without custom tools.

* fix(relayconvert): keep Responses custom tools on Claude and Gemini upstreams

Responses requests sent to Claude Messages dropped Codex custom tools
such as exec and apply_patch as unsupported hosted tools, and Gemini
omitted their definitions and stripped custom_tool_call history before
conversion. A forced custom tool_choice was rejected on both targets,
and Claude parsed JSON-looking custom tool input from history.

Claude and Gemini now get the same treatment as Chat Completions: each
custom tool is sent as a function that takes one string input, with
the grammar kept in the description, and a forced custom tool_choice
becomes the matching named tool choice when that tool was sent. Gemini
declarations go through the usual parameter cleanup.

custom_tool_call history replays the raw input as {"input": ...} on
both targets, and its outputs pair with the call as tool results. The
custom tool selection is shared by all three targets and recorded for
each of them, so the existing Chat to Responses step restores
custom_tool_call items from Claude and Gemini responses as well.

* test(relayconvert): cover custom tool restore from Claude and Gemini responses

Claude and Gemini responses reach the Responses format through the
Chat to Responses step, but only the Chat source had a restore test.
The test is now table-driven over all three sources and checks the
non-stream output item and the exact streamed custom tool events.

* test(gemini): assert custom tools survive the Responses to Gemini adaptor path

---------

Co-authored-by: Kabuda <495696116@qq.com>
@Calcium-Ion
Calcium-Ion deleted the fix/responses-custom-tools-claude-gemini branch October 22, 2026 04:57
timwenx pushed a commit to timwenx/new-api that referenced this pull request Nov 18, 2026
…pstreams (QuantumNous#7636)

* fix(relayconvert): drop unnamed function calls from Responses output

A Chat Completions tool call without a function name cannot become a
valid Responses function_call item, so the non-stream conversion now
skips it instead of emitting an item with an empty name.

* fix(relayconvert): hold streamed tool calls until a name arrives

A streamed tool call is no longer announced while it has no function
name, whether or not custom tools are in play. A call whose name never
arrives is left out of the stream and the completed response. A call
ID that arrives after the first fragment becomes the item ID as long
as the item has not been announced yet.

* test(relayconvert): fold unnamed stream tool cases into the hold test

The two standalone stream tests repeated what the nameless-tool hold
test already covers. Their distinct checks now live in its subtests:
a held function call without custom tools, its late call ID becoming
the item ID, and calls whose name never arrives leaving no event,
output index or sequence number behind, with and without custom tools.

* fix(relayconvert): keep Responses custom tools on Claude and Gemini upstreams

Responses requests sent to Claude Messages dropped Codex custom tools
such as exec and apply_patch as unsupported hosted tools, and Gemini
omitted their definitions and stripped custom_tool_call history before
conversion. A forced custom tool_choice was rejected on both targets,
and Claude parsed JSON-looking custom tool input from history.

Claude and Gemini now get the same treatment as Chat Completions: each
custom tool is sent as a function that takes one string input, with
the grammar kept in the description, and a forced custom tool_choice
becomes the matching named tool choice when that tool was sent. Gemini
declarations go through the usual parameter cleanup.

custom_tool_call history replays the raw input as {"input": ...} on
both targets, and its outputs pair with the call as tool results. The
custom tool selection is shared by all three targets and recorded for
each of them, so the existing Chat to Responses step restores
custom_tool_call items from Claude and Gemini responses as well.

* test(relayconvert): cover custom tool restore from Claude and Gemini responses

Claude and Gemini responses reach the Responses format through the
Chat to Responses step, but only the Chat source had a restore test.
The test is now table-driven over all three sources and checks the
non-stream output item and the exact streamed custom tool events.

* test(gemini): assert custom tools survive the Responses to Gemini adaptor path

---------

Co-authored-by: Kabuda <495696116@qq.com>
xiaolanawaqaq pushed a commit to xiaolanawaqaq/new-apiaaa that referenced this pull request Nov 26, 2026
…pstreams (QuantumNous#7636)

* fix(relayconvert): drop unnamed function calls from Responses output

A Chat Completions tool call without a function name cannot become a
valid Responses function_call item, so the non-stream conversion now
skips it instead of emitting an item with an empty name.

* fix(relayconvert): hold streamed tool calls until a name arrives

A streamed tool call is no longer announced while it has no function
name, whether or not custom tools are in play. A call whose name never
arrives is left out of the stream and the completed response. A call
ID that arrives after the first fragment becomes the item ID as long
as the item has not been announced yet.

* test(relayconvert): fold unnamed stream tool cases into the hold test

The two standalone stream tests repeated what the nameless-tool hold
test already covers. Their distinct checks now live in its subtests:
a held function call without custom tools, its late call ID becoming
the item ID, and calls whose name never arrives leaving no event,
output index or sequence number behind, with and without custom tools.

* fix(relayconvert): keep Responses custom tools on Claude and Gemini upstreams

Responses requests sent to Claude Messages dropped Codex custom tools
such as exec and apply_patch as unsupported hosted tools, and Gemini
omitted their definitions and stripped custom_tool_call history before
conversion. A forced custom tool_choice was rejected on both targets,
and Claude parsed JSON-looking custom tool input from history.

Claude and Gemini now get the same treatment as Chat Completions: each
custom tool is sent as a function that takes one string input, with
the grammar kept in the description, and a forced custom tool_choice
becomes the matching named tool choice when that tool was sent. Gemini
declarations go through the usual parameter cleanup.

custom_tool_call history replays the raw input as {"input": ...} on
both targets, and its outputs pair with the call as tool results. The
custom tool selection is shared by all three targets and recorded for
each of them, so the existing Chat to Responses step restores
custom_tool_call items from Claude and Gemini responses as well.

* test(relayconvert): cover custom tool restore from Claude and Gemini responses

Claude and Gemini responses reach the Responses format through the
Chat to Responses step, but only the Chat source had a restore test.
The test is now table-driven over all three sources and checks the
non-stream output item and the exact streamed custom tool events.

* test(gemini): assert custom tools survive the Responses to Gemini adaptor path

---------

Co-authored-by: Kabuda <495696116@qq.com>
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.

fix: Responses conversion writes empty function_call.name into history, breaking session replay

2 participants