Skip to content

Commit 14e53ed

Browse files
authored
[V1] Fix json_object support with xgrammar (#15488)
Signed-off-by: Russell Bryant <rbryant@redhat.com>
1 parent ddb94c2 commit 14e53ed

File tree

5 files changed

+10
-19
lines changed

5 files changed

+10
-19
lines changed

requirements/common.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ lm-format-enforcer >= 0.10.11, < 0.11
2121
llguidance >= 0.7.9, < 0.8.0; platform_machine == "x86_64" or platform_machine == "arm64" or platform_machine == "aarch64"
2222
outlines == 0.1.11
2323
lark == 1.2.2
24-
xgrammar == 0.1.16; platform_machine == "x86_64" or platform_machine == "aarch64"
24+
xgrammar == 0.1.17; platform_machine == "x86_64" or platform_machine == "aarch64"
2525
typing_extensions >= 4.10
2626
filelock >= 3.16.1 # need to contain https://github.com/tox-dev/filelock/pull/317
2727
partial-json-parser # used for parsing partial JSON outputs

tests/v1/entrypoints/llm/test_struct_output_generate.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,9 @@ def test_structured_output(
125125
print(generated_text)
126126
assert generated_text is not None
127127

128-
# Parse to verify it is valid JSON
128+
# Parse to verify it is a valid JSON object
129129
parsed_json = json.loads(generated_text)
130-
allowed_types: tuple[type, ...] = (dict, )
131-
if guided_decoding_backend.startswith("xgrammar"):
132-
# TODO - we are currently too permissive with xgrammar and
133-
# allow # any valid json (typically comes back as a list or
134-
# object). We can fix this by specifying a jsonschema of
135-
# {"type": "object"}, # but we need this fix in a release
136-
# first: https://github.com/mlc-ai/xgrammar/pull/264
137-
allowed_types = (dict, list)
138-
assert isinstance(parsed_json, allowed_types)
130+
assert isinstance(parsed_json, dict)
139131

140132
#
141133
# Test 3: test a jsonschema incompatible with xgrammar

vllm/model_executor/guided_decoding/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@ def fallback_or_error(guided_params: GuidedDecodingParams, message: str,
7979
"xgrammar does not support Lark grammars and the "
8080
"grammar failed to convert to GBNF.", "outlines")
8181

82-
elif guided_params.json_object:
83-
# https://github.com/mlc-ai/xgrammar/issues/256
84-
fallback_or_error(guided_params,
85-
"xgrammar does not support json_object.",
86-
"guidance")
87-
8882
# If the xgrammar module cannot be imported successfully,
8983
# we should still allow users to use guided decoding with a fallback.
9084
elif not xgr_installed:

vllm/model_executor/guided_decoding/xgrammar_decoding.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,10 @@ def _ensure_ctx(self):
320320
elif self.config.grammar_str is not None:
321321
self.ctx = compiler.compile_grammar(self.config.grammar_str)
322322
elif self.config.json_object:
323-
self.ctx = compiler.compile_builtin_json_grammar()
323+
any_whitespace = self.config.any_whitespace
324+
self.ctx = compiler\
325+
.compile_json_schema('{"type": "object"}',
326+
any_whitespace=any_whitespace)
324327
else:
325328
raise ValueError(
326329
"Invalid configuration for xgrammar logits processor")

vllm/v1/structured_output/backend_xgrammar.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ def compile_grammar(self, request_type: StructuredOutputOptions,
8484
ctx = self.compiler.compile_json_schema(
8585
grammar_spec, any_whitespace=not self.disable_any_whitespace)
8686
elif request_type == StructuredOutputOptions.JSON_OBJECT:
87-
ctx = self.compiler.compile_builtin_json_grammar()
87+
ctx = self.compiler.compile_json_schema(
88+
'{"type": "object"}',
89+
any_whitespace=not self.disable_any_whitespace)
8890
elif request_type == StructuredOutputOptions.GRAMMAR:
8991
ctx = self.compiler.compile_grammar(grammar_spec)
9092
elif request_type == StructuredOutputOptions.REGEX:

0 commit comments

Comments
 (0)