-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
[V1] Fix json_object support with xgrammar #15488
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
Conversation
|
👋 Hi! Thank you for contributing to the vLLM project. 💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels. Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging. To run CI, PR reviewers can either: Add 🚀 |
aarnphm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This overall looks good to me, I just have one follow questions wrt an options for any json schemas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, should we also have an option to compile_builtin_json_schema?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe. You could also have a json schema of {} to get the same behavior. I'm not sure if there's a performance difference between the two.
From a user perspective though, it's still possible to get "give me any valid json", but I seriously doubt most people actually want that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, I always thought that compile_builtin_json_schema is just compiling the default jsonschema spec, then users will have a prompt saying something like:
```Generate me an user for a game that has default 'strength', 'power', and 'abilities'``
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But you are correct, probably makes it more easier this way then.
1a3c69b to
e46b6eb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes the handling of the json_object output format when using xgrammar by explicitly compiling a JSON schema with {"type": "object"}. It ensures that only a valid JSON object is produced rather than other JSON types.
- Updated backend_xgrammar.py to compile the JSON schema instead of using the built-in JSON grammar.
- Modified xgrammar_decoding.py to use the JSON schema for json_object with proper whitespace handling.
- Removed the fallback error for json_object in guided_decoding/init.py.
- Updated tests to assert that the generated output is a JSON object.
Reviewed Changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| vllm/v1/structured_output/backend_xgrammar.py | Replaced compile_builtin_json_grammar with compile_json_schema using '{"type": "object"}'. |
| vllm/model_executor/guided_decoding/xgrammar_decoding.py | Updated json_object branch to compile the proper JSON schema with correct whitespace handling. |
| vllm/model_executor/guided_decoding/init.py | Removed the error fallback for json_object to support the new behavior. |
| tests/v1/entrypoints/llm/test_struct_output_generate.py | Changed test assertion to verify the generated JSON is an object. |
Files not reviewed (1)
- requirements/common.txt: Language not supported
|
This pull request has merge conflicts that must be resolved before it can be |
Our previous behavior with xgrammar was to ensure that the response was
valid JSON. When the json_object output format is requested, the correct
behavior is to ensure the response is a JSON object. Valid JSON could
also be an array, a string, or a number, which is not desired here.
The fix is to explicitly use a json schema of {"type": "object"}. An
upgrade to xgrammar is necessary to fix a bug with xgrammar's support of
this schema, so we upgrade to 0.1.17.
- mlc-ai/xgrammar#256
- mlc-ai/xgrammar#264
Signed-off-by: Russell Bryant <rbryant@redhat.com>
e46b6eb to
fca77a5
Compare
DarkLight1337
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stamp
Signed-off-by: Russell Bryant <rbryant@redhat.com> Signed-off-by: xinyuxiao <xinyuxiao2024@gmail.com>
Signed-off-by: Russell Bryant <rbryant@redhat.com> Signed-off-by: Louis Ulmer <ulmerlouis@gmail.com>
Signed-off-by: Russell Bryant <rbryant@redhat.com>
Signed-off-by: Russell Bryant <rbryant@redhat.com>
Signed-off-by: Russell Bryant <rbryant@redhat.com> Signed-off-by: Mu Huai <tianbowen.tbw@antgroup.com>
Our previous behavior with xgrammar was to ensure that the response was
valid JSON. When the json_object output format is requested, the correct
behavior is to ensure the response is a JSON object. Valid JSON could
also be an array, a string, or a number, which is not desired here.
The fix is to explicitly use a json schema of {"type": "object"}. An
upgrade to xgrammar is necessary to fix a bug with xgrammar's support of
this schema, so we upgrade to 0.1.17.
{"type": "object"}json schema fails mlc-ai/xgrammar#256Signed-off-by: Russell Bryant rbryant@redhat.com