Skip to content

fix(claude): omit absent tool schema required/properties instead of sending null - #6039

Closed
oierxjn wants to merge 5 commits into
QuantumNous:mainfrom
oierxjn:fix/claude-tool-schema-required-null
Closed

oierxjn wants to merge 5 commits into
QuantumNous:mainfrom
oierxjn:fix/claude-tool-schema-required-null

Conversation

@oierxjn

@oierxjn oierxjn commented Jul 9, 2026 •

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

chatCompletions 格式在转换 tools 中 required 这个键的时候加上空判断,防止类似 required: null 这种情况出现,从而导致请求失败

🚀 变更类型 / Type of change

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

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 Issues 与 PRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

修改后自己构建进行请求:
image

多轮不出现问题,而修改前仅需几轮就会报错

Summary by CodeRabbit

  • Bug Fixes

    • Improved tool schema conversion to preserve defined properties and required fields accurately.
    • Prevented empty required fields from appearing when no required properties are specified.
  • Tests

    • Added coverage for tool conversion with and without required fields.

@coderabbitai

coderabbitai Bot commented Jul 9, 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: acd55994-a179-4c2c-a1e9-5bcacb137555

📥 Commits

Reviewing files that changed from the base of the PR and between df60dc9 and d5e13d1.

📒 Files selected for processing (1)
  • relaykit/relayconvert/internal/oai_chat/to_claude_messages_req_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • relaykit/relayconvert/internal/oai_chat/to_claude_messages_req_test.go

Walkthrough

The change updates Claude tool schema conversion to omit required when the source schema does not provide it. A test verifies omission for absent fields and preservation for declared fields.

Changes

Claude tool schema fix

Layer / File(s) Summary
Conditional InputSchema field assignment and test
relaykit/relayconvert/internal/oai_chat/to_claude_messages_req.go, relaykit/relayconvert/internal/oai_chat/to_claude_messages_req_test.go
The converter copies properties and conditionally copies required. The test covers absent, present, and minimal schemas.

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

Possibly related PRs

Poem

