Skip to content

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

fix(chat): enable native tool calling for reasoning models via OpenAI-compatible endpoints (#528)

wedoneb2f2002a0a62
diff --git a/deeptutor/agents/chat/agentic_pipeline.py b/deeptutor/agents/chat/agentic_pipeline.py
index f139983b3..2828b3697 100644
--- a/deeptutor/agents/chat/agentic_pipeline.py
+++ b/deeptutor/agents/chat/agentic_pipeline.py
@@ -73,9 +73,11 @@ from deeptutor.services.llm import (
clean_thinking_tags,
get_llm_config,
get_token_limit_kwargs, # noqa: F401 (re-exported for tests)
+ has_thinking_tags,
prepare_multimodal_messages,
supports_tools, # noqa: F401 (re-exported for tests)
)
+from deeptutor.services.provider_registry import find_by_name
from deeptutor.services.llm import (
stream as llm_stream,
)
@@ -378,11 +380,14 @@ class AgenticChatPipeline:
enabled_tools = self._compose_enabled_tools(context)
use_native_tools = bool(enabled_tools) and self._can_use_native_tool_calling()
+ _is_reasoner = has_thinking_tags(self.binding, self.model)
tool_schemas = (
self._build_llm_tool_schemas(enabled_tools, context) if use_native_tools else None
)
system_prompt = self._build_system_prompt(enabled_tools, context)
+ if _is_reasoner and use_native_tools:
+ system_prompt = self._append_reasoner_protocol_note(system_prompt)
user_content = self._t(
"user_template",
default=context.user_message,
@@ -421,13 +426,39 @@ class AgenticChatPipeline:
# call_id so it does NOT spawn its own sub-trace; each LLM iteration
# and each tool call below allocate their own call_id and surface as
# individual sub-traces in CallTracePanel.
+ completion_kwargs = self._completion_kwargs(max_tokens=self._responding_max_tokens)
+
+ # Reasoning models (e.g. Qwen3.6-Plus) need ``enable_thinking``
+ # explicitly set so the API correctly handles thinking + tool_calls.
+ # When the binding already has a ``thinking_style`` (e.g. dashscope),
+ # ``build_provider_extra_kwargs`` handles this. But when the model is
+ # accessed via a generic binding (e.g. custom / OpenAI-compatible)
+ # that lacks ``thinking_style``, we must inject it here.
+ if _is_reasoner and use_native_tools:
+ _spec = find_by_name(self.binding)
+ if not (_spec and _spec.thinking_style):
+ completion_kwargs.setdefault("extra_body", {})["enable_thinking"] = True
+
+ # When native tool calling is unavailable (no tool schemas), strip
+ # the TOOL label from the protocol so the loop does not expect
+ # ``tool_calls`` deltas and trigger ``tool_without_calls`` violations.
+ protocol = _CHAT_PROTOCOL
+ if not use_native_tools and protocol.tool_label is not None:
+ protocol = LabelProtocol(
+ allowed=tuple(l for l in protocol.allowed if l != LABEL_TOOL),
+ terminal=protocol.terminal,
+ intermediate=protocol.intermediate,
+ final=protocol.final,
+ tool_label=None,
+ )
+
async with stream.stage("responding", source="chat"):
outcome = await run_agentic_loop(
initial_messages=messages,
- protocol=_CHAT_PROTOCOL,
+ protocol=protocol,
client=client,
model=self.model,
- completion_kwargs=self._completion_kwargs(max_tokens=self._responding_max_tokens),
+ completion_kwargs=completion_kwargs,
binding=self.binding,
tool_schemas=tool_schemas,
stream=stream,
@@ -436,12 +467,16 @@ class AgenticChatPipeline:
max_iterations=max(1, self._max_iterations),
host=host,
usage=self._usage,
- # Reasoning models that natively emit ``<think>...</think>``
- # without parroting back ``\`\`THINK\`\``` are gracefully
- # accepted as a THINK iteration rather than treated as a
- # protocol violation (which would burn budget on repair
- # retries that the model can't actually satisfy).
- implicit_think_label=LABEL_THINK,
+ # Reasoning models with native tool-calling support emit
+ # ``reasoning_content`` without parroting back
+ # ````THINK```` labels. When the model is a reasoner
+ # AND native tool calling is active, use LABEL_FINISH so
+ # that a reply containing ``reasoning_content`` + answer
+ # text (but no explicit label) terminates the loop — the
+ # answer lives in ``content``, reasoning in
+ # ``reasoning_content``. Non-reasoning models that emit
+ # ``<think/>`` tags get LABEL_THINK so the loop continues.
+ implicit_think_label=LABEL_FINISH if (_is_reasoner and use_native_tools) else LABEL_THINK,
)
if outcome.sources:
@@ -1217,6 +1252,40 @@ class AgenticChatPipeline:
)
return append_language_directive(system, self.language)
+ def _append_reasoner_protocol_note(self, system_prompt: str) -> str:
+ """Append a note for reasoning models that support native tool calling.
+
+ Reasoning models (e.g. Qwen3.6-Plus) emit reasoning via
+ ``reasoning_content`` and tool calls via native ``tool_calls``
+ deltas. The label protocol (TOOL/THINK/FINISH/PAUSE) confuses
+ them into writing tool-call JSON in ``content`` instead of using
+ the native mechanism. This note tells them to ignore labels and
+ use native tool calling directly.
+ """
+ if self.language == "zh":
+ note = (
+ "\n\n# 推理模型特别说明\n"
+ "你是一个原生支持推理和工具调用的模型。"
+ "请**忽略上面「输出协议」中关于 ``TOOL``/``THINK``/``FINISH``/``PAUSE`` 标签的指示**。"
+ "你不需要在回复中输出任何标签。"
+ "你的推理过程会自动在独立区域显示。"
+ "当你需要调用工具时,直接通过原生 tool_calls 功能发起调用,不要在文本中写 JSON。"
+ "当你准备好给出最终答案时,直接在回复正文中写出答案即可。"
+ )
+ else:
+ note = (
+ "\n\n# Reasoning Model Special Instructions\n"
+ "You are a model with native reasoning and tool-calling support. "
+ "Please **ignore the 'Output Protocol' instructions above about "
+ "``TOOL``/``THINK``/``FINISH``/``PAUSE`` labels**. "
+ "You do NOT need to output any labels in your replies. "
+ "Your reasoning is automatically displayed in a separate area. "
+ "When you need to call a tool, use native tool_calls directly — "
+ "do NOT write JSON in your text output. "
+ "When you are ready to give the final answer, just write it in your reply body."
+ )
+ return system_prompt + note
+
def _build_messages(
self,
*,
diff --git a/deeptutor/services/llm/capabilities.py b/deeptutor/services/llm/capabilities.py
index 1731324a6..c56fba082 100644
--- a/deeptutor/services/llm/capabilities.py
+++ b/deeptutor/services/llm/capabilities.py
@@ -29,6 +29,15 @@ PROVIDER_CAPABILITIES: dict[str, dict[str, object]] = {
"system_in_messages": True, # System prompt goes in messages array
"newer_models_use_max_completion_tokens": True,
},
+ # Custom / user-defined OpenAI-compatible endpoints
+ "custom": {
+ "supports_response_format": True,
+ "supports_streaming": True,
+ "supports_tools": True, # Most OpenAI-compat endpoints support function calling
+ "supports_vision": False, # Per-model; set True via MODEL_OVERRIDES
+ "system_in_messages": True,
+ "has_thinking_tags": False, # Per-model; MODEL_OVERRIDES handles qwen/deepseek etc.
+ },
"azure_openai": {
"supports_response_format": True,
"supports_streaming": True,
@@ -105,6 +114,16 @@ PROVIDER_CAPABILITIES: dict[str, dict[str, object]] = {
"supports_vision": True,
"system_in_messages": True,
},
+ # DashScope / Alibaba Cloud (Qwen family)
+ # Uses OpenAI-compatible API with native function calling support.
+ "dashscope": {
+ "supports_response_format": True,
+ "supports_streaming": True,
+ "supports_tools": True,
+ "supports_vision": False, # Per-model; set True via MODEL_OVERRIDES
+ "system_in_messages": True,
+ "has_thinking_tags": True, # Qwen reasoner models emit <think/> tags
+ },
# Moonshot / Kimi — vision is per-model (see MODEL_OVERRIDES below).
# Per the official docs the image input must be base64-encoded inline; URL
# form is rejected. We therefore force the multimodal layer to resolve any