Skip to content

fix(deepseek): 修复工具调用时缺失 reasoning_content 导致的 400 错误 - #7153

Closed
ID-VerNe wants to merge 1 commit into
QuantumNous:mainfrom
ID-VerNe:deepseek-backfill-only
Closed

ID-VerNe wants to merge 1 commit into
QuantumNous:mainfrom
ID-VerNe:deepseek-backfill-only

Conversation

@ID-VerNe

@ID-VerNe ID-VerNe commented Sep 2, 2026 •

Copy link
Copy Markdown

Agent

  • Tool: TRAE (TraeCode)
  • Tool version: latest
  • Model (full id): DeepSeek-V4-Flash 正式版
  • Host (CLI / IDE / GitHub coding agent / other): IDE
  • Date (UTC): 2026-09-01

Links

User request

API Error: 400 If thinking mode and tool_calls, reasoning_content must be passed back to the API. 这是为什么?Claude code 经常触发这个问题。

(随后提供了自行调研的社区资料:DeepSeek 在思考模式下强制要求带 tool_calls 的 assistant 轮次原样回传 reasoning_content,而客户端或中转常在重组历史时裁剪该字段;社区方案包括让网关在收到 tool 结果回传时,检查上一条 assistant 消息——若含 tool_calls 但 reasoning_content 被删,强制注入空字符串,"DeepSeek 接受空字符串 reasoning_content 作为回传,只要该字段存在即可规避 400 报错"。)

用的是 new api,接的 DeepSeek(腾讯云接口),你看看社区里面有没有提到这个问题……看看这个问题怎么解决?

Out of scope — refuse

  • Matched: no

Kind

  • Bug fix

Issue facts

Change

在 relay/channel/deepseek/adaptor.go 的 ConvertOpenAIRequest 出口处新增 ensureToolCallReasoningContent:

  1. 遍历 request.Messages;
  2. 凡 role == "assistant" 且携带 tool_calls 而 reasoning_content 为 nil 的消息,补全为空字符串 "";
  3. 已有非空值原样保留,user / tool 等其他角色的消息不受影响。

为什么补空字符串? 上游校验仅要求字段存在(接受空串),补空串既能通过校验,又不伪造思考内容、不增加 Token 计费。

为什么只在 DeepSeek 适配器改? 影响面最小。通用转换层的映射(#6593 方向)无法覆盖字段已被客户端裁剪的场景——此时历史里已无 thinking 可映射,渠道出口是唯一有效层;且 DeepSeek 渠道专属兜底不触碰通用转换,与其他供应商零相互影响。

Research

Duplicate / prior art

Docs and code

Alternatives considered

Files

Path Why
relay/channel/deepseek/adaptor.go ensureToolCallReasoningContent 兜底补全
relay/channel/deepseek/adaptor_test.go 5 个单测:补全缺失 / 保留非空值 / 无 tool_calls 不改写 / user·tool 不受影响 / 混合消息

Behavior

Verification

Risks

  • Failure modes: 仅 DeepSeek 渠道生效;若上游未来要求非空 reasoning_content,需改为注入占位文本(上游目前接受空串)。
  • Billing / quota / auth impact: 无——空字符串不产生额外 Token,不触碰计费路径。
  • Follow-ups: 若维护者后续接受通用转换方案,本兜底与之互补。

Scope check

  • Single focused change: yes
  • Secrets included: no
  • Out of scope (Coding Plan / reverse-engineered channel / third-party wrapper / Codex): no

Note: 本 PR 的问题定位、代码实现与单测由 AI 辅助完成,提交者已审阅全部内容并对其准确性负责,在真实的腾讯云 DeepSeek 接口上通过了 15 小时 / 约 20,000 次调用的实测验证。

Summary by CodeRabbit

  • Bug Fixes

    • Fixed DeepSeek requests with assistant tool calls being rejected when reasoning content was missing.
    • Missing reasoning content is now supplied automatically, while existing values remain unchanged.
    • Messages without tool calls are unaffected.
  • Tests

    • Added coverage for tool-call requests, preserved reasoning content, and unaffected messages.

…he DeepSeek channel

DeepSeek 系上游(官方及兼容接口)开启思考模式时,历史中携带 tool_calls 的
assistant 轮次必须回传 reasoning_content,否则上游返回 HTTP 400 "reasoning_content
must be passed back to the API"(QuantumNous#4543 / QuantumNous#6939 / QuantumNous#4408)。即使客户端(如
Cursor、上下文压缩后的重放)或中间路由裁剪掉了该字段,只要经过 DeepSeek 渠道
就会触发。

FIX:
- DeepSeek 适配器 ConvertOpenAIRequest 出口处补全:凡带 tool_calls 且缺
  reasoning_content 的 assistant 消息补空字符串,已有非空值原样保留,user/tool
  消息不受影响

ADD:
- relay/channel/deepseek/adaptor_test.go:5 个单测覆盖补全/保留/不误伤场景

仅在 DeepSeek 渠道类型内生效,不触碰通用格式转换层。
@coderabbitai

coderabbitai Bot commented Sep 2, 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: Team

Run ID: 5d8cf049-bed7-41b1-b358-0132b09b4aa7

📥 Commits

Reviewing files that changed from the base of the PR and between 0ed497f and ea028e9.

📒 Files selected for processing (2)
  • relay/channel/deepseek/adaptor.go
  • relay/channel/deepseek/adaptor_test.go

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


Walkthrough

The DeepSeek adapter now ensures assistant messages with tool calls include reasoning_content. Existing values remain unchanged. New table-driven tests cover missing, existing, and unrelated message values.

Changes

DeepSeek reasoning content

Layer / File(s) Summary
Preserve tool-call reasoning content
relay/channel/deepseek/adaptor.go, relay/channel/deepseek/adaptor_test.go
The adapter sets missing reasoning_content to an empty string for assistant messages with tool calls. Existing values and unrelated messages remain unchanged. Tests cover all cases.

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

Merge Risk: ⚪ Minimal · up to ea028

This localized fix restores the required reasoning field only for DeepSeek assistant tool-call requests and preserves existing values, preventing the reported 400 errors without changing permissions, deployment scope, or other providers. No actionable merge-blocking risk remains beyond normal checks and review.

Suggested reviewers: seefs001

Poem

A rabbit reads each line,
The patch grows clear beneath the moon,
Small changes hop in place,
Tests guard the garden path,
Reviews bloom before the dawn.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. 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 identifies the DeepSeek tool-call fix and the missing reasoning_content error.
Linked Issues check ✅ Passed The changes address issue #6939 by preserving existing reasoning_content and adding an empty value for assistant messages with tool_calls when it is missing. The tests cover the required regression sc…
Out of Scope Changes check ✅ Passed The changes are limited to the DeepSeek channel adapter and its unit tests. They match the linked issue and PR objective without unrelated database, UI, or common conversion changes.
Full details: Linked Issues check

Explanation

The changes address issue #6939 by preserving existing reasoning_content and adding an empty value for assistant messages with tool_calls when it is missing. The tests cover the required regression scenarios.

  • Fix all pre-merge checks with AI
✨ 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.

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.

2 participants