Skip to content

fix(claude): 保留 thinking signature 以修复 DeepSeek 多轮对话 400 - #6998

Open
hrhcode wants to merge 1 commit into
QuantumNous:mainfrom
hrhcode:fix-responses-deepseek-reasoning-content
Open

hrhcode wants to merge 1 commit into
QuantumNous:mainfrom
hrhcode:fix-responses-deepseek-reasoning-content

Conversation

@hrhcode

@hrhcode hrhcode commented Aug 24, 2026 •

Copy link
Copy Markdown

Claude 适配器在 Anthropic 响应转 OpenAI 格式时丢弃了 thinking 块的 signature,并在构建 Claude 请求时忽略 reasoning_content,导致需要回传 完整 thinking 块(文本 + signature)的上游(如 DeepSeek Anthropic 渠道) 在多轮对话时报 HTTP 400 "thinking must be passed back"。

三处修复:

  • 流式响应:signature_delta 编码为 {"_sig":""} 写入 reasoning_content, 不再用换行符替换丢弃
  • 非流式响应:thinking 文本末尾追加同样的 signature 标记
  • 请求路径:assistant 消息的 reasoning_content 还原为完整的 {type:"thinking", thinking, signature} 块后再发送上游

无 thinking 的普通对话不受影响,新逻辑仅在 reasoning_content 非空时触发。

⚠️ 提交说明 / PR Notice

Important

我们公司的模型网关也基于new-api搭建,在使用过程中发现的这个bug,具体触发场景是网关配置了anthropic渠道的deepseek模型,用户本地使用codex + ccswitch采用openai的格式请求网关,会发生偶发的“ HTTP 400; cause: The request is invalid: The content[].thinking in the thinking mode must be passed back to the API.. Please check the request body, required fields, and request format.”这种报错。

经过排查发现原因其实很简单,deepSeek 要求开启 thinking 模式后,上轮产生的 thinking 内容块(含加密签名)必须在下轮请求中原样传回。但网关在 claude↔openAI 格式转换时, 响应路径丢弃了签名 , 请求路径忽略了 reasoning_content ,导致 thinking 块在多轮对话中永久丢失,上游返回 400。

📝 变更描述 / Description

  • 请求侧:在 OpenAI Chat 转 Claude 消息时,将 assistant 消息的 reasoning_content 还原为 {"type":"thinking","thinking":"..."} 内容块,并保证流式消息合并正确。
  • 响应侧:保留 thinking 块的 signature,并定义可随响应透传、下一轮可读的承载方式,避免信息在首轮就丢失。
  • 兼容性:仅在 thinking 模式开启且上游需要时插入 thinking 块,避免影响普通模型和历史兼容。

🚀 变更类型 / 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
报错:CC Switch local proxy failed while handling Codex endpoint /responses. Provider: deepseek v4; model: deepseek-v4-pro[1m]; upstream_status: HTTP 400; cause: The request is invalid: The content[].thinking in the thinking mode must be passed back to the API.. Please check the request body, required fields, and request format.

修改后测试:
image
运行正常

Summary by CodeRabbit

  • New Features

    • Preserved assistant reasoning content when converting between chat message formats.
    • Converted reasoning into Claude thinking blocks, including optional signature metadata.
    • Included signature metadata in streamed and non-streamed reasoning responses.
  • Bug Fixes

    • Improved preservation of reasoning and thinking details across message conversions.

Claude 适配器在 Anthropic 响应转 OpenAI 格式时丢弃了 thinking 块的
signature,并在构建 Claude 请求时忽略 reasoning_content,导致需要回传
完整 thinking 块(文本 + signature)的上游(如 DeepSeek Anthropic 渠道)
在多轮对话时报 HTTP 400 "thinking must be passed back"。

三处修复:
- 流式响应:signature_delta 编码为 {"_sig":"<sig>"} 写入 reasoning_content,
  不再用换行符替换丢弃
- 非流式响应:thinking 文本末尾追加同样的 signature 标记
- 请求路径:assistant 消息的 reasoning_content 还原为完整的
  {type:"thinking", thinking, signature} 块后再发送上游

无 thinking 的普通对话不受影响,新逻辑仅在 reasoning_content 非空时触发。
@coderabbitai

coderabbitai Bot commented Aug 24, 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: ac5fe057-5c56-4f67-b4b1-d0af517b6ab7

📥 Commits

Reviewing files that changed from the base of the PR and between 2d8e50b and f11dee4.

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

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


Walkthrough

The conversion paths now preserve assistant reasoning content and Claude signatures. Claude signatures use JSON _sig markers in OpenAI reasoning content. OpenAI assistant reasoning content converts into Claude thinking blocks before text or tool-use blocks.

Changes

Reasoning preservation

Layer / File(s) Summary
Emit reasoning signatures
relaykit/relayconvert/internal/claude_messages/to_oai_chat_resp.go
Streaming and non-streaming Claude responses append available signatures as JSON _sig markers.
Reconstruct Claude thinking blocks
relaykit/relayconvert/internal/oai_chat/to_claude_messages_req.go
Intermediate messages retain reasoning fields. Assistant reasoning becomes a Claude thinking block, with an optional extracted signature placed before text or tool-use blocks.

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

Merge Risk: ⚪ Minimal · up to f11de

This is a localized request-conversion fix with no actionable merge-blocking risk remaining; the standalone relaykit build should be completed as a normal validation step.

Sequence Diagram(s)

sequenceDiagram
  participant ClaudeResponse
  participant OpenAIChatResponse
  participant OpenAIAssistantMessage
  participant ClaudeRequest
  ClaudeResponse->>OpenAIChatResponse: append JSON _sig marker
  OpenAIChatResponse->>OpenAIAssistantMessage: preserve reasoning content
  OpenAIAssistantMessage->>ClaudeRequest: create thinking block with signature
  ClaudeRequest->>ClaudeRequest: place thinking before text or tool use
Loading

Poem

A rabbit watched the thoughts go by,
With _sig stars across the sky.
Reasoning hopped from frame to frame,
Then thinking blocks arrived the same.
Tools found the trail and followed through.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed 变更保留 reasoning_content,并在 Claude thinking 块中恢复 signature,满足 Issue #6939 对思考模式工具调用多轮请求的核心要求。
Out of Scope Changes check ✅ Passed 所有变更均围绕 reasoning_content 和 thinking signature 的跨格式保留,未发现与 Issue #6939 无关的代码修改。
Docstring Coverage ✅ Passed Docstring check was indeterminate for this PR — some files could not be analyzed in time. Not blocking.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了保留 Claude thinking signature 并修复 DeepSeek 多轮对话 HTTP 400 的主要变更。
✨ 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.

This branch has not been deployed

No deployments
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.

调用deepseek-v4-flash会报错:400 The 'reasoning_content’ in the thinking mode must be passed back to the API.

1 participant