{"repo":"NousResearch/hermes-agent","public_id":"16748","display_label":"Preserving thinking blocks for DeepSeek and Xiaomi MiMo Anthropic endpoints","summary":"Members 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.","subsystem":"agent","kind":"bug","counts":{"members":11,"open_issues":2,"open_issues_distinct":2,"open_prs":1,"closed_issues":2,"merged_prs":1,"attempt_prs":6},"landed_fix":{"status":"merged_to_main","basis":"structural","pr":17510,"merged_at":"2026-04-29T15:10:30+00:00"},"resolution":{"state":"partially_resolved","by":"pr-17510"},"freshness":{"state":"stale","last_verified_at":"2026-04-29T15:10:30+00:00","is_stale":true},"grouping":{"backbone":"STRUCTURAL_PARTIAL","link_density":0.1469090909090909},"fix_edges":[{"issue_number":16748,"pr_number":17510,"tier":"verified","basis":"closing_ref","confidence":0.95,"recoverable_from_timeline":true,"pr_merged_at":"2026-04-29T15:10:30+00:00"}],"members":[{"kind":"issue","number":16748,"title":"DeepSeek /anthropic (V4 thinking): stripped thinking blocks cause HTTP 400 on replay","url":null,"state":"closed"},{"kind":"issue","number":22313,"title":"bug: DeepSeek Anthropic-compatible API — HTTP 400: thinking blocks stripped from message history","url":null,"state":"closed"},{"kind":"issue","number":24884,"title":"feat: Add Xiaomi MiMo Anthropic-compatible endpoint support for thinking mode","url":null,"state":"open"},{"kind":"issue","number":26774,"title":"Xiaomi MiMo models via Anthropic-compatible relay: thinking blocks stripped causing HTTP 400","url":null,"state":"open"},{"kind":"pr","number":16781,"title":"fix: preserve DeepSeek thinking blocks on Anthropic replay (#16748)","url":null,"state":"closed","close_reason":{"refs":[{"kind":"commit","value":"fd5479a4f","number":0},{"kind":"pr","value":"#17510","number":17510}],"category":"merged_via","implication":"superseded"},"merged":false},{"kind":"pr","number":17223,"title":"fix(anthropic_adapter): preserve unsigned thinking blocks for DeepSeek /anthropic (#16748)","url":null,"state":"closed","close_reason":{"refs":[{"kind":"commit","value":"a7e7921db","number":0},{"kind":"pr","value":"#16748","number":16748}],"category":"implemented_elsewhere","implication":"superseded"},"merged":false,"is_fix_pr":true,"fix_tier":"suggested","fix_detail_withheld":true},{"kind":"pr","number":17510,"title":"fix(anthropic): preserve DeepSeek /anthropic thinking blocks on replay (#16748)","url":null,"state":"merged","close_reason":{"category":"merged"},"merged":true,"is_fix_pr":true,"fix_tier":"verified","files_changed":["agent/anthropic_adapter.py","tests/agent/test_deepseek_anthropic_thinking.py"],"fix_hunks":[{"file":"agent/anthropic_adapter.py","hunks":["@@ -420,6 +420,33 @@ def _is_kimi_family_endpoint(base_url: str | None, model: str | None = None) ->\n     return False\n \n \n+def _is_deepseek_anthropic_endpoint(base_url: str | None) -> bool:\n+    \"\"\"Return True for DeepSeek's Anthropic-compatible endpoint.\n+\n+    DeepSeek's ``/anthropic`` route speaks the Anthropic Messages protocol\n+    but, when thinking mode is enabled, requires the ``thinking`` blocks\n+    from prior assistant turns to round-trip on subsequent requests — the\n+    generic third-party path strips them and triggers HTTP 400::\n+\n+        The content[].thinking in the thinking mode must be passed back\n+        to the API.\n+\n+    Per DeepSeek's published compatibility matrix the blocks are unsigned\n+    (no Anthropic-proprietary signature, no ``redacted_thinking`` support),\n+    so this endpoint is handled with the same strip-signed / keep-unsigned\n+    policy used for Kimi's ``/coding`` endpoint.  The match is pinned to\n+    the ``/anthropic`` path so the OpenAI-compatible ``api.deepseek.com``\n+    base URL (which never reaches this adapter) is not misclassified.\n+    See hermes-agent#16748.\n+    \"\"\"\n+    if not base_url_host_matches(base_url or \"\", \"api.deepseek.com\"):\n+        return False\n+    normalized = _normalize_base_url_text(base_url)\n+    if not normalized:\n+        return False\n+    return \"/anthropic\" in normalized.rstrip(\"/\").lower()\n+\n+\n def _requires_bearer_auth(base_url: str | None) -> bool:\n     \"\"\"Return True for Anthropic-compatible providers that require Bearer auth.\n ","@@ -1569,7 +1596,16 @@ def convert_messages_to_anthropic(\n     #    cache markers can interfere with signature validation.\n     _THINKING_TYPES = frozenset((\"thinking\", \"redacted_thinking\"))\n     _is_third_party = _is_third_party_anthropic_endpoint(base_url)\n-    _is_kimi = _is_kimi_family_endpoint(base_url, model)\n+    # Kimi /coding and DeepSeek /anthropic share a contract: both speak the\n+    # Anthropic Messages protocol upstream but require that thinking blocks\n+    # synthesised from reasoning_content round-trip on subsequent turns when\n+    # thinking is enabled.  Signed Anthropic blocks still have to be stripped\n+    # (neither endpoint can validate Anthropic's signatures); unsigned blocks\n+    # are preserved.  See hermes-agent#13848 (Kimi) and #16748 (DeepSeek).\n+    _preserve_unsigned_thinking = (\n+        _is_kimi_family_endpoint(base_url, model)\n+        or _is_deepseek_anthropic_endpoint(base_url)\n+    )\n \n     last_assistant_idx = None\n     for i in range(len(result) - 1, -1, -1):","@@ -1581,22 +1617,22 @@ def convert_messages_to_anthropic(\n         if m.get(\"role\") != \"assistant\" or not isinstance(m.get(\"content\"), list):\n             continue\n \n-        if _is_kimi:\n-            # Kimi's /coding endpoint enables thinking server-side and\n-            # requires unsigned thinking blocks on replayed assistant\n-            # tool-call messages.  Strip signed Anthropic blocks (Kimi\n-            # can't validate signatures) but preserve the unsigned ones\n-            # we synthesised from reasoning_content above.\n+        if _preserve_unsigned_thinking:\n+            # Kimi's /coding and DeepSeek's /anthropic endpoints both enable\n+            # thinking server-side and require unsigned thinking blocks on\n+            # replayed assistant tool-call messages.  Strip signed Anthropic\n+            # blocks (neither upstream can validate Anthropic signatures) but\n+            # preserve the unsigned ones we synthesised from reasoning_content.\n             new_content = []\n             for b in m[\"content\"]:\n                 if not isinstance(b, dict) or b.get(\"type\") not in _THINKING_TYPES:\n                     new_content.append(b)\n                     continue\n                 if b.get(\"signature\") or b.get(\"data\"):\n-                    # Anthropic-signed block — Kimi can't validate, strip\n+                    # Anthropic-signed block — upstream can't validate, strip\n                     continue\n                 # Unsigned thinking (synthesised from reasoning_content) —\n-                # keep it: Kimi needs it for message-history validation.\n+                # keep it: the upstream needs it for message-history validation.\n                 new_content.append(b)\n             m[\"content\"] = new_content or [{\"type\": \"text\", \"text\": \"(empty)\"}]\n         elif _is_third_party or idx != last_assistant_idx:"]},{"file":"tests/agent/test_deepseek_anthropic_thinking.py","hunks":["@@ -0,0 +1,242 @@\n+\"\"\"Regression guard: preserve thinking blocks on DeepSeek's /anthropic endpoint.\n+\n+DeepSeek's ``api.deepseek.com/anthropic`` route speaks the Anthropic Messages\n+protocol but, when thinking mode is enabled, requires ``thinking`` blocks from\n+prior assistant turns to round-trip on subsequent requests.  The generic\n+third-party path strips them (signatures are Anthropic-proprietary and other\n+proxies cannot validate them), so without a DeepSeek-specific carve-out the\n+next tool-call turn fails with HTTP 400::\n+\n+    The content[].thinking in the thinking mode must be passed back to the\n+    API.\n+\n+DeepSeek's compatibility matrix lists ``thinking`` as supported but\n+``redacted_thinking`` and ``cache_control`` on thinking blocks as not\n+supported.  Handling is the same as Kimi's ``/coding`` endpoint: strip\n+Anthropic-signed blocks (DeepSeek can't validate them) but preserve unsigned\n+blocks that Hermes synthesises from ``reasoning_content``.\n+\n+See hermes-agent#16748.\n+\"\"\"\n+\n+from __future__ import annotations\n+\n+import pytest\n+\n+\n+class TestDeepSeekAnthropicPreservesThinking:\n+    \"\"\"convert_messages_to_anthropic must replay DeepSeek thinking blocks.\"\"\"\n+\n+    @pytest.mark.parametrize(\n+        \"base_url\",\n+        [\n+            \"https://api.deepseek.com/anthropic\",\n+            \"https://api.deepseek.com/anthropic/\",\n+            \"https://api.deepseek.com/anthropic/v1\",\n+            \"https://API.DeepSeek.com/anthropic\",\n+        ],\n+    )\n+    def test_unsigned_thinking_block_survives_replay(self, base_url: str) -> None:\n+        \"\"\"Unsigned thinking (synthesised from reasoning_content) must be preserved.\"\"\"\n+        from agent.anthropic_adapter import convert_messages_to_anthropic\n+\n+        messages = [\n+            {\"role\": \"user\", \"content\": \"hi\"},\n+            {\n+                \"role\": \"assistant\",\n+                \"reasoning_content\": \"planning the tool call\",\n+                \"tool_calls\": [\n+                    {\n+                        \"id\": \"call_1\",\n+                        \"type\": \"function\",\n+                        \"function\": {\"name\": \"skill_view\", \"arguments\": \"{}\"},\n+                    }\n+                ],\n+            },\n+            {\"role\": \"tool\", \"tool_call_id\": \"call_1\", \"content\": \"ok\"},\n+        ]\n+        _system, converted = convert_messages_to_anthropic(\n+            messages, base_url=base_url\n+        )\n+\n+        assistant_msg = next(m for m in converted if m[\"role\"] == \"assistant\")\n+        thinking_blocks = [\n+            b for b in assistant_msg[\"content\"]\n+            if isinstance(b, dict) and b.get(\"type\") == \"thinking\"\n+        ]\n+        assert len(thinking_blocks) == 1, (\n+            f\"DeepSeek /anthropic ({base_url}) must preserve unsigned thinking \"\n+            \"blocks synthesised from reasoning_content — upstream rejects \"\n+            \"replayed tool-call messages without them.\"\n+        )\n+        assert thinking_blocks[0][\"thinking\"] == \"planning the tool call\"\n+        # Synthesised block — never has a signature\n+        assert \"signature\" not in thinking_blocks[0]\n+\n+    def test_unsigned_thinking_preserved_on_non_latest_assistant_turn(self) -> None:\n+        \"\"\"DeepSeek validates history across every prior assistant turn, not just last.\"\"\"\n+        from agent.anthropic_adapter import convert_messages_to_anthropic\n+\n+        messages = [\n+            {\"role\": \"user\", \"content\": \"q1\"},\n+            {\n+                \"role\": \"assistant\",\n+                \"reasoning_content\": \"r1\",\n+                \"tool_calls\": [\n+                    {\n+                        \"id\": \"call_1\",\n+                        \"type\": \"function\",\n+                        \"function\": {\"name\": \"f\", \"arguments\": \"{}\"},\n+                    }\n+                ],\n+            },\n+            {\"role\": \"tool\", \"tool_call_id\": \"call_1\", \"content\": \"ok\"},\n+            {\"role\": \"user\", \"content\": \"q2\"},\n+            {\n+                \"role\": \"assistant\",\n+                \"reasoning_content\": \"r2\",\n+                \"tool_calls\": [\n+                    {\n+                        \"id\": \"call_2\",\n+                        \"type\": \"function\",\n+                        \"function\": {\"name\": \"f\", \"arguments\": \"{}\"},\n+                    }\n+                ],\n+            },\n+            {\"role\": \"tool\", \"tool_call_id\": \"call_2\", \"content\": \"ok\"},\n+        ]\n+        _system, converted = convert_messages_to_anthropic(\n+            messages, base_url=\"https://api.deepseek.com/anthropic\"\n+        )\n+\n+        assistants = [m for m in converted if m[\"role\"] == \"assistant\"]\n+        assert len(assistants) == 2\n+        for assistant, expected in zip(assistants, (\"r1\", \"r2\")):\n+            thinking = [\n+                b for b in assistant[\"content\"]\n+                if isinstance(b, dict) and b.get(\"type\") == \"thinking\"\n+            ]\n+            assert len(thinking) == 1\n+            assert thinking[0][\"thinking\"] == expected\n+\n+    def test_signed_anthropic_thinking_block_is_stripped(self) -> None:\n+        \"\"\"Anthropic-signed blocks (that leaked through) must still be stripped.\n+\n+        DeepSeek issues its own signatures and cannot validate Anthropic's —\n+        the strip-signed / keep-unsigned split matches the Kimi policy.\n+        \"\"\"\n+        from agent.anthropic_adapter import convert_messages_to_anthropic\n+\n+        messages = [\n+            {\"role\": \"user\", \"content\": \"hi\"},\n+            {\n+                \"role\": \"assistant\",\n+                \"content\": [\n+                    {\n+                        \"type\": \"thinking\",\n+                        \"thinking\": \"anthropic-signed payload\",\n+                        \"signature\": \"anthropic-sig-xyz\",\n+                    },\n+                    {\"type\": \"text\", \"text\": \"hello\"},\n+                ],\n+            },\n+            {\"role\": \"user\", \"content\": \"again\"},\n+        ]\n+        _system, converted = convert_messages_to_anthropic(\n+            messages, base_url=\"https://api.deepseek.com/anthropic\"\n+        )\n+\n+        assistant_msg = next(m for m in converted if m[\"role\"] == \"assistant\")\n+        thinking_blocks = [\n+            b for b in assistant_msg[\"content\"]\n+            if isinstance(b, dict) and b.get(\"type\") == \"thinking\"\n+        ]\n+        assert thinking_blocks == [], (\n+            \"Signed Anthropic thinking blocks must be stripped on DeepSeek — \"\n+            \"DeepSeek cannot validate Anthropic-proprietary signatures.\"\n+        )\n+\n+    def test_cache_control_stripped_from_thinking_block(self) -> None:\n+        \"\"\"cache_control must still be stripped even when the block is preserved.\n+\n+        DeepSeek's compatibility matrix lists cache_control on thinking blocks\n+        as ignored — cache markers interfere with signature validation on\n+        upstreams that do check them, so Hermes strips them everywhere.\n+        \"\"\"\n+        from agent.anthropic_adapter import convert_messages_to_anthropic\n+\n+        messages = [\n+            {\"role\": \"user\", \"content\": \"hi\"},\n+            {\n+                \"role\": \"assistant\",\n+                \"reasoning_content\": \"r1\",\n+                \"tool_calls\": [\n+                    {\n+                        \"id\": \"call_1\",\n+                        \"type\": \"function\",\n+                        \"function\": {\"name\": \"f\", \"arguments\": \"{}\"},\n+                    }\n+                ],\n+            },\n+            {\"role\": \"tool\", \"tool_call_id\": \"call_1\", \"content\": \"ok\"},\n+        ]\n+        # Inject cache_control on the synthesised thinking block after-the-fact\n+        # by running conversion once, mutating, then re-running would be\n+        # indirect.  Instead check the simpler invariant: no thinking block in\n+        # the converted output carries cache_control.\n+        _system, converted = convert_messages_to_anthropic(\n+            messages, base_url=\"https://api.deepseek.com/anthropic\"\n+        )\n+        for m in converted:\n+            if not isinstance(m.get(\"content\"), list):\n+                continue\n+            for b in m[\"content\"]:\n+                if isinstance(b, dict) and b.get(\"type\") in (\"thinking\", \"redacted_thinking\"):\n+                    assert \"cache_control\" not in b\n+\n+    def test_openai_compat_deepseek_base_is_not_matched(self) -> None:\n+        \"\"\"The OpenAI-compatible ``api.deepseek.com`` base must NOT trigger the\n+        DeepSeek /anthropic branch — it never reaches this adapter, but the\n+        detector should still fail closed so an accidental misuse doesn't\n+        quietly send signed Anthropic blocks to an OpenAI endpoint.\n+        \"\"\"\n+        from agent.anthropic_adapter import _is_deepseek_anthropic_endpoint\n+\n+        assert _is_deepseek_anthropic_endpoint(\"https://api.deepseek.com\") is False\n+        assert _is_deepseek_anthropic_endpoint(\"https://api.deepseek.com/v1\") is False\n+        assert _is_deepseek_anthropic_endpoint(\"https://api.deepseek.com/anthropic\") is True\n+        assert _is_deepseek_anthropic_endpoint(\"https://api.deepseek.com/anthropic/v1\") is True\n+\n+    def test_non_deepseek_third_party_still_strips_all_thinking(self) -> None:\n+        \"\"\"MiniMax and other third-party Anthropic endpoints must keep the\n+        generic strip-all behaviour (they reject unsigned blocks outright).\n+        \"\"\"\n+        from agent.anthropic_adapter import convert_messages_to_anthropic\n+\n+        messages = [\n+            {\"role\": \"user\", \"content\": \"hi\"},\n+            {\n+                \"role\": \"assistant\",\n+                \"reasoning_content\": \"r1\",\n+                \"tool_calls\": [\n+                    {\n+                        \"id\": \"call_1\",\n+                        \"type\": \"function\",\n+                        \"function\": {\"name\": \"f\", \"arguments\": \"{}\"},\n+                    }\n+                ],\n+            },\n+            {\"role\": \"tool\", \"tool_call_id\": \"call_1\", \"content\": \"ok\"},\n+        ]\n+        _system, converted = convert_messages_to_anthropic(\n+            messages, base_url=\"https://api.minimax.io/anthropic\"\n+        )\n+        assistant_msg = next(m for m in converted if m[\"role\"] == \"assistant\")\n+        thinking_blocks = [\n+            b for b in assistant_msg[\"content\"]\n+            if isinstance(b, dict) and b.get(\"type\") == \"thinking\"\n+        ]\n+        assert thinking_blocks == [], (\n+            \"Non-DeepSeek third-party endpoints must keep the generic \"\n+            \"strip-all-thinking behaviour — unsigned blocks get rejected.\"\n+        )\n"]}]},{"kind":"pr","number":24887,"title":"feat: add Xiaomi MiMo Anthropic-compatible endpoint support for thinking mode","url":null,"state":"closed","close_reason":{"refs":[{"kind":"pr","value":"#24465","number":24465},{"kind":"pr","value":"#24603","number":24603},{"kind":"pr","value":"#24784","number":24784},{"kind":"pr","value":"#24810","number":24810},{"kind":"pr","value":"#24884","number":24884},{"kind":"pr","value":"#24443","number":24443}],"category":"duplicate_of","implication":"duplicate"},"merged":false},{"kind":"pr","number":24893,"title":"fix: preserve Xiaomi MiMo thinking blocks","url":null,"state":"closed","close_reason":{"refs":[{"kind":"pr","value":"#24465","number":24465}],"category":"duplicate_of","implication":"duplicate"},"merged":false},{"kind":"pr","number":26775,"title":"fix: preserve thinking blocks for Xiaomi MiMo models via Anthropic relay","url":null,"state":"closed","close_reason":{"category":"uncategorized","implication":"needs_llm"},"merged":false,"is_fix_pr":true,"fix_tier":"suggested","fix_detail_withheld":true},{"kind":"pr","number":43947,"title":"Fix/deepsfix(deepseek): omitting tool_choice auto/any and preserving signed thinking blocks for /anthropic endpointeek anthropic compat","url":null,"state":"open","close_reason":{"category":"uncategorized","implication":"needs_llm"},"merged":false}],"trust":{"default_tier":"verified","includes_suggested":false}}