[Bugfix][Structured Output] Avoid spurious FSM errors after speculative reasoning end (#53046)

Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
This commit is contained in:
Chauncey
2026-08-21 10:01:06 +08:00
committed by GitHub
parent 83c5d59209
commit c6e19b3be2
2 changed files with 8 additions and 2 deletions
@@ -189,7 +189,7 @@ def test_bitmask_constrained_when_reasoning_ends_midwindow(backend):
@pytest.mark.parametrize("backend", ["xgrammar", "guidance"])
def test_bitmask_post_reasoning_end_drafts_skip_grammar_advance(backend):
def test_bitmask_post_reasoning_end_drafts_skip_grammar_advance(backend, caplog):
"""Post-marker drafts predate the bitmask and may be grammar-invalid;
grammar_bitmask must skip the grammar advance instead of asserting.
"""
@@ -235,6 +235,7 @@ def test_bitmask_post_reasoning_end_drafts_skip_grammar_advance(backend):
assert not (bitmask[2] == -1).all()
# Grammar must not have advanced through the unvalidated draft.
assert not grammar.is_terminated()
assert "Failed to advance FSM" not in caplog.text
@pytest.mark.parametrize("backend", ["xgrammar", "guidance"])
+6 -1
View File
@@ -326,7 +326,12 @@ class StructuredOutputManager:
advance_grammar = False
post_reasoning_end_in_window = True
if advance_grammar and not grammar.is_terminated():
accepted = grammar.accept_tokens(req_id, [token])
if post_reasoning_end_in_window:
accepted = bool(grammar.validate_tokens([token]))
if accepted:
accepted = grammar.accept_tokens(req_id, [token])
else:
accepted = grammar.accept_tokens(req_id, [token])
if accepted:
state_advancements += 1
elif not post_reasoning_end_in_window: