@@ -62,6 +62,16 @@ class CarDescription(BaseModel):
62
62
car_type : CarType
63
63
64
64
65
+ def _load_json (s : str , backend : str ) -> str :
66
+ if backend != "xgrammar" :
67
+ return json .loads (s )
68
+
69
+ # xgrammar specific workarounds
70
+ # https://github.com/mlc-ai/xgrammar/issues/286
71
+ s = re .sub (r'[\x00-\x1F\x7F-\xFF]' , '' , s )
72
+ return json .loads (s )
73
+
74
+
65
75
@pytest .mark .skip_global_cleanup
66
76
@pytest .mark .parametrize (
67
77
"model_name, guided_decoding_backend, tokenizer_mode, speculative_config" ,
@@ -102,7 +112,7 @@ def test_structured_output(
102
112
#
103
113
sampling_params = SamplingParams (
104
114
temperature = 1.0 ,
105
- max_tokens = 1000 ,
115
+ max_tokens = 4096 ,
106
116
guided_decoding = GuidedDecodingParams (json = sample_json_schema ))
107
117
outputs = llm .generate (prompts = [
108
118
(f"Give an example JSON for an employee profile that fits this "
@@ -131,7 +141,7 @@ def test_structured_output(
131
141
#
132
142
sampling_params = SamplingParams (
133
143
temperature = 1.0 ,
134
- max_tokens = 100 ,
144
+ max_tokens = 4096 ,
135
145
n = 2 ,
136
146
guided_decoding = GuidedDecodingParams (json_object = True ))
137
147
@@ -161,7 +171,7 @@ def test_structured_output(
161
171
#
162
172
sampling_params = SamplingParams (
163
173
temperature = 1.0 ,
164
- max_tokens = 1000 ,
174
+ max_tokens = 4096 ,
165
175
guided_decoding = GuidedDecodingParams (json = unsupported_json_schema ))
166
176
if guided_decoding_backend .startswith ("xgrammar" ):
167
177
with pytest .raises (ValueError ,
@@ -376,12 +386,13 @@ def test_structured_output(
376
386
"minLength" : min_length
377
387
}
378
388
},
379
- "required" : ["description" ]
389
+ "required" : ["description" ],
390
+ "additionalProperties" : False
380
391
}
381
392
382
393
sampling_params = SamplingParams (
383
394
temperature = 1.0 ,
384
- max_tokens = 1000 ,
395
+ max_tokens = 4096 ,
385
396
guided_decoding = GuidedDecodingParams (json = json_schema ))
386
397
387
398
outputs = llm .generate (
@@ -417,7 +428,8 @@ def test_structured_output(
417
428
"city" : {
418
429
"type" : "string"
419
430
}
420
- }
431
+ },
432
+ "additionalProperties" : False
421
433
},
422
434
"end" : "</function>"
423
435
}],
@@ -426,7 +438,7 @@ def test_structured_output(
426
438
427
439
sampling_params = SamplingParams (
428
440
temperature = 0.0 ,
429
- max_tokens = 100 ,
441
+ max_tokens = 4096 ,
430
442
guided_decoding = GuidedDecodingParams (
431
443
structural_tag = json .dumps (structural_tag_config )))
432
444
0 commit comments