22# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
33
44import pytest
5+ from xgrammar import Grammar
6+ from xgrammar .testing import _is_grammar_accept_string
57
68from vllm .v1 .structured_output .backend_xgrammar import (
79 has_xgrammar_unsupported_json_features ,
1012pytestmark = pytest .mark .cpu_test
1113
1214
15+ def grammar_accepts (schema : dict , text : str ) -> bool :
16+ return _is_grammar_accept_string (Grammar .from_json_schema (schema ), text )
17+
18+
1319@pytest .fixture
1420def unsupported_string_schemas ():
1521 return [
1622 {"type" : "string" , "format" : "non_existing_format" },
23+ # TODO(arpera):
1724 # pattern/format is compiled but length bounds are silently dropped,
1825 # so the combination must be rejected instead of producing quietly
1926 # wrong output
27+ # https://github.com/mlc-ai/xgrammar/issues/749
2028 {"type" : "string" , "pattern" : "^a+$" , "maxLength" : 2 },
2129 {"type" : "string" , "pattern" : "^a+$" , "minLength" : 3 },
2230 {"type" : "string" , "format" : "email" , "maxLength" : 10 },
@@ -48,15 +56,93 @@ def unsupported_array_schemas():
4856
4957
5058@pytest .fixture
51- def unsupported_object_schemas ():
59+ def unsupported_property_names_combinations ():
60+ return [
61+ # TODO(arpera):
62+ # this case is not covered by xgrammar, so we should report this bug to xgrammar
63+ # xgrammar drops propertyNames whenever patternProperties is present,
64+ # so "grade_12" matches the pattern and escapes the name constraint
65+ {
66+ "type" : "object" ,
67+ "patternProperties" : {"^grade_[0-9]+$" : {"type" : "integer" }},
68+ "propertyNames" : {"pattern" : "^grade_[0-9]$" }, # does NOT match "grade_12"
69+ },
70+ # propertyNames makes xgrammar discard the sibling additionalProperties
71+ # value schema: https://github.com/mlc-ai/xgrammar/issues/826
72+ {
73+ "type" : "object" ,
74+ "propertyNames" : {"pattern" : "^[a-z]+$" },
75+ "additionalProperties" : {"type" : "integer" },
76+ },
77+ # propertyNames is a string schema that conventionally omits "type", so
78+ # it escapes the string check while xgrammar drops its length bound all
79+ # the same: https://github.com/mlc-ai/xgrammar/issues/749
80+ {
81+ "type" : "object" ,
82+ "propertyNames" : {"pattern" : "^a+$" , "maxLength" : 2 },
83+ },
84+ # xgrammar emits named `properties` separately from `propertyNames` and
85+ # only applies `propertyNames` in its additional-properties branch, so a
86+ # key declared in `properties` escapes the name constraint regardless of
87+ # what `propertyNames` contains.
88+ {
89+ "type" : "object" ,
90+ "properties" : {"Bad" : {"type" : "integer" }},
91+ "required" : ["Bad" ],
92+ "propertyNames" : {"pattern" : "^[a-z]+$" },
93+ },
94+ {
95+ "type" : "object" ,
96+ "properties" : {"Bad" : {"type" : "integer" }},
97+ "propertyNames" : {"enum" : ["good" ]},
98+ },
99+ # xgrammar's propertyNames-only branch falls back to an unconstrained
100+ # value type, so it drops unevaluatedProperties whether it restricts
101+ # values (a schema) or forbids them (false).
102+ {
103+ "type" : "object" ,
104+ "propertyNames" : {"pattern" : "^[a-z]+$" },
105+ "unevaluatedProperties" : {"type" : "integer" },
106+ },
107+ {
108+ "type" : "object" ,
109+ "propertyNames" : {"pattern" : "^[a-z]+$" },
110+ "unevaluatedProperties" : False ,
111+ },
112+ ]
113+
114+
115+ @pytest .fixture
116+ def unsupported_pattern_properties_combinations ():
52117 return [
53- {"type" : "object" , "propertyNames" : {"pattern" : "^[a-z]+$" }},
54- {"type" : "object" , "patternProperties" : {"^S" : {"type" : "string" }}},
118+ # JSON Schema requires a property matched by both `properties` and
119+ # `patternProperties` to satisfy both (conjunction), but xgrammar
120+ # compiles them as alternative branches, so satisfying either one is
121+ # enough.
122+ {
123+ "type" : "object" ,
124+ "properties" : {"x" : {"type" : "string" }},
125+ "patternProperties" : {"^x$" : {"type" : "integer" }},
126+ },
127+ # The same alternative-branches problem applies to overlapping
128+ # patternProperties patterns: a key matching both patterns only has to
129+ # satisfy one of them.
130+ {
131+ "type" : "object" ,
132+ "patternProperties" : {
133+ "^a[a-z]*$" : {"type" : "string" },
134+ "^[a-z]*z$" : {"type" : "integer" },
135+ },
136+ },
55137 ]
56138
57139
58140@pytest .fixture
59- def supported_schema ():
141+ def supported_frankenstein_schema ():
142+ # IMPORTANT(arpera):
143+ # Do NOT add more keywords here! This schema is overcrowded enough that a new
144+ # keyword can end up having no effect on the compiled grammar while the
145+ # test still passes. Give a new keyword its own fixture and named test.
60146 return {
61147 "type" : "object" ,
62148 "properties" : {
@@ -88,14 +174,28 @@ def supported_schema():
88174 }
89175
90176
177+ @pytest .fixture
178+ def property_names_schema ():
179+ return {"type" : "object" , "propertyNames" : {"pattern" : "^[a-z_]+$" }}
180+
181+
182+ @pytest .fixture
183+ def pattern_properties_schema ():
184+ return {
185+ "type" : "object" ,
186+ "patternProperties" : {"^grade_[0-9]+$" : {"type" : "integer" }},
187+ }
188+
189+
91190@pytest .mark .parametrize (
92191 "schema_type" ,
93192 [
94193 "unsupported_string_schemas" ,
95194 "unsupported_integer_schemas" ,
96195 "unsupported_number_schemas" ,
97196 "unsupported_array_schemas" ,
98- "unsupported_object_schemas" ,
197+ "unsupported_property_names_combinations" ,
198+ "unsupported_pattern_properties_combinations" ,
99199 ],
100200)
101201def test_unsupported_json_features_by_type (schema_type , request ):
@@ -106,7 +206,29 @@ def test_unsupported_json_features_by_type(schema_type, request):
106206 )
107207
108208
109- def test_supported_json_features (supported_schema ):
110- assert not has_xgrammar_unsupported_json_features (supported_schema ), (
111- "Schema should be supported"
209+ @pytest .mark .parametrize (
210+ "schema_type" ,
211+ [
212+ "supported_frankenstein_schema" ,
213+ "pattern_properties_schema" ,
214+ "property_names_schema" ,
215+ ],
216+ )
217+ def test_supported_json_features (schema_type , request ):
218+ schema = request .getfixturevalue (schema_type )
219+ assert not has_xgrammar_unsupported_json_features (schema ), (
220+ f"Schema should be supported: { schema } "
112221 )
222+
223+
224+ def test_property_names_constrains_keys (property_names_schema ):
225+ assert grammar_accepts (property_names_schema , '{"score": 5}' )
226+ assert grammar_accepts (property_names_schema , '{"score": "seven"}' )
227+ assert not grammar_accepts (property_names_schema , '{"Score": 5}' )
228+ assert not grammar_accepts (property_names_schema , '{"score_1": 5}' )
229+
230+
231+ def test_pattern_properties_constrains_keys_and_values (pattern_properties_schema ):
232+ assert grammar_accepts (pattern_properties_schema , '{"grade_1": 5}' )
233+ assert not grammar_accepts (pattern_properties_schema , '{"other": 5}' )
234+ assert not grammar_accepts (pattern_properties_schema , '{"grade_1": "five"}' )
0 commit comments