A rabbit checks each schema field,
Empty required stays concealed.
Declared names remain in view,
Three tool cases test the queue.
Clean requests hop onward.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the primary change: omitting absent Claude tool schema fields instead of serializing them as null.
Linked Issues check ✅ Passed The implementation omits absent required and properties fields, preserves provided values, and adds tests for the reported schema validation issue [#6038].
Out of Scope Changes check ✅ Passed The changes are limited to Claude tool schema conversion and focused regression tests for the linked issue [#6038].
✨ 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.

@oierxjn

oierxjn commented Jul 11, 2026

Copy link
Copy Markdown
Author

有人来审一下吗?挺小的一个改动

@oierxjn
oierxjn force-pushed the fix/claude-tool-schema-required-null branch from 7f2931a to dfd42f9 Compare July 13, 2026 04:21

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
service/relayconvert/internal/oai_chat/to_claude_messages_req.go (1)

40-42: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Unguarded type assertion on params["type"] can panic.

params["type"].(string) will panic if type is present but not a string (e.g., a JSON Schema type union array). This is the same class of bug this PR is fixing for required/properties—an unsafe read of a dynamically-typed map value. Since tool parameter schemas are typically root-level "object" this is unlikely in practice, but a malformed/unexpected tool schema would crash the request instead of gracefully degrading.

🛡️ Proposed fix using comma-ok idiom
-			if params["type"] != nil {
-				claudeTool.InputSchema["type"] = params["type"].(string)
-			}
+			if typeVal, ok := params["type"].(string); ok {
+				claudeTool.InputSchema["type"] = typeVal
+			}

Since this involves a Go type-assertion/language-semantics concern, please confirm whether upstream validation guarantees type is always a string before reaching this function.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@service/relayconvert/internal/oai_chat/to_claude_messages_req.go` around
lines 40 - 42, Update the type handling in the tool-schema conversion logic to
use a guarded string type assertion for params["type"] instead of directly
asserting it. Only assign claudeTool.InputSchema["type"] when the value is
actually a string, allowing unexpected schema values such as arrays to degrade
gracefully without panicking; verify whether upstream validation guarantees this
type before relying on it.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@service/relayconvert/internal/oai_chat/to_claude_messages_req_test.go`:
- Around line 56-64: Replace the non-fatal value checks in the test with assert,
including the webSearchTools emptiness check and the InputSchema
presence/content checks for both tools. Keep require.Len for the
setup/index-safety assertion so subsequent tool-specific assertions remain
valid.

---

Nitpick comments:
In `@service/relayconvert/internal/oai_chat/to_claude_messages_req.go`:
- Around line 40-42: Update the type handling in the tool-schema conversion
logic to use a guarded string type assertion for params["type"] instead of
directly asserting it. Only assign claudeTool.InputSchema["type"] when the value
is actually a string, allowing unexpected schema values such as arrays to
degrade gracefully without panicking; verify whether upstream validation
guarantees this type before relying on it.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a08233b7-ad9c-4679-b993-580232b36ba4

📥 Commits

Reviewing files that changed from the base of the PR and between 7f2931a and dfd42f9.

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

@oierxjn

oierxjn commented Jul 13, 2026

Copy link
Copy Markdown
Author

注意到main分支对相关文件进行重构,已同步

JacksonsY added a commit to JacksonsY/new-api that referenced this pull request Jul 19, 2026
移植自上游 PR QuantumNous#6039。OpenAI→Claude 工具转换无条件拷贝
params["required"]/params["properties"],源 schema 未声明该键时
map 取值为 nil,序列化成 "required": null 被 Claude 上游按
`null is not of type "array"` 拒绝(Codex 的 list_mcp_resources
等无必填参数工具全部 400)。改为仅在键存在时拷贝,并补回归测试。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@oierxjn oierxjn changed the title Fix/claude tool schema required null fix(claude): omit absent tool schema required/properties instead of sending null Jul 28, 2026
oierxjn added 3 commits August 6, 2026 09:52
OpenAIChatRequestToClaudeMessages copied `properties`/`required` from the
source OpenAI tool schema into the Anthropic input_schema map unconditionally.
When a function omits `required` (valid for a tool with no required params,
e.g. list_mcp_resources), the missing map key reads back as nil, gets stored
explicitly, and marshals to `"required": null`. Upstreams that validate the
schema reject it with `null is not of type "array"` (HTTP 400).

Guard both assignments with a nil check, matching the existing `type` handling
above, so absent keys stay absent.
Add TestOpenAIChatRequestToClaudeMessagesOmitsAbsentToolRequired: a tool that
omits `required` must not serialize it (it previously became "required": null),
while a tool that declares `required` keeps it intact.
@oierxjn
oierxjn force-pushed the fix/claude-tool-schema-required-null branch from 86266ee to df60dc9 Compare August 6, 2026 01:53
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
relaykit/relayconvert/internal/oai_chat/to_claude_messages_req_test.go (1)

18-48: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add coverage for omitted properties.

Both test schemas include properties. The test does not protect the omission branch added in the converter. Add a schema without properties and assert that its output InputSchema does not contain that key.

Also applies to: 60-65

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@relaykit/relayconvert/internal/oai_chat/to_claude_messages_req_test.go`
around lines 18 - 48, Add a tool schema case without a properties field in the
test fixtures for the converter, then assert the converted tool’s InputSchema
omits the properties key. Keep the existing schemas and assertions intact, and
target the test covering the conversion of Tools into Claude messages.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@relaykit/relayconvert/internal/oai_chat/to_claude_messages_req_test.go`:
- Around line 6-7: Update the test invoking OpenAIChatRequestToClaudeMessages to
pass context.Context, convmeta.Meta, and relaykit/dto.GeneralOpenAIRequest, and
replace the root dto import with github.com/QuantumNous/new-api/relaykit/dto.
Verify with GOWORK=off go build ./... and go test
./relayconvert/internal/oai_chat from relaykit.

---

Nitpick comments:
In `@relaykit/relayconvert/internal/oai_chat/to_claude_messages_req_test.go`:
- Around line 18-48: Add a tool schema case without a properties field in the
test fixtures for the converter, then assert the converted tool’s InputSchema
omits the properties key. Keep the existing schemas and assertions intact, and
target the test covering the conversion of Tools into Claude messages.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b8e42d55-297a-4934-891f-907ca457f501

📥 Commits

Reviewing files that changed from the base of the PR and between 0ab0202 and df60dc9.

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

oierxjn added 2 commits August 6, 2026 10:41
Import relaykit-local dto/convmeta instead of the root module so relaykit
stays independently buildable, pass convmeta.Meta to the converter, and set
max_tokens which the current converter requires.
@oierxjn

oierxjn commented Aug 6, 2026

Copy link
Copy Markdown
Author

@coderabbitai resume

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Reviews resumed.

@oierxjn

oierxjn commented Aug 19, 2026

Copy link
Copy Markdown
Author

Fixed in #6862

维护者看起来还挺忙的(

@oierxjn oierxjn closed this Aug 19, 2026
@oierxjn
oierxjn deleted the fix/claude-tool-schema-required-null branch August 19, 2026 02:49
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.

chatCompletions 格式转 Anthropic 格式行为不符合预期

1 participant