Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(llm): stop replaying Anthropic thinking from completed turns

GordonYounge49d2287503d
diff --git a/deeptutor/services/llm/provider_core/anthropic_provider.py b/deeptutor/services/llm/provider_core/anthropic_provider.py
index 47bd029e5..4bb0bc638 100644
--- a/deeptutor/services/llm/provider_core/anthropic_provider.py
+++ b/deeptutor/services/llm/provider_core/anthropic_provider.py
@@ -142,7 +142,61 @@ class AnthropicProvider(LLMProvider):
)
continue
- return system, self._merge_consecutive(raw)
+ return system, self._drop_completed_turn_thinking(self._merge_consecutive(raw))
+
+ @staticmethod
+ def _opens_new_turn(msg: dict[str, Any]) -> bool:
+ """Whether a converted user message is a new prompt.
+
+ A user message carrying ``tool_result`` blocks answers the preceding
+ assistant's tool calls, so it continues that turn. Any other user
+ message starts one.
+ """
+ if msg.get("role") != "user":
+ return False
+ content = msg.get("content")
+ if not isinstance(content, list):
+ return True
+ return not any(
+ isinstance(block, dict) and block.get("type") == "tool_result" for block in content
+ )
+
+ @classmethod
+ def _drop_completed_turn_thinking(cls, msgs: list[dict[str, Any]]) -> list[dict[str, Any]]:
+ """Keep signed thinking only for the turn still in progress.
+
+ Anthropic needs the thinking blocks of the assistant rounds after the
+ latest user prompt (a tool loop must replay them verbatim). Blocks
+ from earlier, completed turns are optional, and the API binds each
+ signature to the system prompt and messages it was produced under.
+ DeepTutor rebuilds that prefix every turn (runtime context, learning
+ journal, KB seed), so replaying an earlier turn's block is rejected
+ with ``Invalid `signature` in `thinking` block`` and every follow-up
+ message in a conversation fails.
+ """
+ last_prompt = -1
+ for index, msg in enumerate(msgs):
+ if cls._opens_new_turn(msg):
+ last_prompt = index
+ if last_prompt <= 0:
+ return msgs
+
+ result: list[dict[str, Any]] = []
+ for index, msg in enumerate(msgs):
+ content = msg.get("content")
+ if index < last_prompt and msg.get("role") == "assistant" and isinstance(content, list):
+ kept = [
+ block
+ for block in content
+ if not (
+ isinstance(block, dict)
+ and block.get("type") in {"thinking", "redacted_thinking"}
+ )
+ ]
+ if len(kept) != len(content):
+ msg = {**msg, "content": kept or [{"type": "text", "text": "(empty)"}]}
+ result.append(msg)
+ return result
@staticmethod
def _tool_result_block(msg: dict[str, Any]) -> dict[str, Any]:
diff --git a/tests/services/llm/test_anthropic_provider_fixes.py b/tests/services/llm/test_anthropic_provider_fixes.py
index 132728a02..781236ca0 100644
--- a/tests/services/llm/test_anthropic_provider_fixes.py
+++ b/tests/services/llm/test_anthropic_provider_fixes.py
@@ -293,3 +293,127 @@ def test_assistant_blocks_omit_unsigned_history_blocks() -> None:
)
assert all(block.get("type") != "thinking" for block in blocks)
+
+
+# ---------------------------------------------------------------------------
+# Thinking from completed turns is not replayed. The API binds each signed
+# block to the prefix it was produced under, and DeepTutor rebuilds that prefix
+# every turn, so replaying it failed every follow-up message with
+# "Invalid `signature` in `thinking` block".
+# ---------------------------------------------------------------------------
+
+
+def _thinking_signatures(message: dict[str, Any]) -> list[str]:
+ return [
+ block["signature"]
+ for block in message["content"]
+ if isinstance(block, dict) and block.get("type") == "thinking"
+ ]
+
+
+def _tool_call(call_id: str) -> dict[str, Any]:
+ return {"id": call_id, "type": "function", "function": {"name": "rag", "arguments": "{}"}}
+
+
+def test_convert_messages_drops_thinking_from_a_completed_turn() -> None:
+ _, converted = _provider("claude-opus-5-5")._convert_messages(
+ [
+ {"role": "system", "content": "sys"},
+ {"role": "user", "content": "first question"},
+ {
+ "role": "assistant",
+ "content": "first answer",
+ "thinking_blocks": [_signed_block("earlier-turn")],
+ },
+ {"role": "user", "content": "follow-up"},
+ ]
+ )
+
+ assert [m["role"] for m in converted] == ["user", "assistant", "user"]
+ assert _thinking_signatures(converted[1]) == []
+ assert converted[1]["content"] == [{"type": "text", "text": "first answer"}]
+
+
+def test_convert_messages_drops_thinking_rebuilt_from_history_state() -> None:
+ _, converted = _provider("claude-opus-5-5")._convert_messages(
+ [
+ {"role": "user", "content": "first question"},
+ {
+ "role": "assistant",
+ "content": "first answer",
+ "_provider_response_state": {"thinking_blocks": [_signed_block("persisted")]},
+ },
+ {"role": "user", "content": "follow-up"},
+ ]
+ )
+
+ assert _thinking_signatures(converted[1]) == []
+
+
+def test_convert_messages_keeps_thinking_for_the_tool_loop_in_progress() -> None:
+ _, converted = _provider("claude-opus-5-5")._convert_messages(
+ [
+ {"role": "user", "content": "first question"},
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [_tool_call("call_old")],
+ "thinking_blocks": [_signed_block("old-round")],
+ },
+ {"role": "tool", "tool_call_id": "call_old", "content": "old result"},
+ {
+ "role": "assistant",
+ "content": "first answer",
+ "thinking_blocks": [_signed_block("old-final")],
+ },
+ {"role": "user", "content": "follow-up"},
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [_tool_call("call_live_1")],
+ "thinking_blocks": [_signed_block("live-1")],
+ },
+ {"role": "tool", "tool_call_id": "call_live_1", "content": "live result 1"},
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [_tool_call("call_live_2")],
+ "thinking_blocks": [_signed_block("live-2")],
+ },
+ {"role": "tool", "tool_call_id": "call_live_2", "content": "live result 2"},
+ ]
+ )
+
+ assistant = [m for m in converted if m["role"] == "assistant"]
+ assert [_thinking_signatures(m) for m in assistant] == [[], [], ["live-1"], ["live-2"]]
+ # The tool loop of the completed turn keeps its tool_use blocks.
+ assert any(block.get("type") == "tool_use" for block in assistant[0]["content"])
+
+
+def test_convert_messages_never_leaves_a_completed_turn_empty() -> None:
+ _, converted = _provider("claude-opus-5-5")._convert_messages(
+ [
+ {"role": "user", "content": "first question"},
+ {"role": "assistant", "content": "", "thinking_blocks": [_signed_block()]},
+ {"role": "user", "content": "follow-up"},
+ ]
+ )
+
+ assert converted[1]["content"] == [{"type": "text", "text": "(empty)"}]
+
+
+def test_convert_messages_leaves_a_single_turn_untouched() -> None:
+ _, converted = _provider("claude-opus-5-5")._convert_messages(
+ [
+ {"role": "user", "content": "question"},
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [_tool_call("call_1")],
+ "thinking_blocks": [_signed_block("only-turn")],
+ },
+ {"role": "tool", "tool_call_id": "call_1", "content": "result"},
+ ]
+ )
+
+ assert _thinking_signatures(converted[1]) == ["only-turn"]