Skip to content

Commit d4c62d2

Browse files
ExtReMLapinCNE Pierre FICHEPOILgemini-code-assist[bot]
authored andcommitted
Updated xgrammar backend to not deny supported string formats (#27253)
Signed-off-by: CNE Pierre FICHEPOIL <[email protected]> Signed-off-by: ExtReMLapin <[email protected]> Co-authored-by: CNE Pierre FICHEPOIL <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Signed-off-by: sstamenk <[email protected]>
1 parent bb1b94e commit d4c62d2

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

tests/v1/structured_output/test_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
@pytest.fixture
1414
def unsupported_string_schemas():
1515
return [
16-
{"type": "string", "format": "email"},
16+
{"type": "string", "format": "non_existing_format"},
1717
]
1818

1919

@@ -58,6 +58,7 @@ def supported_schema():
5858
"properties": {
5959
"name": {"type": "string"},
6060
"age": {"type": "integer"},
61+
"email": {"type": "string", "format": "email"},
6162
"status": {"type": "string"},
6263
"scores": {"type": "array", "items": {"type": "number"}},
6364
"car_type": {"type": "string", "enum": ["sedan", "suv", "truck"]},

vllm/v1/structured_output/backend_xgrammar.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,25 @@ def reset(self):
200200
self.matcher.reset()
201201

202202

203+
# cf https://github.com/mlc-ai/xgrammar/blob/a32ac892676d2eedc0327416105b9b06edfb94b2/cpp/json_schema_converter.cc
204+
STRING_SUPPORTED_FORMATS = {
205+
"email",
206+
"date",
207+
"time",
208+
"date-time",
209+
"duration",
210+
"ipv4",
211+
"ipv6",
212+
"hostname",
213+
"uuid",
214+
"uri",
215+
"uri-reference",
216+
"uri-template",
217+
"json-pointer",
218+
"relative-json-pointer",
219+
}
220+
221+
203222
def has_xgrammar_unsupported_json_features(schema: dict[str, Any]) -> bool:
204223
"""Check if JSON schema contains features unsupported by xgrammar."""
205224

@@ -219,7 +238,11 @@ def check_object(obj: dict[str, Any]) -> bool:
219238
return True
220239

221240
# Unsupported keywords for strings
222-
if obj.get("type") == "string" and "format" in obj:
241+
if (
242+
obj.get("type") == "string"
243+
and "format" in obj
244+
and obj["format"] not in STRING_SUPPORTED_FORMATS
245+
):
223246
return True
224247

225248
# Unsupported keywords for objects

0 commit comments

Comments
 (0)