Skip to content

Commit ebab1ac

Browse files
authored
[CI] Make JSON output tests less likely to fail (#17859)
Signed-off-by: Russell Bryant <rbryant@redhat.com>
1 parent 2b0db9b commit ebab1ac

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

tests/v1/entrypoints/conftest.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ def sample_json_schema():
7272
"type": "string"
7373
}
7474
},
75-
"required": ["company", "duration", "position"]
75+
"required": ["company", "duration", "position"],
76+
"additionalProperties": False
7677
}
7778
}
7879
},
7980
"required":
80-
["name", "age", "skills", "grade", "email", "work_history"]
81+
["name", "age", "skills", "grade", "email", "work_history"],
82+
"additionalProperties": False
8183
}
8284

8385

@@ -100,7 +102,8 @@ def unsupported_json_schema():
100102
}
101103
}
102104
},
103-
"required": ["score", "tags"]
105+
"required": ["score", "tags"],
106+
"additionalProperties": False
104107
}
105108

106109

@@ -139,7 +142,8 @@ def sample_definition_json_schema():
139142
},
140143
'required': ['steps', 'final_answer'],
141144
'title': 'MathReasoning',
142-
'type': 'object'
145+
'type': 'object',
146+
"additionalProperties": False
143147
}
144148

145149

tests/v1/entrypoints/llm/test_struct_output_generate.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ class CarDescription(BaseModel):
6262
car_type: CarType
6363

6464

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+
6575
@pytest.mark.skip_global_cleanup
6676
@pytest.mark.parametrize(
6777
"model_name, guided_decoding_backend, tokenizer_mode, speculative_config",
@@ -102,7 +112,7 @@ def test_structured_output(
102112
#
103113
sampling_params = SamplingParams(
104114
temperature=1.0,
105-
max_tokens=1000,
115+
max_tokens=4096,
106116
guided_decoding=GuidedDecodingParams(json=sample_json_schema))
107117
outputs = llm.generate(prompts=[
108118
(f"Give an example JSON for an employee profile that fits this "
@@ -131,7 +141,7 @@ def test_structured_output(
131141
#
132142
sampling_params = SamplingParams(
133143
temperature=1.0,
134-
max_tokens=100,
144+
max_tokens=4096,
135145
n=2,
136146
guided_decoding=GuidedDecodingParams(json_object=True))
137147

@@ -161,7 +171,7 @@ def test_structured_output(
161171
#
162172
sampling_params = SamplingParams(
163173
temperature=1.0,
164-
max_tokens=1000,
174+
max_tokens=4096,
165175
guided_decoding=GuidedDecodingParams(json=unsupported_json_schema))
166176
if guided_decoding_backend.startswith("xgrammar"):
167177
with pytest.raises(ValueError,
@@ -376,12 +386,13 @@ def test_structured_output(
376386
"minLength": min_length
377387
}
378388
},
379-
"required": ["description"]
389+
"required": ["description"],
390+
"additionalProperties": False
380391
}
381392

382393
sampling_params = SamplingParams(
383394
temperature=1.0,
384-
max_tokens=1000,
395+
max_tokens=4096,
385396
guided_decoding=GuidedDecodingParams(json=json_schema))
386397

387398
outputs = llm.generate(
@@ -417,7 +428,8 @@ def test_structured_output(
417428
"city": {
418429
"type": "string"
419430
}
420-
}
431+
},
432+
"additionalProperties": False
421433
},
422434
"end": "</function>"
423435
}],
@@ -426,7 +438,7 @@ def test_structured_output(
426438

427439
sampling_params = SamplingParams(
428440
temperature=0.0,
429-
max_tokens=100,
441+
max_tokens=4096,
430442
guided_decoding=GuidedDecodingParams(
431443
structural_tag=json.dumps(structural_tag_config)))
432444

0 commit comments

Comments
 (0)