[Bugfix] validate grammar and throw 400 error instead of crashing the engine when xgrammar validation fails (#17623)

Signed-off-by: Jason Cheng <jasoncky96@gmail.com>
Co-authored-by: Russell Bryant <rbryant@redhat.com>
This commit is contained in:
Cheng Kuan Yong Jason
2025-05-12 09:06:10 +08:00
committed by GitHub
co-authored by Russell Bryant
parent d45fe333fb
commit 08bf784078
4 changed files with 240 additions and 1 deletions
@@ -282,6 +282,12 @@ def validate_xgrammar_grammar(sampling_params: SamplingParams) -> None:
else:
schema = gd_params.json
try:
xgr.Grammar.from_json_schema(schema)
except Exception as err:
raise ValueError("Failed to transform json schema into a grammar: "
f"{err}") from err
if has_xgrammar_unsupported_json_features(schema):
raise ValueError("The provided JSON schema contains features not "
"supported by xgrammar.")