Skip to content

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

fix(llm): replay reasoning for every provider that sends it, not just DeepSeek

Bingxi Zhao (Frank)e431c81f9f0e
diff --git a/deeptutor/services/llm/provider_core/openai_compat_provider.py b/deeptutor/services/llm/provider_core/openai_compat_provider.py
index 4c48318b8..2b59ae07c 100644
--- a/deeptutor/services/llm/provider_core/openai_compat_provider.py
+++ b/deeptutor/services/llm/provider_core/openai_compat_provider.py
@@ -321,7 +321,6 @@ class OpenAICompatProvider(LLMProvider):
messages: list[dict[str, Any]],
*,
responses_api: bool = False,
- model: str | None = None,
) -> list[dict[str, Any]]:
prepared: list[dict[str, Any]] = []
for message in messages:
@@ -338,15 +337,24 @@ class OpenAICompatProvider(LLMProvider):
if output_items:
clean["_provider_response_state"] = {"responses_output_items": output_items}
else:
+ # Replay the round's own reasoning on the assistant turn that
+ # produced it. A thinking model's provider rejects a history
+ # that lost it ("the reasoning_content in the thinking mode
+ # must be passed back to the API"), and only a provider that
+ # SENT ``reasoning_content``/``reasoning`` can have put it in
+ # this state — so replaying it is symmetric, never additive.
+ #
+ # This used to be gated on ``"deepseek" in model``, which is
+ # not how a model announces the dialect: Volcengine Ark takes
+ # an endpoint id (``ep-…``) as the model name, and Doubao /
+ # GLM / Qwen / Kimi thinking models speak the same field under
+ # their own names. Every one of them lost its reasoning the
+ # moment a turn replayed history, while the *same* round
+ # inside one turn kept it (the loop sets the field directly).
state = normalize_provider_response_state(message.get("_provider_response_state"))
reasoning_content = state.get("reasoning_content") if state is not None else None
- if (
- isinstance(reasoning_content, str)
- and reasoning_content
- and model
- and "deepseek" in model.lower()
- ):
- clean["reasoning_content"] = reasoning_content
+ if isinstance(reasoning_content, str) and reasoning_content:
+ clean.setdefault("reasoning_content", reasoning_content)
prepared.append(clean)
sanitized = LLMProvider._sanitize_request_messages(prepared, _ALLOWED_MSG_KEYS)
@@ -415,9 +423,7 @@ class OpenAICompatProvider(LLMProvider):
kwargs: dict[str, Any] = {
"model": model_name,
- "messages": self._sanitize_messages(
- self._sanitize_empty_content(messages), model=model_name
- ),
+ "messages": self._sanitize_messages(self._sanitize_empty_content(messages)),
}
if self._supports_temperature(model_name, reasoning_effort):
@@ -719,9 +725,7 @@ class OpenAICompatProvider(LLMProvider):
model_name = model_name.split("/")[-1]
instructions, input_items = convert_messages(
- self._sanitize_messages(
- self._sanitize_empty_content(messages), responses_api=True, model=model_name
- )
+ self._sanitize_messages(self._sanitize_empty_content(messages), responses_api=True)
)
body: dict[str, Any] = {
"model": model_name,
diff --git a/tests/services/llm/test_openai_compat_reasoning_content.py b/tests/services/llm/test_openai_compat_reasoning_content.py
index 7bea2068d..399926044 100644
--- a/tests/services/llm/test_openai_compat_reasoning_content.py
+++ b/tests/services/llm/test_openai_compat_reasoning_content.py
@@ -150,10 +150,19 @@ def test_services_deepseek_replays_persisted_reasoning_content() -> None:
assert "_provider_response_state" not in assistant_message
-def test_non_deepseek_drops_persisted_reasoning_content() -> None:
+def test_replay_is_not_gated_on_the_model_being_named_deepseek() -> None:
+ """Volcengine Ark takes an endpoint id as the model name.
+
+ The replay used to require ``"deepseek" in model``, so an ``ep-…`` model
+ (and every Doubao / GLM / Qwen / Kimi thinking model) lost its reasoning
+ the moment a turn replayed history — and the provider answered "the
+ reasoning_content in the thinking mode must be passed back to the API".
+ Only a provider that sent the field can have put it in this state, so
+ replaying it is symmetric rather than additive.
+ """
provider = ServicesOpenAICompatProvider.__new__(ServicesOpenAICompatProvider)
- provider.default_model = "gpt-test"
- provider._spec = find_service_provider("openai")
+ provider.default_model = "ep-20260101120000-abcde"
+ provider._spec = find_service_provider("volcengine")
kwargs = provider._build_kwargs(
messages=[
@@ -164,6 +173,26 @@ def test_non_deepseek_drops_persisted_reasoning_content() -> None:
}
],
tools=None,
+ model=None,
+ max_tokens=32,
+ temperature=0.7,
+ reasoning_effort=None,
+ tool_choice=None,
+ )
+
+ assert kwargs["messages"][0]["reasoning_content"] == "private reasoning"
+ assert "_provider_response_state" not in kwargs["messages"][0]
+
+
+def test_a_model_that_never_reasoned_carries_no_reasoning_content() -> None:
+ """No state, no field — the replay adds nothing to an ordinary history."""
+ provider = ServicesOpenAICompatProvider.__new__(ServicesOpenAICompatProvider)
+ provider.default_model = "gpt-test"
+ provider._spec = find_service_provider("openai")
+
+ kwargs = provider._build_kwargs(
+ messages=[{"role": "assistant", "content": "previous answer"}],
+ tools=None,
model="gpt-test",
max_tokens=32,
temperature=0.7,