Skip to content

fix(relay): stop injecting empty tools into Claude requests that omit them - #6837

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
zcxads666:fix/6817-omit-empty-claude-tools
Aug 15, 2026
Merged

seefs001 merged 1 commit into
QuantumNous:mainfrom
zcxads666:fix/6817-omit-empty-claude-tools

Conversation

@zcxads666

@zcxads666 zcxads666 commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ 提交说明 / PR Notice源请求未携带 tools 时保持 Tools 为 nil,避免 omitempty 失效后序列化出 tools:[] 被上游拒绝。

⚠️ 提交说明 / PR Notice

Important

📝 变更描述 / Description

问题现象

当 OpenAI 格式的 Chat 请求经 new-api 转发到 Anthropic 兼容渠道时,如果客户端没有传 tools,转换后的 Claude 请求里却会出现一个空的 "tools": [] 字段。部分 Anthropic 兼容上游不接受空数组,会直接返回 400「tools 低于允许下限」。

同一个渠道上可以稳定复现出三种结果:

  • 直连、不带 tools:200 正常;
  • 直连、显式传 tools: []:400;
  • 经 new-api 的 OpenAI → Claude 转换(未传 tools):400。

根本原因

转换函数里用 make([]any, 0, len(tools)) 创建了非 nil 的空切片,然后无条件赋给 ClaudeRequest.Tools。虽然该字段的 json tag 带 omitempty,但它的类型是 any,Go 对非 nil 的 interface 值不会做省略,所以即使客户端根本没有传 tools,最终序列化时仍然会输出 "tools": []。

修复方案

只有当转换结果中确实存在 tool(function 或 web_search)时才给 ClaudeRequest.Tools 赋值;没有 tool 时保持 nil,让 omitempty 正常生效,请求体里不再出现 tools 字段。显式传入 tools 的请求行为不受影响。

与 #6698 的关系

#6698 已经让 endpoint_type=anthropic 的渠道测试走原生 Claude 请求,不再经过本次转换;但当 endpoint_type 为空时,测试仍会构建 GeneralOpenAIRequest,而业务侧 OpenAI Chat 打到 Claude 渠道走的也是同一个转换函数,空 tools: [] 依然会被注入。因此本 PR 直接在转换层修复,既能覆盖渠道测试的自动检测,也能修复正常转发路径。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
    • 说明:本 PR 由 AI 协助完成,描述与代码均经我逐行复核后提交,此项不勾选。
  • 非重复提交: 已搜索查看 Issue 与 PR,测试 Anthropic 渠道(type=14)时因空 tools:[] 导致 400 错误 #6817 开放且没有对应的修复 PR。fix: test Claude/Gemini endpoints with native request format #6698 只改了渠道测试的原生路径,不覆盖本次转换层。
  • Bug fix 说明: 已关联 测试 Anthropic 渠道(type=14)时因空 tools:[] 导致 400 错误 #6817。转换层把未提供的 tools 序列化成空数组。
  • 变更理解: 改动只影响 OpenAI Chat → Claude Messages 的 Tools 赋值:有 function / web_search 时仍下发 tools,没有时字段省略,显式传入的行为不变。
  • 范围聚焦: 只修改了 relaykit/relayconvert/internal/oai_chat/to_claude_messages_req.go,并在现有 claude_default_max_tokens_test.go 中增加回归测试。
  • 本地验证: 已运行下方列出的构建与测试,全部通过。
  • 安全合规: 无敏感凭据;符合项目规范。

📸 运行证明 / Proof of Work

  • 新增后端回归测试 TestOpenAIChatRequestToClaudeMessagesOmitsEmptyTools 通过:覆盖未传 tools、显式空数组 tools: []、function tool、仅 WebSearchOptions 四种输入,go test 全部 PASS。
  • 既有相关测试通过:TestClaudeDefaultMaxTokensPresence、TestClaudeThinkingAdapterSatisfiesMaxTokensWithoutCallback 均 PASS。
  • 格式检查通过:gofmt -l 对本次改动的两个文件无输出。
  • relaykit 模块校验通过:GOWORK=off go vet ./...、GOWORK=off go build ./... 无输出。
  • 根模块校验通过:GOWORK=off go vet ./...、GOWORK=off go build ./... 无输出。

Summary by CodeRabbit

  • Bug Fixes

    • Claude requests no longer include an empty tools field when no tools are configured.
    • Function and web-search tools continue to be included and serialized correctly when provided.
  • Tests

    • Added coverage for requests with absent, empty, and populated tool configurations.

@coderabbitai

coderabbitai Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 24cd64ee-cf0c-4095-8fc1-dc0ea809cd08

📥 Commits

Reviewing files that changed from the base of the PR and between 58d4e9b and b098b18.

📒 Files selected for processing (2)
  • relaykit/relayconvert/claude_default_max_tokens_test.go
  • relaykit/relayconvert/internal/oai_chat/to_claude_messages_req.go

Walkthrough

The Claude request converter now omits the top-level tools field when no tools are provided. Table-driven tests cover absent, empty, function, and web-search tools, including serialized request bodies.

Changes

Claude tool conversion

Layer / File(s) Summary
Conditionally serialize Claude tools
relaykit/relayconvert/internal/oai_chat/to_claude_messages_req.go, relaykit/relayconvert/claude_default_max_tokens_test.go
The converter assigns Tools only when Claude tools exist. Tests verify omission of empty tools and retention of function and web-search tools in serialized requests.

Estimated code review effort: 2 (Simple) | ~10 minutes

Mergeability Score: ⚪ Minimal · up to b098b

The fix prevents invalid empty tools fields while preserving explicitly supplied tools. Tests and builds are reported passing, and no actionable merge-blocking risk remains after normal checks.

Possibly related issues

Possibly related PRs

Poem

A rabbit checks the tools array,
Finds nothing there, and leaves it bare.
Function tools hop in line,
Web searches serialize fine.
No empty braces cause despair.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The conversion now omits Tools when no tools exist and preserves function and web-search tools, matching issue #6817.
Out of Scope Changes check ✅ Passed The changes are limited to the Claude conversion logic and focused regression tests for the linked issue.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main fix: preventing empty tools from being injected into Claude requests.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

源请求未携带 tools 时保持 Tools 为 nil,避免 omitempty 失效后序列化出 tools:[] 被上游拒绝。

Co-authored-by: Cursor <cursoragent@cursor.com>
@zcxads666
zcxads666 force-pushed the fix/6817-omit-empty-claude-tools branch from b098b18 to 34bed08 Compare August 14, 2026 02:07
@seefs001
seefs001 merged commit 4442bb3 into QuantumNous:main Aug 15, 2026
1 check 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.

测试 Anthropic 渠道(type=14)时因空 tools:[] 导致 400 错误

2 participants