[Bugfix][Parser] Scope reasoning-end detection to the current turn via turn-boundary tokens (#54089)

Signed-off-by: Mikhail Podvitskii <podvitskiymichael@gmail.com>
This commit is contained in:
Mikhail Podvitskii
2026-08-27 16:57:32 -04:00
committed by GitHub
parent 7d5769b2d9
commit 3a9bfc209f
5 changed files with 147 additions and 4 deletions
+8
View File
@@ -42,6 +42,7 @@ THINK_START = "<think>"
THINK_END = "</think>"
TOOL_CALL_START = "<tool_call>"
TOOL_CALL_END = "</tool_call>"
CHATML_TURN_BOUNDARIES = frozenset(("<|im_start|>", "<|im_end|>"))
FUNC_PREFIX = "<function="
FUNC_END = "</function>"
PARAM_START = "<parameter="
@@ -94,10 +95,12 @@ def qwen3_config(
think_end: str = THINK_END,
tool_start: str = TOOL_CALL_START,
tool_end: str = TOOL_CALL_END,
turn_boundary_tokens: frozenset[str] = frozenset(),
) -> ParserEngineConfig:
return ParserEngineConfig(
name=name,
initial_state=ParserState.REASONING if thinking else ParserState.CONTENT,
turn_boundary_tokens=turn_boundary_tokens,
terminals={
# Reasoning terminals
"THINK_START": think_start,
@@ -215,6 +218,7 @@ class Qwen3Parser(ParserEngine):
THINK_END = THINK_END
TOOL_START = TOOL_CALL_START
TOOL_END = TOOL_CALL_END
TURN_BOUNDARIES: frozenset[str] = CHATML_TURN_BOUNDARIES
def __init__(
self,
@@ -233,6 +237,7 @@ class Qwen3Parser(ParserEngine):
think_end=self.THINK_END,
tool_start=self.TOOL_START,
tool_end=self.TOOL_END,
turn_boundary_tokens=self.TURN_BOUNDARIES,
),
)
super().__init__(
@@ -259,6 +264,7 @@ class Qwen3Parser(ParserEngine):
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 (
@@ -266,6 +272,8 @@ class Qwen3Parser(ParserEngine):
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