[Refactor] Derive is_reasoning_end from the engine grammar (#56200)

Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
Flora Feng
2026-09-15 10:07:58 +00:00
committed by GitHub
parent 142020c6c2
commit 24bfbd5d4d
13 changed files with 111 additions and 117 deletions
+3 -30
View File
@@ -100,6 +100,7 @@ def qwen3_config(
return ParserEngineConfig(
name=name,
initial_state=ParserState.REASONING if thinking else ParserState.CONTENT,
wait_for_reasoning=thinking,
turn_boundary_tokens=turn_boundary_tokens,
terminals={
# Reasoning terminals
@@ -205,8 +206,8 @@ class Qwen3Parser(ParserEngine):
"""Qwen3 parser: ``<think>``/``</think>`` reasoning +
``<tool_call>`` XML tool calls in a single engine.
- ``<tool_call>`` as implicit reasoning end
- Unpaired ``<tool_call>`` token ID detection for ``is_reasoning_end``
- ``<tool_call>`` as implicit reasoning end (a grammar transition, so it
also feeds ``is_reasoning_end`` and the structured-output gate)
Subclasses that share the grammar but differ only in the four wrapper
token strings (reasoning + tool-call) override the class attributes
@@ -245,9 +246,6 @@ class Qwen3Parser(ParserEngine):
tools,
**kwargs,
)
vocab = self.vocab
self._tool_call_token_id: int | None = vocab.get(self.TOOL_START)
self._tool_call_end_token_id: int | None = vocab.get(self.TOOL_END)
def extract_reasoning(
self,
@@ -257,28 +255,3 @@ class Qwen3Parser(ParserEngine):
if not self.thinking_enabled:
return None, model_output
return super().extract_reasoning(model_output, request)
def is_reasoning_end(self, input_ids: list[int]) -> bool:
if super().is_reasoning_end(input_ids):
return True
tool_call_id = self._tool_call_token_id
tool_call_end_id = self._tool_call_end_token_id
reasoning_start_id = self._reasoning_start_token_id
boundary_ids = self._turn_boundary_token_ids
if tool_call_id is not None:
for i in range(len(input_ids) - 1, -1, -1):
if (
reasoning_start_id is not None
and input_ids[i] == reasoning_start_id
):
return False
if input_ids[i] in boundary_ids:
return False
if input_ids[i] == tool_call_id:
if tool_call_end_id is not None and any(
input_ids[j] == tool_call_end_id
for j in range(i + 1, len(input_ids))
):
continue
return True
return False