Skip to content

fix(formatter): preserve reasoning_content for tool-call segments in …#1323

Merged
LearningGp merged 1 commit into
agentscope-ai:mainfrom
Mrjyw:fix/deepseek-thinking-toolcall-reasoning
May 21, 2026
Merged

fix(formatter): preserve reasoning_content for tool-call segments in …#1323
LearningGp merged 1 commit into
agentscope-ai:mainfrom
Mrjyw:fix/deepseek-thinking-toolcall-reasoning

Conversation

@Mrjyw

@Mrjyw Mrjyw commented May 2, 2026

Copy link
Copy Markdown
Contributor

When thinking mode is enabled and a user turn contains tool calls,
all assistant messages in that turn must preserve their
reasoning_content when sent back to the DeepSeek API in subsequent
requests. Previously, reasoning_content was only kept for the
current turn, causing HTTP 400 errors in multi-turn tool-calling
scenarios.

See: https://api-docs.deepseek.com/guides/thinking_mode#tool-calls
Fixes #1281

AgentScope-Java Version

1.0.12-SNAPSHOT

Description

Background: DeepSeek's thinking mode requires reasoning_content to be preserved for all
assistant messages within a user turn that contains tool calls. The original code only preserved it
for the current turn, causing HTTP 400 errors in multi-turn tool-calling scenarios.

Changes:

  • applyDeepSeekFixes(): Added segment-level tool call detection via computeSegmentToolFlags(). A
    segment is defined as messages between two consecutive user messages. If any message in a segment
    contains tool calls, all assistant messages in that segment are flagged to preserve their
    reasoning_content.
  • fixMessage(): Renamed parameter from isCurrentTurn to needReasoning to reflect that
    preservation is now determined by the segment analysis, not just the turn boundary.
  • Added ReasoningPreservationTests nested test class covering key scenarios: non-thinking mode
    fallback, multiple rounds with tool calls, mixed text/tool rounds, text-only messages in tool-call
    segments, and regression for text-only segments.

Checklist

  • 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
  • Code is ready for review

…DeepSeek thinking mode

When thinking mode is enabled and a user turn contains tool calls,
all assistant messages in that turn must preserve their
reasoning_content when sent back to the DeepSeek API in subsequent
requests. Previously, reasoning_content was only kept for the
current turn, causing HTTP 400 errors in multi-turn tool-calling
scenarios.

See: https://api-docs.deepseek.com/guides/thinking_mode#tool-calls
Fixes agentscope-ai#1281

1.0.12-SNAPSHOT

**Background:** DeepSeek's thinking mode requires `reasoning_content` to be preserved for all
assistant messages within a user turn that contains tool calls. The original code only preserved it
for the current turn, causing HTTP 400 errors in multi-turn tool-calling scenarios.

**Changes:**

- `applyDeepSeekFixes()`: Added segment-level tool call detection via `computeSegmentToolFlags()`. A
segment is defined as messages between two consecutive user messages. If any message in a segment
contains tool calls, all assistant messages in that segment are flagged to preserve their
`reasoning_content`.
- `fixMessage()`: Renamed parameter from `isCurrentTurn` to `needReasoning` to reflect that
preservation is now determined by the segment analysis, not just the turn boundary.
- Added `ReasoningPreservationTests` nested test class covering key scenarios: non-thinking mode
fallback, multiple rounds with tool calls, mixed text/tool rounds, text-only messages in tool-call
segments, and regression for text-only segments.

- [x] Code has been formatted with `mvn spotless:apply`
- [x] All tests are passing (`mvn test`)
- [x] Javadoc comments are complete and follow project conventions
- [ ] Related documentation has been updated
- [x] Code is ready for review
@Mrjyw Mrjyw requested a review from a team May 2, 2026 08:05
@CLAassistant

CLAassistant commented May 2, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@Mrjyw

Mrjyw commented May 2, 2026 via email

Copy link
Copy Markdown
Contributor Author

@codecov

codecov Bot commented May 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.95652% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...scope/core/formatter/openai/DeepSeekFormatter.java 86.95% 0 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

@Mrjyw

Mrjyw commented May 14, 2026

Copy link
Copy Markdown
Contributor Author

@LearningGp PTAL — could you help request a review from the team? All checks have passed. Thanks!

@LearningGp LearningGp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@LearningGp LearningGp merged commit 4f16706 into agentscope-ai:main May 21, 2026
5 checks passed
liangxingguang pushed a commit to liangxingguang/agentscope-java that referenced this pull request May 21, 2026
agentscope-ai#1323)

When thinking mode is enabled and a user turn contains tool calls,
all assistant messages in that turn must preserve their
reasoning_content when sent back to the DeepSeek API in subsequent
requests. Previously, reasoning_content was only kept for the
current turn, causing HTTP 400 errors in multi-turn tool-calling
scenarios.

See: https://api-docs.deepseek.com/guides/thinking_mode#tool-calls
Fixes agentscope-ai#1281

## AgentScope-Java Version

1.0.12-SNAPSHOT

## Description

**Background:** DeepSeek's thinking mode requires `reasoning_content` to
be preserved for all
assistant messages within a user turn that contains tool calls. The
original code only preserved it
for the current turn, causing HTTP 400 errors in multi-turn tool-calling
scenarios.

**Changes:**

- `applyDeepSeekFixes()`: Added segment-level tool call detection via
`computeSegmentToolFlags()`. A
segment is defined as messages between two consecutive user messages. If
any message in a segment
contains tool calls, all assistant messages in that segment are flagged
to preserve their
`reasoning_content`.
- `fixMessage()`: Renamed parameter from `isCurrentTurn` to
`needReasoning` to reflect that
preservation is now determined by the segment analysis, not just the
turn boundary.
- Added `ReasoningPreservationTests` nested test class covering key
scenarios: non-thinking mode
fallback, multiple rounds with tool calls, mixed text/tool rounds,
text-only messages in tool-call
segments, and regression for text-only segments.

## Checklist

- [x] Code has been formatted with `mvn spotless:apply`
- [x] All tests are passing (`mvn test`)
- [x] Javadoc comments are complete and follow project conventions
- [ ] Related documentation has been updated
- [x] Code is ready for review
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]:DeepSeek thinking mode + 工具调用场景下,第二轮对话必然触发 HTTP 400

3 participants