Skip to content

fix: preserve DeepSeek thinking blocks on Anthropic replay (#16748)#16781

Closed
vominh1919 wants to merge 1 commit into
NousResearch:mainfrom
vominh1919:fix/deepseek-thinking-blocks-replay
Closed

fix: preserve DeepSeek thinking blocks on Anthropic replay (#16748)#16781
vominh1919 wants to merge 1 commit into
NousResearch:mainfrom
vominh1919:fix/deepseek-thinking-blocks-replay

Conversation

@vominh1919

Copy link
Copy Markdown
Contributor

Summary

When using DeepSeek via their Anthropic Messages-compatible endpoint (api.deepseek.com/anthropic) with thinking-capable models, Hermes strips ALL thinking blocks from assistant history on replay, causing DeepSeek to return HTTP 400 because it expects thinking context from prior turns.

Root Cause

_is_third_party_anthropic_endpoint() returns True for api.deepseek.com (it's not anthropic.com), causing the thinking-block stripping logic to remove all thinking/redacted_thinking blocks unconditionally. DeepSeek's endpoint, unlike Azure/Bedrock/MiniMax, requires these blocks for reasoning continuity.

Fix

Added _is_deepseek_anthropic_endpoint() detector and a dedicated branch in the thinking-block processing logic that:

  • Strips signed Anthropic blocks (DeepSeek can't validate signatures)
  • Preserves unsigned thinking blocks synthesised from reasoning_content

This follows the exact same pattern as the Kimi exemption (added via issue #13848) and does not change behavior for any other third-party endpoint.

File changed: agent/anthropic_adapter.py (+29 lines)

Key Code Changes

  1. New detector function:
def _is_deepseek_anthropic_endpoint(base_url):
    return "api.deepseek.com" in normalized
  1. New branch in thinking-block logic (after Kimi, before generic third-party):
elif _is_deepseek:
    # Strip signed blocks, preserve unsigned ones for replay

Safety

  • Kimi and DeepSeek are the only third-party endpoints that get special treatment
  • Azure, Bedrock, MiniMax, and all other third-party endpoints are unaffected
  • Direct Anthropic API behavior is completely unchanged

Fixes #16748

…rch#16748)

DeepSeek's /anthropic endpoint requires thinking blocks to be replayed
in multi-turn conversations for reasoning continuity. The existing code
classified api.deepseek.com as a generic third-party endpoint and stripped
ALL thinking blocks, causing HTTP 400 from DeepSeek.

Fix: add _is_deepseek_anthropic_endpoint() detector (following the Kimi
precedent) and a dedicated branch that strips only signed Anthropic blocks
while preserving unsigned ones synthesised from reasoning_content.

This follows the exact same pattern as the Kimi exemption (issue NousResearch#13848)
and does not change behavior for any other third-party endpoint (Azure,
Bedrock, MiniMax, etc.).

Fixes NousResearch#16748
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent loop, run_agent.py, prompt builder provider/deepseek DeepSeek API labels Apr 28, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #16149 — same fix: preserve DeepSeek thinking blocks on Anthropic replay in anthropic_adapter.py.

@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #17510 — your commit was cherry-picked onto current main with authorship preserved via rebase-merge (commit fd5479a). Thanks for the fix! See #17510 for the salvaged change + regression test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder P2 Medium — degraded but workaround exists provider/deepseek DeepSeek API type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DeepSeek /anthropic (V4 thinking): stripped thinking blocks cause HTTP 400 on replay

3 participants