From 8ca3ebf8697ab9fa7cbcda202be00c349d460f3b Mon Sep 17 00:00:00 2001 From: zszz3 <91608029+zszz3@users.noreply.github.com> Date: Wed, 25 Nov 2026 16:45:26 +0800 Subject: [PATCH] fix: send an empty reasoning_content on reasoning-less tool rounds A thinking model occasionally streams a tool call with no reasoning. The loop only writes reasoning_content when there is text, so DeepSeek rejected the next request of the turn with "The reasoning_content in the thinking mode must be passed back to the API". Once the request already carries reasoning elsewhere, the reasoning-less tool round now gets an explicit empty field. --- .../provider_core/openai_compat_provider.py | 36 ++++++++++ .../test_openai_compat_reasoning_content.py | 67 +++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/deeptutor/services/llm/provider_core/openai_compat_provider.py b/deeptutor/services/llm/provider_core/openai_compat_provider.py index 54efe47fa..f28255ddc 100644 --- a/deeptutor/services/llm/provider_core/openai_compat_provider.py +++ b/deeptutor/services/llm/provider_core/openai_compat_provider.py @@ -178,6 +178,39 @@ def _provider_state_output_items(message: dict[str, Any]) -> list[dict[str, Any] return [dict(item) for item in items if isinstance(item, dict)] +def _fill_empty_tool_round_reasoning(messages: list[dict[str, Any]]) -> None: + """Give a reasoning-less tool-call round an explicit empty reasoning field. + + A thinking model occasionally streams a tool call with no reasoning at all. + The loop only writes ``reasoning_content`` when there is text, so that + round went back without the field, and DeepSeek rejected the next request + of the same turn: "The `reasoning_content` in the thinking mode must be + passed back to the API". An empty string satisfies it. + + A field that is present but ``None`` counts as missing: it would go out as + ``null``. The field is only added once another assistant message in the + request already carries reasoning, i.e. the provider has shown it speaks this + dialect; an ordinary history stays untouched. + """ + speaks_reasoning = any( + isinstance(message, dict) + and message.get("role") == "assistant" + and isinstance(message.get("reasoning_content"), str) + and message["reasoning_content"] + for message in messages + ) + if not speaks_reasoning: + return + for message in messages: + if ( + isinstance(message, dict) + and message.get("role") == "assistant" + and message.get("tool_calls") + and message.get("reasoning_content") is None + ): + message["reasoning_content"] = "" + + def _is_direct_openai_base(api_base: str | None) -> bool: if not api_base: return False @@ -493,6 +526,9 @@ class OpenAICompatProvider(LLMProvider): clean.setdefault("reasoning_content", reasoning_content) prepared.append(clean) + if not responses_api: + _fill_empty_tool_round_reasoning(prepared) + sanitized = LLMProvider._sanitize_request_messages(prepared, _ALLOWED_MSG_KEYS) if responses_api: # Responses function calls use the provider-issued ``call_id`` as diff --git a/tests/services/llm/test_openai_compat_reasoning_content.py b/tests/services/llm/test_openai_compat_reasoning_content.py index acb129d57..890f98b7d 100644 --- a/tests/services/llm/test_openai_compat_reasoning_content.py +++ b/tests/services/llm/test_openai_compat_reasoning_content.py @@ -376,3 +376,70 @@ def test_temperature_for_gpt6_on_both_wires(model, expected): tool_choice=None, ) assert ("temperature" in body) is expected + + +def _tool_round(reasoning: str | None) -> dict: + message = { + "role": "assistant", + "content": None, + "tool_calls": [ + { + "id": "call_1", + "type": "function", + "function": {"name": "research_lit", "arguments": "{}"}, + } + ], + } + if reasoning is not None: + message["reasoning_content"] = reasoning + return message + + +def test_reasoning_less_tool_round_gets_an_empty_reasoning_field() -> None: + """A thinking model sometimes streams a tool call with no reasoning. + + DeepSeek then rejected the next request of the turn: "The + `reasoning_content` in the thinking mode must be passed back to the API". + Once the request already carries reasoning elsewhere, the reasoning-less + tool round is sent with an explicit empty field. + """ + messages = ServicesOpenAICompatProvider._sanitize_messages( + [ + {"role": "user", "content": "find papers"}, + _tool_round("load the search tool first"), + {"role": "tool", "tool_call_id": "call_1", "content": "loaded"}, + _tool_round(None), + {"role": "tool", "tool_call_id": "call_1", "content": "results"}, + ] + ) + + assert messages[1]["reasoning_content"] == "load the search tool first" + assert messages[3]["reasoning_content"] == "" + + +def test_history_without_reasoning_gets_no_reasoning_field() -> None: + messages = ServicesOpenAICompatProvider._sanitize_messages( + [ + {"role": "user", "content": "find papers"}, + _tool_round(None), + {"role": "tool", "tool_call_id": "call_1", "content": "results"}, + ] + ) + + assert "reasoning_content" not in messages[1] + + +def test_a_null_reasoning_field_on_a_tool_round_is_sent_as_empty() -> None: + """A field that is present but ``None`` would go out as ``null``.""" + null_round = {**_tool_round(None), "reasoning_content": None} + messages = ServicesOpenAICompatProvider._sanitize_messages( + [ + {"role": "user", "content": "find papers"}, + _tool_round("load the search tool first"), + {"role": "tool", "tool_call_id": "call_1", "content": "loaded"}, + null_round, + {"role": "tool", "tool_call_id": "call_1", "content": "results"}, + ] + ) + + assert messages[3]["reasoning_content"] == ""