[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 <noreply@openai.com>
This commit is contained in:
CaiJohn
2026-09-21 09:59:20 +03:00
committed by GitHub
co-authored by john Codex
parent 9598487bc1
commit 3c84ad13ed
3 changed files with 82 additions and 7 deletions
+46
View File
@@ -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"}')
@@ -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.
(
{