fix(deepseek): V4 reasoning_content consistency backfill#15476
Closed
whtoo wants to merge 1 commit into
Closed
Conversation
DeepSeek V4 requires all assistant messages to have reasoning_content once any turn has produced reasoning. Mixing (some with, some without) triggers HTTP 400 on DeepSeek reasoning models. Upstream recently added _copy_reasoning_content_for_api() and _needs_deepseek_tool_reasoning() for tool-call messages (NousResearch#15250). This commit extends that protection to ALL assistant messages: - Add _needs_reasoning_backfill(): detects when conversation history contains any assistant message with reasoning - Three API message build paths now backfill empty reasoning_content for assistant messages that lack reasoning when backfill is needed: - Main conversation loop - Memory flush path - _handle_max_iterations summary path - Complements upstream tool-call fixes by covering plain assistant acknowledgments and other non-tool turns - Legacy deepseek-reasoner and non-DeepSeek providers are unaffected - tests/agent/test_deepseek_v4_reasoning.py: 10 regression tests
d93fa4b to
3e4dbe4
Compare
Author
|
This PR has been superseded by subsequent commits on main. The reasoning_content consistency issue for DeepSeek V4 was resolved through a series of later PRs that landed on main:
These commits implemented a more general solution than the conditional backfill approach proposed here:
Additionally, main already carries (213 lines) which covers the same scenarios plus Kimi and custom-provider base_url edge cases. Because of the structural divergence ( removal + refactor), rebasing this PR would only introduce redundant code. Closing as obsolete. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
DeepSeek V4 models (
deepseek-v4-pro,deepseek-v4-flash) require all assistant messages in the conversation history to have areasoning_contentfield once any turn has produced reasoning. Mixing assistant messages with and withoutreasoning_contenttriggers HTTP 400 from the DeepSeek API.Relation to Recent Upstream Fixes
Commits
93a2d6b3andd58b305a(merged to main Apr 24–25) added_copy_reasoning_content_for_api()and_needs_deepseek_tool_reasoning()to protect tool-call assistant messages (#15250). This is excellent progress.However, the upstream fix only covers assistant messages that contain
tool_calls. Plain assistant turns (e.g., a simple acknowledgment like "Okay, let me check that") withouttool_callsare not backfilled. In a long conversation where:reasoning_contentreasoning_contentDeepSeek V4 still rejects with HTTP 400.
This PR
This PR complements the upstream tool-call fixes by extending protection to all assistant messages:
_needs_reasoning_backfill(): detects when any assistant message in the history has reasoning (provider-scoped, excludes legacydeepseek-reasoner)""reasoning_contentfor assistant messages that lack reasoning when backfill is needed:_handle_max_iterationssummary path_copy_reasoning_content_for_api()so we don't interfere with existing Kimi/DeepSeek tool-call logicTests
10 regression tests in
tests/agent/test_deepseek_v4_reasoning.py:_needs_reasoning_backfilldetectionreasoning_contentinjection across all three code pathsAll passing on latest main.