[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:
@@ -27,6 +27,8 @@ _THINK_START_ID = 50
|
||||
_THINK_END_ID = 51
|
||||
_TOOL_CALL_ID = 60
|
||||
_TOOL_CALL_END_ID = 61
|
||||
_IM_START_ID = 70
|
||||
_IM_END_ID = 71
|
||||
_TEXT_ID = 100
|
||||
|
||||
_QWEN3_VOCAB = {
|
||||
@@ -34,6 +36,8 @@ _QWEN3_VOCAB = {
|
||||
"</think>": _THINK_END_ID,
|
||||
"<tool_call>": _TOOL_CALL_ID,
|
||||
"</tool_call>": _TOOL_CALL_END_ID,
|
||||
"<|im_start|>": _IM_START_ID,
|
||||
"<|im_end|>": _IM_END_ID,
|
||||
}
|
||||
|
||||
|
||||
@@ -192,6 +196,72 @@ class TestIsReasoningEnd:
|
||||
assert not parser.is_reasoning_end([])
|
||||
|
||||
|
||||
class TestIsReasoningEndTurnBoundaries:
|
||||
"""Reasoning markers from earlier turns must not mark the new turn's
|
||||
reasoning as finished (think-marker injection / interleaved-thinking
|
||||
replay).
|
||||
"""
|
||||
|
||||
def test_injected_think_end_in_history_not_end(self, parser):
|
||||
assert not parser.is_reasoning_end(
|
||||
[_IM_START_ID, _TEXT_ID, _THINK_END_ID, _IM_END_ID, _IM_START_ID]
|
||||
)
|
||||
|
||||
def test_replayed_reasoning_block_in_history_not_end(self, parser):
|
||||
assert not parser.is_reasoning_end(
|
||||
[
|
||||
_IM_START_ID,
|
||||
_THINK_START_ID,
|
||||
_TEXT_ID,
|
||||
_THINK_END_ID,
|
||||
_TEXT_ID,
|
||||
_IM_END_ID,
|
||||
_IM_START_ID,
|
||||
]
|
||||
)
|
||||
|
||||
def test_think_end_in_current_turn_is_end(self, parser):
|
||||
assert parser.is_reasoning_end(
|
||||
[_IM_START_ID, _TEXT_ID, _IM_END_ID, _IM_START_ID, _THINK_END_ID]
|
||||
)
|
||||
|
||||
def test_unpaired_tool_call_in_history_not_end(self, parser):
|
||||
assert not parser.is_reasoning_end(
|
||||
[_TOOL_CALL_ID, _TEXT_ID, _IM_END_ID, _IM_START_ID]
|
||||
)
|
||||
|
||||
def test_continued_turn_with_closed_thinking_is_end(self, parser):
|
||||
assert parser.is_reasoning_end(
|
||||
[_IM_START_ID, _THINK_START_ID, _TEXT_ID, _THINK_END_ID, _TEXT_ID]
|
||||
)
|
||||
|
||||
def test_continued_turn_with_open_thinking_not_end(self, parser):
|
||||
assert not parser.is_reasoning_end([_IM_START_ID, _THINK_START_ID, _TEXT_ID])
|
||||
|
||||
def test_empty_boundary_config_keeps_global_walk(self):
|
||||
"""Configs without turn_boundary_tokens keep the pre-existing
|
||||
behavior: any </think> in the sequence ends reasoning."""
|
||||
cfg = qwen3_config(turn_boundary_tokens=frozenset())
|
||||
parser = Qwen3Parser(
|
||||
make_mock_tokenizer(_QWEN3_VOCAB), parser_engine_config=cfg
|
||||
)
|
||||
assert parser.is_reasoning_end(
|
||||
[_IM_START_ID, _TEXT_ID, _THINK_END_ID, _IM_END_ID, _IM_START_ID]
|
||||
)
|
||||
|
||||
def test_boundary_tokens_missing_from_vocab_keep_global_walk(self):
|
||||
"""Boundary tokens absent from the vocabulary resolve to nothing,
|
||||
so subclasses reusing the default on a non-ChatML vocab keep the
|
||||
pre-existing behavior."""
|
||||
vocab = {
|
||||
token: token_id
|
||||
for token, token_id in _QWEN3_VOCAB.items()
|
||||
if not token.startswith("<|im_")
|
||||
}
|
||||
parser = Qwen3Parser(make_mock_tokenizer(vocab))
|
||||
assert parser.is_reasoning_end([_TEXT_ID, _THINK_END_ID, _TEXT_ID])
|
||||
|
||||
|
||||
class TestDelegatingPromptDetection:
|
||||
def test_prompt_tool_example_does_not_skip_streaming_reasoning(
|
||||
self, mock_tokenizer, mock_request
|
||||
@@ -211,6 +281,56 @@ class TestDelegatingPromptDetection:
|
||||
assert delta.reasoning == "thinking"
|
||||
assert delta.content is None
|
||||
|
||||
def test_injected_think_end_in_history_keeps_streaming_reasoning(
|
||||
self, mock_tokenizer, mock_request
|
||||
):
|
||||
parser = _Qwen3DelegatingParser(mock_tokenizer)
|
||||
prompt_ids = [
|
||||
_IM_START_ID,
|
||||
_TEXT_ID,
|
||||
_THINK_END_ID,
|
||||
_TEXT_ID,
|
||||
_IM_END_ID,
|
||||
_IM_START_ID,
|
||||
]
|
||||
|
||||
delta = parser.parse_delta(
|
||||
"thinking",
|
||||
[_TEXT_ID],
|
||||
mock_request,
|
||||
prompt_token_ids=prompt_ids,
|
||||
finished=False,
|
||||
)
|
||||
|
||||
assert delta is not None
|
||||
assert delta.reasoning == "thinking"
|
||||
assert delta.content is None
|
||||
|
||||
def test_think_end_in_generation_prompt_still_seeds_reasoning_ended(
|
||||
self, mock_tokenizer, mock_request
|
||||
):
|
||||
parser = _Qwen3DelegatingParser(mock_tokenizer)
|
||||
prompt_ids = [
|
||||
_IM_START_ID,
|
||||
_TEXT_ID,
|
||||
_IM_END_ID,
|
||||
_IM_START_ID,
|
||||
_THINK_START_ID,
|
||||
_THINK_END_ID,
|
||||
]
|
||||
|
||||
delta = parser.parse_delta(
|
||||
"answer",
|
||||
[_TEXT_ID],
|
||||
mock_request,
|
||||
prompt_token_ids=prompt_ids,
|
||||
finished=False,
|
||||
)
|
||||
|
||||
assert delta is not None
|
||||
assert delta.reasoning is None
|
||||
assert delta.content == "answer"
|
||||
|
||||
|
||||
class TestStreaming:
|
||||
def test_basic_streaming(self, parser):
|
||||
|
||||
Reference in New Issue
Block a user