Skip to content

fix(deepseek): pass reasoning_content back for all assistant turns - #3272

Merged
jujn merged 2 commits into
agentscope-ai:mainfrom
amyaxy:fix/deepseek-reasoning-passback
Sep 29, 2026
Merged

jujn merged 2 commits into
agentscope-ai:mainfrom
amyaxy:fix/deepseek-reasoning-passback

Conversation

@amyaxy

@amyaxy amyaxy commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.0.4-SNAPSHOT

Description

Fixes #3246

Summary

DeepSeek's thinking mode requires reasoning_content to be fully passed back for every assistant turn when the request carries tools, but the framework produced HTTP 400 ("The reasoning_content in the thinking mode must be passed back to the API") in everyday scenarios: plain text assistant turns lost their reasoning, and framework-synthesized assistant messages (e.g. subagent completion notifications) never carry any.

Root cause

Two code paths violated the DeepSeek thinking-mode contract:

  1. Formatter stripped reasoning from non-tool turns. DeepSeekFormatter (both the compat and the deprecated variants) only kept reasoning_content on assistant messages containing tool calls, silently dropping it from plain text turns — the next tool-carrying request then violated the pass-back rule.
  2. Framework-synthesized assistant messages carry no reasoning at all. Subagent completion notifications are injected as assistant messages with only a name field and text content; having never carried a ThinkingBlock, they serialize without reasoning_content.

Both produce a request whose history misses reasoning_content on assistant turns while tools is present → HTTP 400. Additionally, the fixes only lived on the one-arg applyDeepSeekFixes(List) overload, while production traffic goes through the two-arg doFormat(List, GenerateOptions) path — so the existing one-arg fixes were never applied in production at all.

Changes

  • applyDeepSeekFixes never strips reasoning_content and backfills an empty string for assistant messages that lack it, in both the compat and the deprecated DeepSeekFormatter and DeepSeekMultiAgentFormatter; both formatters now override the two-arg doFormat(List, GenerateOptions) production path.
  • Thinking mode is decided by the request's thinking option ({"type": "enabled"|"disabled"} in the additional body params, as encoded by DeepSeekModelProvider). When the option is absent the formatter assumes thinking is enabled (DeepSeek's server-side default), so missing traces are backfilled anyway; an explicit disabled opts out. The previous history-scan fallback is removed — it could not detect the reported scenario (no thinking option and no reasoning_content in history).
  • The one-arg applyDeepSeekFixes overload had no production caller and is removed; the deprecated formatter additionally keeps its legacy name-field removal.
  • Fixes mutate the freshly converted message list in place; the contract is documented on both formatters' javadoc, and docs/v2/en|zh/integration/model/deepseek.md describe the new thinking-option semantics.

How to test

No Docker/network required:

mvn -pl agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-openai test
  • DeepSeekReasoningContentPassbackTest (new): the reported scenario plus two-arg production-path, thinking-option and in-place-mutation tests.
  • Existing formatter tests updated for the two-arg signature and the thinking-option semantics.

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

DeepSeek's thinking mode requires reasoning_content to be fully passed back
for every assistant turn when the request carries tools, but the formatter
used to strip it from turns without tool calls, and framework-synthesized
assistant messages (e.g. subagent completion notifications) never carry a
ThinkingBlock at all - both producing HTTP 400 ("The reasoning_content in
the thinking mode must be passed back to the API").

- applyDeepSeekFixes never strips reasoning_content and backfills an empty
  string for assistant messages that lack it, in both the compat and the
  deprecated DeepSeekFormatter and DeepSeekMultiAgentFormatter
- thinking mode is decided by the request's thinking option
  ({"type": "enabled"|"disabled"}); when the option is absent the formatter
  assumes thinking is enabled (DeepSeek's server-side default), so missing
  traces are backfilled anyway, and an explicit "disabled" opts out
- fixes run on the two-arg doFormat(List, GenerateOptions) production path;
  the one-arg applyDeepSeekFixes overload had no production caller and is
  removed; the deprecated formatter additionally keeps its legacy
  name-field removal
- fixes mutate the freshly converted message list in place; the contract is
  documented on both formatters, and en/zh deepseek.md describe the new
  thinking-option semantics
- add DeepSeekReasoningContentPassbackTest covering the reported scenario
  plus two-arg production-path, thinking-option and in-place-mutation tests
  (593 module tests green, DeepSeek formatter classes at 100% line/branch
  coverage)

Fixes agentscope-ai#3246
@codecov

codecov Bot commented Sep 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Summary

Backfills reasoning_content on all assistant turns (including tool-call and framework-synthesized sub-agent turns) in both the formatter/ and compat/deepseek/ DeepSeek formatters, replaces the old "keep reasoning only for segments with tool calls" rule, and documents the thinking-mode detection and the in-place mutation contract. The behaviour change is deliberate and the API contract (HTTP 400 unless reasoning is passed back for tool-carrying requests) is quoted in the javadoc, so this fixes a real failure mode rather than masking one.

I read the two copies side by side at head: the reasoning backfill is present in both, the "unset option ⇒ assume thinking on" default and the caller-owns-the-list mutation contract are both documented where I would have asked about them, and the new DeepSeekReasoningContentPassbackTest plus the added cases in the four existing formatter tests cover backfill, explicit opt-out, serialization and the two-arg formatting path. CLA is signed (license/cla=success on 4037595c), and ~1,090 of the +1,375 lines are tests/docs. Approved.

Findings

  • [Warning] compat/deepseek/DeepSeekFormatter.java:131 — the name-stripping rule applied by the non-compat copy is absent here; the fix landed on one copy only. Worth a parity note or test.

Cross-repo note

None required: the change is confined to agentscope-extensions-model-openai, and compat/deepseek keeps its existing public surface.


Automated review by github-manager-bot

}
return result;
for (OpenAIMessage msg : messages) {
fixMessage(msg);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[Warning] Verified at head: this compat/deepseek copy only backfills reasoning_content, while formatter/DeepSeekFormatter.fixMessage(msg, thinkingMode) also does an unconditional msg.setName(null) ("Legacy rule: drop the name field (DeepSeek returns HTTP 400 when it is present)"). If that 400 rule holds for the wire format, it holds for this copy too — either the compat path has a latent name-field bug, or the asymmetry is intentional and is worth one line of comment plus a parity test asserting both copies emit the same shape. Not blocking this PR (the name handling here is pre-existing and untouched by your change), just flagging that the fix landed on one copy only.

@jujn
jujn merged commit 9343ff0 into agentscope-ai:main Sep 29, 2026
7 checks passed
chickenlj pushed a commit that referenced this pull request Nov 13, 2026
…3272)

Backport the verified fix to the 2.0.x implementation and retain its
existing public contracts where the upstream kernel differs.

(cherry picked from commit 9343ff0)
Constraint: No AgentScope Service or SDK migration
Confidence: high
Not-tested: Whole-repository verification pending after lane integration
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.

[Bug]: openAI request error : [The reasoning_content in the thinking mode must be passed back to the API.]

3 participants