Skip to content

fix(provider): preserve reasoning_content when merging consecutive assistant messages#3419

Closed
chengyongru wants to merge 1 commit intomainfrom
fix/deepseek_reasoning
Closed

fix(provider): preserve reasoning_content when merging consecutive assistant messages#3419
chengyongru wants to merge 1 commit intomainfrom
fix/deepseek_reasoning

Conversation

@chengyongru
Copy link
Copy Markdown
Collaborator

Summary

  • Fix DeepSeek thinking mode 400 error caused by _enforce_role_alternation() silently dropping reasoning_content when merging consecutive assistant messages
  • Add _carry_over_reasoning() helper to preserve and concatenate reasoning_content across all 4 merge branches in _enforce_role_alternation()
  • Strip assistant-only fields (reasoning_content, thinking_blocks, tool_calls) when recovering trailing assistant messages as user role

Root Cause

DeepSeek thinking mode requires reasoning_content to be passed back in all subsequent API requests when tool calls were involved. The _enforce_role_alternation() method merged consecutive assistant messages by concatenating content but silently discarded reasoning_content, triggering:

Error: The reasoning_content in the thinking mode must be passed back to the API.

This could occur after history truncation (_snip_history) or checkpoint restoration created consecutive assistant messages.

Changes

  • nanobot/providers/base.py — New _carry_over_reasoning(src, dst) helper + integration into all merge branches + recovery path cleanup
  • tests/providers/test_enforce_role_alternation.py — 8 new test cases covering reasoning_content preservation

Test plan

  • All 8 new tests pass (reasoning carry-over in all merge scenarios)
  • All 167 existing provider/agent tests pass with no regression
  • Manual test with DeepSeek-R1/V4 thinking mode + tool calls in multi-turn conversation

…sistant messages

DeepSeek thinking mode requires reasoning_content to be passed back in
all subsequent API requests when tool calls were involved. The
_enforce_role_alternation method silently dropped reasoning_content
when merging consecutive assistant messages, causing a 400 error.
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.

1 participant