[Bugfix] Raise VLLMValidationError from structured output validators (#52394)

Signed-off-by: Jeffrey Wang <jeffreywang@anyscale.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jeffrey Wang
2026-08-16 01:04:51 -07:00
committed by GitHub
co-authored by Claude Opus 5
parent 8efa13b700
commit 41f12a0daf
8 changed files with 109 additions and 30 deletions
@@ -13,6 +13,7 @@ from pydantic import BaseModel
from tests.reasoning.utils import run_reasoning_extraction
from vllm.config import StructuredOutputsConfig
from vllm.exceptions import VLLMValidationError
from vllm.outputs import RequestOutput
from vllm.platforms import current_platform
from vllm.reasoning.abs_reasoning_parsers import ReasoningParserManager
@@ -326,7 +327,7 @@ def test_structured_output(
)
if backend.startswith("xgrammar"):
with pytest.raises(
ValueError,
VLLMValidationError,
match="The provided JSON schema contains features "
"not supported by xgrammar.",
):
@@ -453,7 +454,9 @@ def test_structured_output(
max_tokens=1000,
structured_outputs=StructuredOutputsParams(grammar="not a grammar"),
)
with pytest.raises(ValueError, match="Failed to convert the grammar "):
with pytest.raises(
VLLMValidationError, match="Failed to convert the grammar "
):
runner.llm.generate(
(
"Generate a sql statement that selects col_1 from "
+58 -1
View File
@@ -5,7 +5,7 @@
import pytest
from vllm.config import StructuredOutputsConfig
from vllm.exceptions import VLLMValidationError
from vllm.exceptions import VLLMClientError, VLLMValidationError
from vllm.sampling_params import SamplingParams, StructuredOutputsParams
pytestmark = pytest.mark.cpu_test
@@ -103,3 +103,60 @@ def test_regex_with_nul_byte_rejected(regex):
with pytest.raises(ValueError, match="NUL"):
validate_xgrammar_grammar(params)
INVALID_JSON_SCHEMA = {"type": "object", "properties": {"name": {"type": "str"}}}
@pytest.mark.parametrize(
"backend, structured_outputs",
[
("xgrammar", StructuredOutputsParams(json=INVALID_JSON_SCHEMA)),
("outlines", StructuredOutputsParams(json=INVALID_JSON_SCHEMA)),
("auto", StructuredOutputsParams(json=INVALID_JSON_SCHEMA)),
("auto", StructuredOutputsParams(json='{"type": ')),
("xgrammar", StructuredOutputsParams(grammar="not a grammar")),
("guidance", StructuredOutputsParams(grammar="not a grammar")),
("lm-format-enforcer", StructuredOutputsParams(grammar="not a grammar")),
("outlines", StructuredOutputsParams(regex="(")),
("guidance", StructuredOutputsParams(structural_tag='{"nope": 1}')),
],
)
def test_unsupported_grammar_is_a_client_error(backend, structured_outputs):
"""Only `VLLMClientError` survives `AsyncLLM.generate` untouched; anything else
is wrapped in `EngineGenerateError` and served as a 500 instead of a 400."""
params = SamplingParams(structured_outputs=structured_outputs)
with pytest.raises(VLLMClientError):
params._validate_structured_outputs(
_StubModelConfig(is_diffusion=False),
StructuredOutputsConfig(backend=backend),
tokenizer=object(),
)
@pytest.mark.parametrize(
"schema, expected_backend",
[
# multipleOf is unsupported by xgrammar, patternProperties also by guidance.
(
{
"type": "object",
"properties": {"n": {"type": "integer", "multipleOf": 2}},
},
"guidance",
),
(
{"type": "object", "patternProperties": {"^a": {"type": "string"}}},
"outlines",
),
],
)
def test_auto_backend_falls_back_on_unsupported_schema(schema, expected_backend):
"""`auto` falls back on rejection, so it must catch what the validators raise."""
params = SamplingParams(structured_outputs=StructuredOutputsParams(json=schema))
params._validate_structured_outputs(
_StubModelConfig(is_diffusion=False),
StructuredOutputsConfig(backend="auto"),
tokenizer=object(),
)
assert params.structured_outputs._backend == expected_backend