mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 11:19:51 +08:00
DeepSeek's thinking models reject a continuation whose history has lost the
previous assistant turn's reasoning:
The `reasoning_content` in the thinking mode must be passed back to the API.
Chat's loop has replayed it since #1058. The shared `runtime/agentic` loop —
the one quiz, research, PageIndex and explore_context all run on — never
did. `LabeledStepResult` had no field for it; the stream reader pulled
`reasoning_content` off each delta, pushed it to the trace UI for display,
and dropped it. All three assistant rebuild sites therefore emitted
reasoning-less turns. Round one cannot fail (system + user only); round two
always did, which is why this reads as "quiz generation failed".
The message builder's own docstring said as much: "The provider has always
known how to read this field off a message — nothing ever wrote it."
v1.6.5 made it visible rather than causing it. `deepseek-v4-flash` was in
`_THINKING_DISABLED_BY_DEFAULT_MODELS` in v1.6.4 and the list was emptied,
so the model now thinks by default — correctly, since suppressing reasoning
to dodge this cost every flash user their reasoning stream. `deepseek-v4-pro`
and `deepseek-reasoner` are sent `thinking: enabled` by our own code and were
already broken in v1.6.4. Re-adding flash to that list would hide it again
and still leave those two broken, so this fixes the replay instead.
The reasoning is accumulated for every label, not only the pre-label prelude
the trace happens to render — whether it was displayed is a display question,
and the provider wants the whole round back. Anthropic's signed thinking
blocks are read off `provider_specific_fields`, where the adapter puts them,
because a signature cannot be rebuilt from text. `_append_repair_messages`
now takes the step rather than its text, since a repair round is still a
round. Rounds with no reasoning append no field, so ordinary models see a
byte-identical history.
Deliberately NOT setting `_provider_response_state` alongside: the shared
loop posts its messages straight into `chat.completions.create` on what is
usually a raw AsyncOpenAI client, and the private-key stripping lives inside
`OpenAICompatProvider._sanitize_messages` — so it would be serialized into
the request body for every model off the adapter route.
Also reorders `_classify_failure`: a provider quoting the field it objected
to was landing in `prompt_leak`, which is not retryable and told the user the
model had leaked a prompt. `"prompt" in lower` goes with it — it matches
`prompt_tokens`.
Still open: `_sanitize_messages(responses_api=True)` ignores `reasoning_content`
entirely, so a surface that mounts native web search (which routes DeepSeek
to the Responses API) is not covered by this and falls back to Chat
Completions. The book quiz mounts no tools, so it is not that surface.
80 lines
2.9 KiB
Python
80 lines
2.9 KiB
Python
"""Canonical message builders for agentic conversations."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
|
|
def assistant_message_with_tool_calls(
|
|
content: str,
|
|
tool_calls: list[dict[str, Any]],
|
|
*,
|
|
reasoning_content: str | None = None,
|
|
thinking_blocks: list[dict[str, Any]] | None = None,
|
|
) -> dict[str, Any]:
|
|
"""Build the assistant message that precedes tool result messages.
|
|
|
|
``reasoning_content`` is optional: DeepSeek thinking-mode Chat Completions
|
|
requires the prior round's reasoning to be echoed on the assistant turn
|
|
that issued the tool calls (#1058). Responses-API replay is handled
|
|
separately via ``_responses_output_items``.
|
|
|
|
``thinking_blocks`` is the Anthropic equivalent, and stricter: extended
|
|
thinking returns *signed* blocks, and a turn that issued tool calls must
|
|
replay them verbatim. The provider has always known how to read this field
|
|
off a message — nothing ever wrote it, so the signatures were dropped on
|
|
every round.
|
|
"""
|
|
serialized_calls: list[dict[str, Any]] = []
|
|
for tool_call in tool_calls:
|
|
serialized: dict[str, Any] = {
|
|
"id": tool_call["id"],
|
|
"type": "function",
|
|
"function": {
|
|
"name": tool_call["name"],
|
|
"arguments": tool_call.get("arguments") or "{}",
|
|
},
|
|
}
|
|
# Gemini's OpenAI-compatible endpoint requires the exact opaque
|
|
# thought signature from each function call to be sent back on the
|
|
# next round (#1181). Other providers simply omit this extension.
|
|
extra_content = tool_call.get("extra_content")
|
|
if isinstance(extra_content, dict) and extra_content:
|
|
serialized["extra_content"] = extra_content
|
|
serialized_calls.append(serialized)
|
|
|
|
message: dict[str, Any] = {
|
|
"role": "assistant",
|
|
"content": content or None,
|
|
"tool_calls": serialized_calls,
|
|
}
|
|
if reasoning_content:
|
|
message["reasoning_content"] = reasoning_content
|
|
if thinking_blocks:
|
|
message["thinking_blocks"] = thinking_blocks
|
|
return message
|
|
|
|
|
|
def assistant_message(
|
|
content: str,
|
|
*,
|
|
reasoning_content: str | None = None,
|
|
thinking_blocks: list[dict[str, Any]] | None = None,
|
|
) -> dict[str, Any]:
|
|
"""Build a plain assistant turn, carrying its reasoning when there was any.
|
|
|
|
The tool-call builder above and this one exist for the same reason: a
|
|
thinking model's history has to keep the reasoning that produced each
|
|
assistant turn, or the provider refuses the continuation. Which of the two
|
|
a round needs depends only on whether it called tools.
|
|
"""
|
|
message: dict[str, Any] = {"role": "assistant", "content": content}
|
|
if reasoning_content:
|
|
message["reasoning_content"] = reasoning_content
|
|
if thinking_blocks:
|
|
message["thinking_blocks"] = thinking_blocks
|
|
return message
|
|
|
|
|
|
__all__ = ["assistant_message", "assistant_message_with_tool_calls"]
|