Preserving thinking blocks for DeepSeek and Xiaomi MiMo Anthropic endpoints
Cluster 16748 · agent · Bug. Data as of
3 open items. Fix merged in #17510 on 29 Apr 2026. Check it covers your case.
The short answer
- Known?
- Yes. 11 related items grouped here, 2 issues already closed.
- Fix event
- A fix was merged to main in PR #17510. Check it covers your case. No release data is tracked. Fix link checked: 29 Apr 2026. This confirmation is old and may be out of date (a revert or reopen would not have invalidated it here); re-verify.
- Tracker
- The maintainers' tracker marks this
partially resolved, deciding PR #17510. That is separate from the fix event.
Next step: read #17510, then check whether your report is one of the 2 still open.
Read #17510Pick up a Lab taskMembers describe issues and proposed changes around thinking blocks being stripped from message history when using DeepSeek or Xiaomi MiMo via Anthropic-compatible endpoints, which is reported to cause HTTP 400 errors. Most are fix PRs or bug reports for this theme, with a couple of feature PRs adding MiMo endpoint support for thinking mode.
Open issues2
Open PRs1
- PR#43947Fix/deepsfix(deepseek): omitting tool_choice auto/any and preserving signed thinking blocks for /anthropic endpointeek anthropic compat
Fixes on record
-
#17510 fix(anthropic): preserve DeepSeek /anthropic thinking blocks on replay (#16748)
files:
agent/anthropic_adapter.py,tests/agent/test_deepseek_anthropic_thinking.pyShow the diff
-
agent/anthropic_adapter.py@@ -420,6 +420,33 @@ def _is_kimi_family_endpoint(base_url: str | None, model: str | None = None) -> return False +def _is_deepseek_anthropic_endpoint(base_url: str | None) -> bool: + """Return True for DeepSeek's Anthropic-compatible endpoint. + + DeepSeek's ``/anthropic`` route speaks the Anthropic Messages protocol + but, when thinking mode is enabled, requires the ``thinking`` blocks + from prior assistant turns to round-trip on subsequent requests — the + generic third-party path strips them and triggers HTTP 400:: + + The content[].thinking in the thinking mode must be passed back + to the API. + + Per DeepSeek's published compatibility matrix the blocks are unsigned + (no Anthropic-proprietary signature, no ``redacted_thinking`` support), + so this endpoint is handled with the same strip-signed / keep-unsigned + policy used for Kimi's ``/coding`` endpoint. The match is pinned to + the ``/anthropic`` path so the OpenAI-compatible ``api.deepseek.com`` + base URL (which never reaches this adapter) is not misclassified. + See hermes-agent#16748. + """ + if not base_url_host_matches(base_url or "", "api.deepseek.com"): + return False + normalized = _normalize_base_url_text(base_url) + if not normalized: + return False + return "/anthropic" in normalized.rstrip("/").lower() + + def _requires_bearer_auth(base_url: str | None) -> bool: """Return True for Anthropic-compatible providers that require Bearer auth. @@ -1569,7 +1596,16 @@ def convert_messages_to_anthropic( # cache markers can interfere with signature validation. _THINKING_TYPES = frozenset(("thinking", "redacted_thinking")) _is_third_party = _is_third_party_anthropic_endpoint(base_url) - _is_kimi = _is_kimi_family_endpoint(base_url, model) + # Kimi /coding and DeepSeek /anthropic share a contract: both speak the + # Anthropic Messages protocol upstream but require that thinking blocks + # synthesised from reasoning_content round-trip on subsequent turns when + # thinking is enabled. Signed Anthropic blocks still have to be stripped + # (neither endpoint can validate Anthropic's signatures); unsigned blocks + # are preserved. See hermes-agent#13848 (Kimi) and #16748 (DeepSeek). + _preserve_unsigned_thinking = ( + _is_kimi_family_endpoint(base_url, model) + or _is_deepseek_anthropic_endpoint(base_url) + ) last_assistant_idx = None for i in range(len(result) - 1, -1, -1): @@ -1581,22 +1617,22 @@ def convert_messages_to_anthropic( if m.get("role") != "assistant" or not isinstance(m.get("content"), list): continue - if _is_kimi: - # Kimi's /coding endpoint enables thinking server-side and - # requires unsigned thinking blocks on replayed assistant - # tool-call messages. Strip signed Anthropic blocks (Kimi - # can't validate signatures) but preserve the unsigned ones - # we synthesised from reasoning_content above. + if _preserve_unsigned_thinking: + # Kimi's /coding and DeepSeek's /anthropic endpoints both enable + # thinking server-side and require unsigned thinking blocks on + # replayed assistant tool-call messages. Strip signed Anthropic + # blocks (neither upstream can validate Anthropic signatures) but + # preserve the unsigned ones we synthesised from reasoning_content. new_content = [] for b in m["content"]: if not isinstance(b, dict) or b.get("type") not in _THINKING_TYPES: new_content.append(b) continue if b.get("signature") or b.get("data"): - # Anthropic-signed block — Kimi can't validate, strip + # Anthropic-signed block — upstream can't validate, strip continue # Unsigned thinking (synthesised from reasoning_content) — - # keep it: Kimi needs it for message-history validation. + # keep it: the upstream needs it for message-history validation. new_content.append(b) m["content"] = new_content or [{"type": "text", "text": "(empty)"}] elif _is_third_party or idx != last_assistant_idx: -
tests/agent/test_deepseek_anthropic_thinking.py@@ -0,0 +1,242 @@ +"""Regression guard: preserve thinking blocks on DeepSeek's /anthropic endpoint. + +DeepSeek's ``api.deepseek.com/anthropic`` route speaks the Anthropic Messages +protocol but, when thinking mode is enabled, requires ``thinking`` blocks from +prior assistant turns to round-trip on subsequent requests. The generic +third-party path strips them (signatures are Anthropic-proprietary and other +proxies cannot validate them), so without a DeepSeek-specific carve-out the +next tool-call turn fails with HTTP 400:: + + The content[].thinking in the thinking mode must be passed back to the + API. + +DeepSeek's compatibility matrix lists ``thinking`` as supported but +``redacted_thinking`` and ``cache_control`` on thinking blocks as not +supported. Handling is the same as Kimi's ``/coding`` endpoint: strip +Anthropic-signed blocks (DeepSeek can't validate them) but preserve unsigned +blocks that Hermes synthesises from ``reasoning_content``. + +See hermes-agent#16748. +""" + +from __future__ import annotations + +import pytest + + +class TestDeepSeekAnthropicPreservesThinking: + """convert_messages_to_anthropic must replay DeepSeek thinking blocks.""" + + @pytest.mark.parametrize( + "base_url", + [ + "https://api.deepseek.com/anthropic", + "https://api.deepseek.com/anthropic/", + "https://api.deepseek.com/anthropic/v1", + "https://API.DeepSeek.com/anthropic", + ], + ) + def test_unsigned_thinking_block_survives_replay(self, base_url: str) -> None: + """Unsigned thinking (synthesised from reasoning_content) must be preserved.""" + from agent.anthropic_adapter import convert_messages_to_anthropic + + messages = [ + {"role": "user", "content": "hi"}, + { + "role": "assistant", + "reasoning_content": "planning the tool call", + "tool_calls": [ + { + "id": "call_1", + "type": "function", + "function": {"name": "skill_view", "arguments": "{}"}, + } + ], + }, + {"role": "tool", "tool_call_id": "call_1", "content": "ok"}, + ] + _system, converted = convert_messages_to_anthropic( + messages, base_url=base_url + ) + + assistant_msg = next(m for m in converted if m["role"] == "assistant") + thinking_blocks = [ + b for b in assistant_msg["content"] + if isinstance(b, dict) and b.get("type") == "thinking" + ] + assert len(thinking_blocks) == 1, ( + f"DeepSeek /anthropic ({base_url}) must preserve unsigned thinking " + "blocks synthesised from reasoning_content — upstream rejects " + "replayed tool-call messages without them." + ) + assert thinking_blocks[0]["thinking"] == "planning the tool call" + # Synthesised block — never has a signature + assert "signature" not in thinking_blocks[0] + + def test_unsigned_thinking_preserved_on_non_latest_assistant_turn(self) -> None: + """DeepSeek validates history across every prior assistant turn, not just last.""" + from agent.anthropic_adapter import convert_messages_to_anthropic + + messages = [ + {"role": "user", "content": "q1"}, + { + "role": "assistant", + "reasoning_content": "r1", + "tool_calls": [ + { + "id": "call_1", + "type": "function", + "function": {"name": "f", "arguments": "{}"}, + } + ], + }, + {"role": "tool", "tool_call_id": "call_1", "content": "ok"}, + {"role": "user", "content": "q2"}, + { + "role": "assistant", + "reasoning_content": "r2", + "tool_calls": [ + { + "id": "call_2", + "type": "function", + "function": {"name": "f", "arguments": "{}"}, + } + ], + }, + {"role": "tool", "tool_call_id": "call_2", "content": "ok"}, + ] + _system, converted = convert_messages_to_anthropic( + messages, base_url="https://api.deepseek.com/anthropic" + ) + + assistants = [m for m in converted if m["role"] == "assistant"] + assert len(assistants) == 2 + for assistant, expected in zip(assistants, ("r1", "r2")): + thinking = [ + b for b in assistant["content"] + if isinstance(b, dict) and b.get("type") == "thinking" + ] + assert len(thinking) == 1 + assert thinking[0]["thinking"] == expected + + def test_signed_anthropic_thinking_block_is_stripped(self) -> None: + """Anthropic-signed blocks (that leaked through) must still be stripped. + + DeepSeek issues its own signatures and cannot validate Anthropic's — + the strip-signed / keep-unsigned split matches the Kimi policy. + """ + from agent.anthropic_adapter import convert_messages_to_anthropic + + messages = [ + {"role": "user", "content": "hi"}, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "anthropic-signed payload", + "signature": "anthropic-sig-xyz", + }, + {"type": "text", "text": "hello"}, + ], + }, + {"role": "user", "content": "again"}, + ] + _system, converted = convert_messages_to_anthropic( + messages, base_url="https://api.deepseek.com/anthropic" + ) + + assistant_msg = next(m for m in converted if m["role"] == "assistant") + thinking_blocks = [ + b for b in assistant_msg["content"] + if isinstance(b, dict) and b.get("type") == "thinking" + ] + assert thinking_blocks == [], ( + "Signed Anthropic thinking blocks must be stripped on DeepSeek — " + "DeepSeek cannot validate Anthropic-proprietary signatures." + ) + + def test_cache_control_stripped_from_thinking_block(self) -> None: + """cache_control must still be stripped even when the block is preserved. + + DeepSeek's compatibility matrix lists cache_control on thinking blocks + as ignored — cache markers interfere with signature validation on + upstreams that do check them, so Hermes strips them everywhere. + """ + from agent.anthropic_adapter import convert_messages_to_anthropic + + messages = [ + {"role": "user", "content": "hi"}, + { + "role": "assistant", + "reasoning_content": "r1", + "tool_calls": [ + { + "id": "call_1", + "type": "function", + "function": {"name": "f", "arguments": "{}"}, + } + ], + }, + {"role": "tool", "tool_call_id": "call_1", "content": "ok"}, + ] + # Inject cache_control on the synthesised thinking block after-the-fact + # by running conversion once, mutating, then re-running would be + # indirect. Instead check the simpler invariant: no thinking block in + # the converted output carries cache_control. + _system, converted = convert_messages_to_anthropic( + messages, base_url="https://api.deepseek.com/anthropic" + ) + for m in converted: + if not isinstance(m.get("content"), list): + continue + for b in m["content"]: + if isinstance(b, dict) and b.get("type") in ("thinking", "redacted_thinking"): + assert "cache_control" not in b + + def test_openai_compat_deepseek_base_is_not_matched(self) -> None: + """The OpenAI-compatible ``api.deepseek.com`` base must NOT trigger the + DeepSeek /anthropic branch — it never reaches this adapter, but the + detector should still fail closed so an accidental misuse doesn't + quietly send signed Anthropic blocks to an OpenAI endpoint. + """ + from agent.anthropic_adapter import _is_deepseek_anthropic_endpoint + + assert _is_deepseek_anthropic_endpoint("https://api.deepseek.com") is False + assert _is_deepseek_anthropic_endpoint("https://api.deepseek.com/v1") is False + assert _is_deepseek_anthropic_endpoint("https://api.deepseek.com/anthropic") is True + assert _is_deepseek_anthropic_endpoint("https://api.deepseek.com/anthropic/v1") is True + + def test_non_deepseek_third_party_still_strips_all_thinking(self) -> None: + """MiniMax and other third-party Anthropic endpoints must keep the + generic strip-all behaviour (they reject unsigned blocks outright). + """ + from agent.anthropic_adapter import convert_messages_to_anthropic + + messages = [ + {"role": "user", "content": "hi"}, + { + "role": "assistant", + "reasoning_content": "r1", + "tool_calls": [ + { + "id": "call_1", + "type": "function", + "function": {"name": "f", "arguments": "{}"}, + } + ], + }, + {"role": "tool", "tool_call_id": "call_1", "content": "ok"}, + ] + _system, converted = convert_messages_to_anthropic( + messages, base_url="https://api.minimax.io/anthropic" + ) + assistant_msg = next(m for m in converted if m["role"] == "assistant") + thinking_blocks = [ + b for b in assistant_msg["content"] + if isinstance(b, dict) and b.get("type") == "thinking" + ] + assert thinking_blocks == [], ( + "Non-DeepSeek third-party endpoints must keep the generic " + "strip-all-thinking behaviour — unsigned blocks get rejected." + )
-
5 PRs closed without merging
From the close comments: duplicate 2 · done elsewhere 1 · merged via another change 1 · other 1
-
#16781 fix: preserve DeepSeek thinking blocks on Anthropic replay (#16748) · merged via another change →
fd5479a,#17510 -
#17223 fix(anthropic_adapter): preserve unsigned thinking blocks for DeepSeek /anthropic (#16748) · done elsewhere →
a7e7921,#16748 -
#24887 feat: add Xiaomi MiMo Anthropic-compatible endpoint support for thinking mode · duplicate →
#24465,#24603,#24784,#24810,#24884,#24443 -
#24893 fix: preserve Xiaomi MiMo thinking blocks · duplicate →
#24465 - #26775 fix: preserve thinking blocks for Xiaomi MiMo models via Anthropic relay · other
Closed issues · 2
How this cluster was grouped
-
Link evidence (
partly structural links): some members are connected by structural evidence (shared changed files, or a closing/superseded reference); the rest are joined by an automated relevance judgement over their text. - Members outside that structural core may belong to a different underlying problem.
-
Pairwise link density
0.15: the share of item pairs that carry a link, counting a structural link in full and a relevance-only link at 0.36.1.00means every pair is linked.