Skip to content

Commit e92d708

Browse files
authored
[Feature][V1] Add xgrammar to support minLength, maxLength with test (#16516)
Signed-off-by: Leon Seidel <[email protected]>
1 parent bd6028d commit e92d708

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

tests/v1/entrypoints/llm/test_struct_output_generate.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,45 @@ def test_structured_output(
325325
output_json = json.loads(generated_text)
326326
jsonschema.validate(instance=output_json, schema=json_schema)
327327

328+
#
329+
# Test 10: Generate structured with minLength and maxLength
330+
#
331+
min_length = 50
332+
max_length = 50
333+
json_schema = {
334+
"type": "object",
335+
"properties": {
336+
"description": {
337+
"type": "string",
338+
"maxLength": max_length,
339+
"minLength": min_length
340+
}
341+
},
342+
"required": ["description"]
343+
}
344+
345+
sampling_params = SamplingParams(
346+
temperature=1.0,
347+
max_tokens=1000,
348+
guided_decoding=GuidedDecodingParams(json=json_schema))
349+
outputs = llm.generate(
350+
prompts="Generate a description of a frog using 50 characters.",
351+
sampling_params=sampling_params,
352+
use_tqdm=True)
353+
354+
assert outputs is not None
355+
356+
for output in outputs:
357+
assert output is not None
358+
assert isinstance(output, RequestOutput)
359+
prompt = output.prompt
360+
361+
generated_text = output.outputs[0].text
362+
assert generated_text is not None
363+
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
364+
output_json = json.loads(generated_text)
365+
jsonschema.validate(instance=output_json, schema=json_schema)
366+
328367

329368
@pytest.mark.skip_global_cleanup
330369
@pytest.mark.parametrize("model_name, tokenizer_mode",

tests/v1/structured_output/test_utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ def unsupported_string_schemas():
1313
"type": "string",
1414
"pattern": "^[a-zA-Z]+$"
1515
},
16-
{
17-
"type": "string",
18-
"minLength": 1
19-
},
20-
{
21-
"type": "string",
22-
"maxLength": 100
23-
},
2416
{
2517
"type": "string",
2618
"format": "email"
@@ -164,6 +156,14 @@ def supported_schema():
164156
"type": "string",
165157
"enum": ["sedan", "suv", "truck"]
166158
},
159+
"short_description": {
160+
"type": "string",
161+
"maxLength": 50
162+
},
163+
"long_description": {
164+
"type": "string",
165+
"minLength": 50
166+
},
167167
"address": {
168168
"type": "object",
169169
"properties": {

vllm/v1/structured_output/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ def check_object(obj: dict[str, Any]) -> bool:
4141
return True
4242

4343
# Unsupported keywords for strings
44-
if obj.get("type") == "string" and any(
45-
key in obj for key in ("minLength", "maxLength", "format")):
44+
if obj.get("type") == "string" and "format" in obj:
4645
return True
4746

4847
# Unsupported keywords for objects

0 commit comments

Comments
 (0)