[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
+12 -4
View File
@@ -9,7 +9,7 @@ from __future__ import annotations
import json
from collections.abc import Sequence
from functools import cached_property
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, NamedTuple
import regex as re
@@ -45,6 +45,13 @@ if TYPE_CHECKING:
logger = init_logger(__name__)
class ReasoningEnd(NamedTuple):
"""Where reasoning ends in a token sequence."""
offset: int # index of the first end token, or the sequence length
implicit: bool # the end token is also content, e.g. a tool-call start
class ToolCallSlot:
__slots__ = (
"id",
@@ -638,14 +645,15 @@ class ParserEngine(Parser):
def reasoning_end_token_ids(self) -> frozenset[int]:
return self._reasoning_end_token_ids
def find_reasoning_end_offset(self, token_ids: Sequence[int]) -> int | None:
def find_reasoning_end(self, token_ids: Sequence[int]) -> ReasoningEnd | None:
"""First reasoning end in `token_ids`, and if its token is content."""
end_ids = self._reasoning_end_token_ids
if not end_ids:
return None
for offset, token_id in enumerate(token_ids):
if token_id in end_ids:
return offset
return len(token_ids)
return ReasoningEnd(offset, token_id != self._reasoning_end_token_id)
return ReasoningEnd(len(token_ids), False)
def is_reasoning_end(self, input_ids: list[int]) -> bool:
config = self.parser_engine_config