[Structured Output] Keep invalid structured-output requests from stopping the engine (#51450)

Co-authored-by: Lucas Bourtoule <35483370+dhalf@users.noreply.github.com>
Co-authored-by: Vadim Gimpelson <156319763+vadiklyutiy@users.noreply.github.com>
This commit is contained in:
Clinton Thomas
2026-09-09 15:32:01 +00:00
committed by GitHub
co-authored by Lucas Bourtoule Vadim Gimpelson
parent 7e95735eb9
commit c55e15a44e
6 changed files with 193 additions and 0 deletions
+3
View File
@@ -71,6 +71,7 @@ from vllm.v1.request import Request, RequestStatus, StreamingUpdate
from vllm.v1.spec_decode.dynamic.utils import build_dynamic_sd_schedule_lookup
from vllm.v1.spec_decode.metrics import SpecDecodingStats
from vllm.v1.structured_output import StructuredOutputGrammar, StructuredOutputManager
from vllm.v1.structured_output.utils import strip_speculative_padding
from vllm.v1.utils import record_function_or_nullcontext
logger = init_logger(__name__)
@@ -2424,6 +2425,7 @@ class Scheduler(SchedulerInterface):
# Add newly generated spec token ids to the request.
if self.structured_output_manager.should_advance(request):
metadata = request.structured_output_request
spec_token_ids = strip_speculative_padding(spec_token_ids)
spec_token_ids = metadata.grammar.validate_tokens(spec_token_ids) # type: ignore[union-attr]
request.spec_token_ids = spec_token_ids
@@ -2453,6 +2455,7 @@ class Scheduler(SchedulerInterface):
# Filter out spec tokens which do not adhere to the grammar.
if self.structured_output_manager.should_advance(request):
metadata = request.structured_output_request
spec_token_ids = strip_speculative_padding(spec_token_ids)
spec_token_ids = metadata.grammar.validate_tokens(spec_token_ids) # type: ignore[union-attr]
# Pad to original number of spec tokens.
num_invalid_tokens = orig_num_spec_tokens - len(spec_token_ids)
@@ -21,6 +21,7 @@ from vllm.v1.structured_output.backend_types import (
StructuredOutputOptions,
)
from vllm.v1.structured_output.request import get_structured_output_key
from vllm.v1.structured_output.utils import strip_speculative_padding
if TYPE_CHECKING:
import llguidance
@@ -198,6 +199,10 @@ class GuidanceGrammar(StructuredOutputGrammar):
if self.ll_matcher.is_stopped():
return []
tokens = strip_speculative_padding(tokens)
if len(tokens) == 0:
return []
num_tokens = self.ll_matcher.validate_tokens(tokens)
self.check_error()
+14
View File
@@ -46,6 +46,20 @@ _T = TypeVar("_T")
CACHE = None
def strip_speculative_padding(token_ids: list[int]) -> list[int]:
"""Drop speculative-decoding padding from a token block.
ngram and other speculative backends pad rejected draft positions with a
-1 sentinel. Structured-output grammars treat every entry as a real token
id, so the sentinels (and everything after the first one) are removed here,
before tokens reach any backend, rather than inside a single backend.
"""
for i, token_id in enumerate(token_ids):
if token_id < 0:
return token_ids[:i]
return token_ids
def compile_regex_with_timeout(fn: Callable[[str], _T], pattern: str) -> _T:
"""Run a regex compilation callable with a timeout.