Skip to content

FreeToken brings datacenter-scale model serving to your desktop. Run massive models locally, fast and efficiently.

fix(server): recover Qwen tool calls after unclosed think (#825)

Julien Pierre075cf5729691
diff --git a/python/freetoken/server/reasoning_parser.py b/python/freetoken/server/reasoning_parser.py
index 411f9bfe..97339802 100644
--- a/python/freetoken/server/reasoning_parser.py
+++ b/python/freetoken/server/reasoning_parser.py
@@ -396,19 +396,42 @@ class ThinkReasoningParser(BaseReasoningParser):
chain-of-thought in think tags (Qwen3/3.5, GLM-4.x, MiniMax-M2). No DSML tool
marker — reasoning ends at ``</think>``.
- Assumes the model closes ``</think>`` before any tool call. These families do
- so in well-formed thinking mode; unlike dsv4 there is no ``tool_start_token``
- fallback, so a (malformed) turn that skips ``</think>`` and runs straight into
- a tool call would fold that block into reasoning. Add a family-appropriate
- ``tool_start_token`` (as ``DeepSeekV32ReasoningParser`` does) if that surfaces.
+ Families with a tool format that can follow an omitted ``</think>`` should
+ use a subclass with its protocol-specific ``tool_start_token``. That lets the
+ normal structured tool parser validate the block; it does not turn arbitrary
+ reasoning text into an executable call.
"""
- def __init__(self, force_reasoning: bool = False, stream_reasoning: bool = True) -> None:
+ def __init__(
+ self,
+ force_reasoning: bool = False,
+ stream_reasoning: bool = True,
+ *,
+ tool_start_token: Optional[str] = None,
+ ) -> None:
super().__init__(
think_start_token=THINK_START_TOKEN,
think_end_token=THINK_END_TOKEN,
force_reasoning=force_reasoning,
stream_reasoning=stream_reasoning,
+ tool_start_token=tool_start_token,
+ )
+
+
+class Qwen3ReasoningParser(ThinkReasoningParser):
+ """Qwen3 reasoning with recovery for an omitted ``</think>`` before XML tools.
+
+ Qwen's chat template starts generation inside ``<think>`` and asks for
+ ``<tool_call>`` XML. Models occasionally transition directly to that block.
+ Preserve the block for the Qwen tool-call parser, which still decides whether
+ it is a valid structured call.
+ """
+
+ def __init__(self, force_reasoning: bool = False, stream_reasoning: bool = True) -> None:
+ super().__init__(
+ force_reasoning=force_reasoning,
+ stream_reasoning=stream_reasoning,
+ tool_start_token="<tool_call>",
)
@@ -877,7 +900,7 @@ class ReasoningParser:
"flint": ThinkReasoningParser,
"deepseekv32": DeepSeekV32ReasoningParser,
"gpt_oss": GptOssHarmonyReasoningParser,
- "qwen3": ThinkReasoningParser,
+ "qwen3": Qwen3ReasoningParser,
"glm": ThinkReasoningParser,
"minimax": ThinkReasoningParser,
"minimax_m3": MiniMaxM3ReasoningParser,
diff --git a/tests/server/test_reasoning_parser_all_models.py b/tests/server/test_reasoning_parser_all_models.py
index 47e0f094..8be213c2 100644
--- a/tests/server/test_reasoning_parser_all_models.py
+++ b/tests/server/test_reasoning_parser_all_models.py
@@ -136,6 +136,22 @@ def test_think_no_reasoning_passthrough(name):
assert content == "just an answer"
+def test_qwen3_missing_think_closer_preserves_tool_block_for_tool_parser():
+ parser = ReasoningParser("qwen3", force_reasoning=True)
+ text = "I need a shell.<tool_call><function=run_command></function></tool_call>"
+ reasoning, content = parser.parse_non_stream(text)
+ assert reasoning == "I need a shell."
+ assert content == "<tool_call><function=run_command></function></tool_call>"
+
+
+def test_qwen3_closed_reasoning_keeps_literal_tool_marker_in_reasoning():
+ parser = ReasoningParser("qwen3", force_reasoning=True)
+ text = "Mention <tool_call> only as documentation.</think>Visible answer"
+ reasoning, content = parser.parse_non_stream(text)
+ assert reasoning == "Mention <tool_call> only as documentation."
+ assert content == "Visible answer"
+
+
# ----------------------------------------------------------------------- gemma
def test_gemma_thought_split():
parser = ReasoningParser("gemma4", force_reasoning=True)
diff --git a/tests/server/test_streaming_model_matrix.py b/tests/server/test_streaming_model_matrix.py
index 1ee79379..87341e68 100644
--- a/tests/server/test_streaming_model_matrix.py
+++ b/tests/server/test_streaming_model_matrix.py
@@ -322,6 +322,27 @@ def test_reasoning_generation_level(name):
assert [c.name for c in full.tool_calls] == ["read"]
+def test_qwen3_missing_think_closer_recovers_structured_tool_call():
+ # Qwen's template pre-opens <think>. A model can skip its closing tag and
+ # transition directly to a valid XML call; recover the boundary before the
+ # tool parser runs, without treating reasoning itself as executable.
+ text = THINKING + CALL_BLOCKS["qwen3_coder"]
+ events = _stream_events(_random_chunks(text, 23), "qwen3_coder", "qwen3")
+ _assert_stream_invariants(events, "qwen3_coder")
+ assert _reasoning(events).strip() == THINKING
+ calls = _calls(events)
+ assert len(calls) == 1
+ assert calls[0].name == "read"
+ assert json.loads(calls[0].parameters) == READ_ARGS
+ assert events[-1].finish_reason == "tool_calls"
+
+ full = _full_result(text, "qwen3_coder", "qwen3")
+ assert full.reasoning.strip() == THINKING
+ assert full.content == ""
+ assert [call.name for call in full.tool_calls] == ["read"]
+ assert full.finish_reason == "tool_calls"
+
+
async def _aiter(items):
for it in items:
yield it