Skip to content

[CI] Make JSON output tests less likely to fail #17859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions tests/v1/entrypoints/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ def sample_json_schema():
"type": "string"
}
},
"required": ["company", "duration", "position"]
"required": ["company", "duration", "position"],
"additionalProperties": False
}
}
},
"required":
["name", "age", "skills", "grade", "email", "work_history"]
["name", "age", "skills", "grade", "email", "work_history"],
"additionalProperties": False
}


Expand All @@ -100,7 +102,8 @@ def unsupported_json_schema():
}
}
},
"required": ["score", "tags"]
"required": ["score", "tags"],
"additionalProperties": False
}


Expand Down Expand Up @@ -139,7 +142,8 @@ def sample_definition_json_schema():
},
'required': ['steps', 'final_answer'],
'title': 'MathReasoning',
'type': 'object'
'type': 'object',
"additionalProperties": False
}


Expand Down
26 changes: 19 additions & 7 deletions tests/v1/entrypoints/llm/test_struct_output_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ class CarDescription(BaseModel):
car_type: CarType


def _load_json(s: str, backend: str) -> str:
if backend != "xgrammar":
return json.loads(s)

# xgrammar specific workarounds
# https://github.com/mlc-ai/xgrammar/issues/286
s = re.sub(r'[\x00-\x1F\x7F-\xFF]', '', s)
return json.loads(s)


@pytest.mark.skip_global_cleanup
@pytest.mark.parametrize(
"model_name, guided_decoding_backend, tokenizer_mode, speculative_config",
Expand Down Expand Up @@ -102,7 +112,7 @@ def test_structured_output(
#
sampling_params = SamplingParams(
temperature=1.0,
max_tokens=1000,
max_tokens=4096,
guided_decoding=GuidedDecodingParams(json=sample_json_schema))
outputs = llm.generate(prompts=[
(f"Give an example JSON for an employee profile that fits this "
Expand Down Expand Up @@ -131,7 +141,7 @@ def test_structured_output(
#
sampling_params = SamplingParams(
temperature=1.0,
max_tokens=100,
max_tokens=4096,
n=2,
guided_decoding=GuidedDecodingParams(json_object=True))

Expand Down Expand Up @@ -161,7 +171,7 @@ def test_structured_output(
#
sampling_params = SamplingParams(
temperature=1.0,
max_tokens=1000,
max_tokens=4096,
guided_decoding=GuidedDecodingParams(json=unsupported_json_schema))
if guided_decoding_backend.startswith("xgrammar"):
with pytest.raises(ValueError,
Expand Down Expand Up @@ -376,12 +386,13 @@ def test_structured_output(
"minLength": min_length
}
},
"required": ["description"]
"required": ["description"],
"additionalProperties": False
}

sampling_params = SamplingParams(
temperature=1.0,
max_tokens=1000,
max_tokens=4096,
guided_decoding=GuidedDecodingParams(json=json_schema))

outputs = llm.generate(
Expand Down Expand Up @@ -417,7 +428,8 @@ def test_structured_output(
"city": {
"type": "string"
}
}
},
"additionalProperties": False
},
"end": "</function>"
}],
Expand All @@ -426,7 +438,7 @@ def test_structured_output(

sampling_params = SamplingParams(
temperature=0.0,
max_tokens=100,
max_tokens=4096,
guided_decoding=GuidedDecodingParams(
structural_tag=json.dumps(structural_tag_config)))

Expand Down