[Bugfix][Structured Output] Reject structural_tag and unhandled specs in backend validation (#58862)

Signed-off-by: Samay <samay.piduru@gmail.com>
Co-authored-by: Artem Perevedentsev <aperevedents@nvidia.com>
This commit is contained in:
samayp01
2026-10-13 13:27:34 +03:00
committed by GitHub
co-authored by Artem Perevedentsev
parent 7173c64e2e
commit 54db759eea
4 changed files with 94 additions and 7 deletions
+12 -2
View File
@@ -414,7 +414,7 @@ def validate_xgrammar_grammar(sampling_params: SamplingParams) -> None:
so_params = sampling_params.structured_outputs
if so_params.regex:
if so_params.regex is not None:
# A NUL byte is never meaningful in a regex pattern and is not handled
# by xgrammar's native regex converter. Reject it here, before the
# pattern reaches that native code; the try/except below does not cover
@@ -432,6 +432,7 @@ def validate_xgrammar_grammar(sampling_params: SamplingParams) -> None:
raise VLLMValidationError(
f"Failed to transform regex into a grammar: {err}"
) from err
return
if so_params.choice:
choice_grammar = choice_as_grammar(so_params.choice)
@@ -445,7 +446,7 @@ def validate_xgrammar_grammar(sampling_params: SamplingParams) -> None:
so_params.grammar = choice_grammar
return
if so_params.json:
if so_params.json is not None:
check_json_nesting(so_params.json)
if isinstance(so_params.json, str):
try:
@@ -470,6 +471,9 @@ def validate_xgrammar_grammar(sampling_params: SamplingParams) -> None:
) from err
return
if so_params.json_object:
return
if so_params.grammar:
# Parse the grammar with the same syntax `compile_grammar` will use,
# but don't compile it. The grammar is passed on unchanged.
@@ -502,3 +506,9 @@ def validate_xgrammar_grammar(sampling_params: SamplingParams) -> None:
xgr.Grammar.from_structural_tag(so_params.structural_tag)
except Exception as e:
raise VLLMValidationError("Invalid structural tag specification.") from e
return
raise VLLMValidationError(
"XGrammar structured outputs backend "
"does not support this structured outputs specification"
)