[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:
co-authored by
Artem Perevedentsev
parent
7173c64e2e
commit
54db759eea
@@ -159,7 +159,7 @@ def validate_structured_output_request_lm_format_enforcer(params: SamplingParams
|
||||
|
||||
so_params = params.structured_outputs
|
||||
|
||||
if so_params.regex:
|
||||
if so_params.regex is not None:
|
||||
try:
|
||||
compile_regex_with_timeout(
|
||||
lmformatenforcer.RegexParser,
|
||||
@@ -170,7 +170,7 @@ def validate_structured_output_request_lm_format_enforcer(params: SamplingParams
|
||||
f"Failed to compile regex for lm-format-enforcer: {err}"
|
||||
) from err
|
||||
return
|
||||
elif so_params.json:
|
||||
elif so_params.json is not None:
|
||||
if isinstance(so_params.json, str):
|
||||
try:
|
||||
# make sure schema is valid json
|
||||
@@ -185,10 +185,20 @@ def validate_structured_output_request_lm_format_enforcer(params: SamplingParams
|
||||
f"Error serializing structured outputs jsonschema: {e}"
|
||||
) from e
|
||||
return
|
||||
elif so_params.choice:
|
||||
elif so_params.choice or so_params.json_object:
|
||||
return
|
||||
elif so_params.grammar:
|
||||
raise VLLMValidationError(
|
||||
"LM Format Enforcer structured outputs backend "
|
||||
"does not support grammar specifications"
|
||||
)
|
||||
elif so_params.structural_tag:
|
||||
raise VLLMValidationError(
|
||||
"LM Format Enforcer structured outputs backend "
|
||||
"does not support structural_tag specifications"
|
||||
)
|
||||
else:
|
||||
raise VLLMValidationError(
|
||||
"LM Format Enforcer structured outputs backend "
|
||||
"does not support this structured outputs specification"
|
||||
)
|
||||
|
||||
@@ -198,9 +198,9 @@ def validate_structured_output_request_outlines(params: SamplingParams):
|
||||
|
||||
so_params = params.structured_outputs
|
||||
|
||||
if so_params.regex:
|
||||
if so_params.regex is not None:
|
||||
validate_regex_is_buildable(so_params.regex)
|
||||
elif so_params.json:
|
||||
elif so_params.json is not None:
|
||||
if isinstance(so_params.json, str):
|
||||
try:
|
||||
# make sure schema is valid json
|
||||
@@ -236,6 +236,16 @@ def validate_structured_output_request_outlines(params: SamplingParams):
|
||||
"Outlines structured outputs backend "
|
||||
"does not support grammar specifications"
|
||||
)
|
||||
elif so_params.structural_tag:
|
||||
raise VLLMValidationError(
|
||||
"Outlines structured outputs backend "
|
||||
"does not support structural_tag specifications"
|
||||
)
|
||||
else:
|
||||
raise VLLMValidationError(
|
||||
"Outlines structured outputs backend "
|
||||
"does not support this structured outputs specification"
|
||||
)
|
||||
|
||||
|
||||
def _prefix_needs_context(parsed) -> bool:
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user