From 3c84ad13ed3f9fcd816bf1d15acb83c28cfa02f9 Mon Sep 17 00:00:00 2001 From: CaiJohn Date: Mon, 21 Sep 2026 14:59:20 +0800 Subject: [PATCH] [Bugfix] Fix xgrammar feature gate bypass when JSON Schema type is a list (#48416) Signed-off-by: john <6712432+CaiJohn@users.noreply.github.com> Co-authored-by: john <6712432+CaiJohn@users.noreply.github.com> Co-authored-by: Codex --- tests/v1/structured_output/test_utils.py | 46 +++++++++++++++++++ tests/v1/structured_output/test_validation.py | 15 ++++++ vllm/v1/structured_output/backend_xgrammar.py | 28 ++++++++--- 3 files changed, 82 insertions(+), 7 deletions(-) diff --git a/tests/v1/structured_output/test_utils.py b/tests/v1/structured_output/test_utils.py index 3e4a12f7e5..ff2b58dfe1 100644 --- a/tests/v1/structured_output/test_utils.py +++ b/tests/v1/structured_output/test_utils.py @@ -224,6 +224,52 @@ def test_supported_json_features(schema_type, request): ) +class TestHasXGrammarUnsupportedJsonFeatures: + class TestPR48416Regressions: + @pytest.mark.parametrize( + "schema", + [ + {"type": ["number"], "multipleOf": 3}, + { + "type": ["string", "null"], + "pattern": "^a+$", + "maxLength": 2, + }, + { + "type": ["array", "null"], + "items": {"type": "integer"}, + "uniqueItems": True, + }, + { + "type": ["object", "null"], + "properties": {"Bad": {"type": "integer"}}, + "propertyNames": {"pattern": "^[a-z]+$"}, + }, + ], + ) + def test_list_type_does_not_bypass_unsupported_feature_check(self, schema): + assert has_xgrammar_unsupported_json_features(schema) + + @pytest.mark.parametrize( + "schema", + [ + {"type": ["string", "null"]}, + {"type": ["integer", "string"]}, + {"type": ["array", "null"], "items": {"type": "string"}}, + { + "type": ["object", "null"], + "propertyNames": {"pattern": "^[a-z_]+$"}, + }, + { + "type": ["object", "null"], + "patternProperties": {"^S": {"type": "string"}}, + }, + ], + ) + def test_supported_list_type_json_features(self, schema): + assert not has_xgrammar_unsupported_json_features(schema) + + def test_property_names_constrains_keys(property_names_schema): assert grammar_accepts(property_names_schema, '{"score": 5}') assert grammar_accepts(property_names_schema, '{"score": "seven"}') diff --git a/tests/v1/structured_output/test_validation.py b/tests/v1/structured_output/test_validation.py index a231894384..a82b3e5117 100644 --- a/tests/v1/structured_output/test_validation.py +++ b/tests/v1/structured_output/test_validation.py @@ -145,6 +145,21 @@ def test_unsupported_grammar_is_a_client_error(backend, structured_outputs): }, "guidance", ), + ( + { + "type": "object", + "properties": {"n": {"type": ["number", "null"], "multipleOf": 3}}, + }, + "guidance", + ), + ( + { + "type": ["string", "null"], + "pattern": "^a+$", + "maxLength": 2, + }, + "guidance", + ), # patternProperties + properties is also unsupported by guidance. ( { diff --git a/vllm/v1/structured_output/backend_xgrammar.py b/vllm/v1/structured_output/backend_xgrammar.py index 1ba7e2734d..3c48ff64a0 100644 --- a/vllm/v1/structured_output/backend_xgrammar.py +++ b/vllm/v1/structured_output/backend_xgrammar.py @@ -242,6 +242,18 @@ def _has_pattern_and_length_bounds(schema: dict[str, Any]) -> bool: ) +# FIXME(arpera): The approach used here needs to be redesigned because of +# existing bugs: https://github.com/vllm-project/vllm/issues/57550 +def _schema_types(schema: dict[str, Any]) -> set[str]: + """Normalize a scalar or list-valued JSON Schema type.""" + schema_type = schema.get("type") + if isinstance(schema_type, str): + return {schema_type} + if isinstance(schema_type, list): + return {item for item in schema_type if isinstance(item, str)} + return set() + + def has_xgrammar_unsupported_json_features(schema: dict[str, Any]) -> bool: """Check if JSON schema contains features unsupported by xgrammar.""" @@ -249,12 +261,14 @@ def has_xgrammar_unsupported_json_features(schema: dict[str, Any]) -> bool: if not isinstance(obj, dict): return False + schema_types = _schema_types(obj) + # Check for numeric ranges - if obj.get("type") in ("integer", "number") and ("multipleOf" in obj): + if (schema_types & {"integer", "number"}) and ("multipleOf" in obj): return True # Check for array unsupported keywords - if obj.get("type") == "array" and any( + if "array" in schema_types and any( key in obj for key in ("uniqueItems", "contains", "minContains", "maxContains") ): @@ -262,7 +276,7 @@ def has_xgrammar_unsupported_json_features(schema: dict[str, Any]) -> bool: # Unsupported keywords for strings if ( - obj.get("type") == "string" + "string" in schema_types and "format" in obj and obj["format"] not in STRING_SUPPORTED_FORMATS ): @@ -275,13 +289,13 @@ def has_xgrammar_unsupported_json_features(schema: dict[str, Any]) -> bool: # the compiled EBNF: pattern/format grammars come out byte-identical # with and without the length keywords, while maxLength alone lowers # to {0, N} correctly. - if obj.get("type") == "string" and _has_pattern_and_length_bounds(obj): + if "string" in schema_types and _has_pattern_and_length_bounds(obj): return True # propertyNames validates names, so it is a string schema even when it # omits "type", which is the form that escapes the check above. if ( - obj.get("type") == "object" + "object" in schema_types and isinstance(obj.get("propertyNames"), dict) and _has_pattern_and_length_bounds(obj["propertyNames"]) ): @@ -291,7 +305,7 @@ def has_xgrammar_unsupported_json_features(schema: dict[str, Any]) -> bool: # additionalProperties/unevaluatedProperties under xgrammar. # https://github.com/mlc-ai/xgrammar/issues/826 if ( - obj.get("type") == "object" + "object" in schema_types and "propertyNames" in obj and ( "properties" in obj @@ -305,7 +319,7 @@ def has_xgrammar_unsupported_json_features(schema: dict[str, Any]) -> bool: # FIXME: multiple patternProperties, or patternProperties alongside # properties, conflict under xgrammar. if ( - obj.get("type") == "object" + "object" in schema_types and isinstance(obj.get("patternProperties"), dict) and ("properties" in obj or len(obj["patternProperties"]) > 1) ):