A high-throughput and memory-efficient inference and serving engine for LLMs
Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git
Synced Nov 15, 14:45 UTC
[Feature][V1] Add xgrammar to support minLength, maxLength with test (#16516)
Signed-off-by: Leon Seidel <leon.seidel@fau.de>
leon-seidel committed
Apr 12, 2025 at 08:22 UTC
e92d7085bfb25200c540391d0be06cb3b7c29ea4
3 files changed
+48
-10
tests/v1/entrypoints/llm/test_struct_output_generate.py
+39
@@ -325,6 +325,45 @@ def test_structured_output(
325
output_json = json.loads(generated_text)
326
jsonschema.validate(instance=output_json, schema=json_schema)
327
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
+
367
368
@pytest.mark.skip_global_cleanup
369
@pytest.mark.parametrize("model_name, tokenizer_mode",
tests/v1/structured_output/test_utils.py
+8
-8
@@ -13,14 +13,6 @@ def unsupported_string_schemas():
13
"type": "string",
14
"pattern": "^[a-zA-Z]+$"
15
},
16
- {
17
- "type": "string",
18
- "minLength": 1
19
- },
20
- {
21
- "type": "string",
22
- "maxLength": 100
23
- },
16
{
17
"type": "string",
18
"format": "email"
@@ -164,6 +156,14 @@ def supported_schema():
156
"type": "string",
157
"enum": ["sedan", "suv", "truck"]
158
},
159
+ "short_description": {
160
+ "type": "string",
161
+ "maxLength": 50
162
+ },
163
+ "long_description": {
164
+ "type": "string",
165
+ "minLength": 50
166
+ },
167
"address": {
168
"type": "object",
169
"properties": {
vllm/v1/structured_output/utils.py
+1
-2
@@ -41,8 +41,7 @@ def has_xgrammar_unsupported_json_features(schema: dict[str, Any]) -> bool:
41
return True
42
43
# 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:
45
return True
46
47
# Unsupported keywords for objects