[Bugfix][Structured Output] Feed the token that implicitly ends reasoning to the grammar (#59627)

Signed-off-by: Simon Veitner <sveitner@redhat.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Misha Goin <mgoin64@gmail.com>
Co-authored-by: Yifan Zong <263738792+yzong-rh@users.noreply.github.com>
This commit is contained in:
simon-veitner-redhat
2026-10-08 19:38:33 +00:00
committed by GitHub
co-authored by Claude Opus 5.5 Misha Goin Yifan Zong
parent bab0ac61f8
commit c23ca06b6d
7 changed files with 150 additions and 52 deletions
+36 -9
View File
@@ -28,12 +28,13 @@ from vllm.entrypoints.openai.chat_completion.protocol import (
from vllm.parser.abstract_parser import DelegatingParser
from vllm.parser.engine.adapters import make_adapters
from vllm.parser.engine.events import EventType, SemanticEvent
from vllm.parser.engine.parser_engine import ParserEngine
from vllm.parser.engine.parser_engine import ParserEngine, ReasoningEnd
from vllm.parser.engine.parser_engine_config import (
ParserEngineConfig,
ParserState,
Transition,
)
from vllm.parser.glm47_moe import glm47_moe_config
from vllm.parser.parser_manager import ParserManager
# ── Shared test configs ──────────────────────────────────────────────
@@ -158,7 +159,10 @@ class TestReasoningEndTokenIds:
(EventType.REASONING_END, EventType.TOOL_CALL_START),
),
)
assert _make_engine(cfg).reasoning_end_token_ids == {201, 202}
engine = _make_engine(cfg)
assert engine.reasoning_end_token_ids == {201, 202}
assert engine.find_reasoning_end([5, 201]) == ReasoningEnd(1, False)
assert engine.find_reasoning_end([5, 202]) == ReasoningEnd(1, True)
def test_transition_staying_in_reasoning_is_ignored(self):
cfg = _with_reasoning_exits(("THINK_START", ParserState.REASONING, ()))
@@ -182,17 +186,40 @@ class TestReasoningEndTokenIds:
def test_config_without_reasoning_has_empty_set(self):
assert _make_engine(_hermes_config()).reasoning_end_token_ids == frozenset()
def test_find_reasoning_end_offset_returns_first_match(self):
def test_find_reasoning_end_returns_first_match(self):
engine = _make_engine()
assert engine.find_reasoning_end_offset([5, 201, 6, 201]) == 1
assert engine.find_reasoning_end_offset([5, 6]) == 2
assert engine.find_reasoning_end_offset([]) == 0
assert engine.find_reasoning_end([5, 201, 6, 201]) == ReasoningEnd(1, False)
assert engine.find_reasoning_end([5, 6]) == ReasoningEnd(2, False)
assert engine.find_reasoning_end([]) == ReasoningEnd(0, False)
# Rejected-draft placeholders never match.
assert engine.find_reasoning_end_offset([-1, 201]) == 1
assert engine.find_reasoning_end([-1, 201]) == ReasoningEnd(1, False)
def test_find_reasoning_end_offset_with_empty_set_returns_none(self):
def test_find_reasoning_end_with_empty_set_returns_none(self):
engine = _make_engine(_hermes_config())
assert engine.find_reasoning_end_offset([201]) is None
assert engine.find_reasoning_end([201]) is None
@pytest.mark.parametrize(
("config", "implicit_token"),
[
pytest.param(glm47_moe_config(), "<tool_call>", id="glm47_moe"),
],
)
def test_implicit_reasoning_end_of_parsers(
config: ParserEngineConfig, implicit_token: str
):
literals = sorted(set(config.token_id_terminals.values()))
vocab = {literal: 1000 + i for i, literal in enumerate(literals)}
engine = _make_engine(config, vocab=vocab)
implicit_ids = set()
for token_id in engine.reasoning_end_token_ids:
end = engine.find_reasoning_end([5, token_id, 6])
assert end is not None and end.offset == 1
kept = engine.extract_content_ids([5, token_id, 6]) == [token_id, 6]
assert end.implicit == kept
if end.implicit:
implicit_ids.add(token_id)
assert implicit_ids == {vocab[implicit_token]}
# ── TestEventsToDelta ────────────────────────────────────────────────