[feature] [xgrammar] support patternProperties/propertyNames/unevaluatedProperties kw for object types (#42904)

Signed-off-by: cjackal <44624812+cjackal@users.noreply.github.com>
Co-authored-by: Artem Perevedentsev <aperevedents@nvidia.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
cjackal
2026-09-15 12:58:52 +08:00
committed by GitHub
co-authored by Artem Perevedentsev Claude Sonnet 5
parent 7b7e117c29
commit cd10ed6f9f
3 changed files with 172 additions and 16 deletions
+35 -6
View File
@@ -236,6 +236,12 @@ STRING_SUPPORTED_FORMATS = {
}
def _has_pattern_and_length_bounds(schema: dict[str, Any]) -> bool:
return ("pattern" in schema or "format" in schema) and (
"minLength" in schema or "maxLength" in schema
)
def has_xgrammar_unsupported_json_features(schema: dict[str, Any]) -> bool:
"""Check if JSON schema contains features unsupported by xgrammar."""
@@ -269,16 +275,39 @@ 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):
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") == "string"
and ("pattern" in obj or "format" in obj)
and ("minLength" in obj or "maxLength" in obj)
obj.get("type") == "object"
and isinstance(obj.get("propertyNames"), dict)
and _has_pattern_and_length_bounds(obj["propertyNames"])
):
return True
# Unsupported keywords for objects
if obj.get("type") == "object" and any(
key in obj for key in ("patternProperties", "propertyNames")
# FIXME: propertyNames conflicts with properties/patternProperties/
# additionalProperties/unevaluatedProperties under xgrammar.
# https://github.com/mlc-ai/xgrammar/issues/826
if (
obj.get("type") == "object"
and "propertyNames" in obj
and (
"properties" in obj
or "patternProperties" in obj
or isinstance(obj.get("additionalProperties"), dict)
or obj.get("unevaluatedProperties", True) is not True
)
):
return True
# FIXME: multiple patternProperties, or patternProperties alongside
# properties, conflict under xgrammar.
if (
obj.get("type") == "object"
and isinstance(obj.get("patternProperties"), dict)
and ("properties" in obj or len(obj["patternProperties"]) > 1)
):
return True