Skip to content

Commit dc1b4a6

Browse files
authored
[Core][V0] Enable regex support with xgrammar (#13228)
Signed-off-by: Russell Bryant <[email protected]>
1 parent 63d2705 commit dc1b4a6

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

tests/entrypoints/llm/test_guided_generate.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,26 @@ def test_validation_against_both_guided_decoding_options(sample_regex, llm):
286286

287287
@pytest.mark.skip_global_cleanup
288288
def test_disable_guided_decoding_fallback(sample_regex, llm):
289+
# see has_xgrammar_unsupported_json_features()
290+
unsupported_json = {
291+
"type": "object",
292+
"properties": {
293+
"example": {
294+
"type": "string",
295+
"minLength": 5 # unsupported by xgrammar
296+
}
297+
}
298+
}
289299
sampling_params = SamplingParams(temperature=0.8,
290300
top_p=0.95,
291301
guided_decoding=GuidedDecodingParams(
292-
regex=sample_regex,
302+
json=unsupported_json,
293303
backend="xgrammar:no-fallback"))
294304

295305
with pytest.raises(
296306
ValueError,
297-
match="xgrammar does not support regex guided decoding"):
307+
match="xgrammar does not support advanced JSON schema features "
308+
"like enums, patterns or numeric ranges."):
298309
llm.generate(prompts="This should fail",
299310
sampling_params=sampling_params,
300311
use_tqdm=True)

vllm/model_executor/guided_decoding/__init__.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,9 @@ def fallback_or_error(guided_params: GuidedDecodingParams, message: str,
5959
from vllm.model_executor.guided_decoding.xgrammar_decoding import (
6060
xgr_installed)
6161

62-
# xgrammar doesn't support regex, fallback to outlines
63-
if guided_params.regex is not None:
64-
fallback_or_error(
65-
guided_params,
66-
"xgrammar does not support regex guided decoding.", "outlines")
6762
# xgrammar doesn't support some JSON schema features
68-
elif (guided_params.json is not None
69-
and has_xgrammar_unsupported_json_features(guided_params.json)):
63+
if (guided_params.json is not None and
64+
has_xgrammar_unsupported_json_features(guided_params.json)):
7065
fallback_or_error(
7166
guided_params,
7267
"xgrammar does not support advanced JSON schema features like "

vllm/model_executor/guided_decoding/xgrammar_decoding.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class GrammarConfig:
152152
grammar_str: str | None = None
153153
json_object: bool | None = None
154154
any_whitespace: bool = True
155+
regex_str: str | None = None
155156
max_threads: int = 8
156157

157158
@classmethod
@@ -255,6 +256,13 @@ def from_guided_params(cls,
255256
max_threads=max_threads,
256257
tokenizer_data=tokenizer_data,
257258
)
259+
elif guided_params.regex:
260+
return cls(
261+
regex_str=guided_params.regex,
262+
tokenizer_hash=tokenizer_hash,
263+
max_threads=max_threads,
264+
tokenizer_data=tokenizer_data,
265+
)
258266
else:
259267
raise ValueError(
260268
"Currently only support JSON and EBNF grammar mode for xgrammar"
@@ -330,6 +338,8 @@ def _ensure_ctx(self):
330338
self.ctx = compiler\
331339
.compile_json_schema('{"type": "object"}',
332340
any_whitespace=any_whitespace)
341+
elif self.config.regex_str:
342+
self.ctx = compiler.compile_regex(self.config.regex_str)
333343
else:
334344
raise ValueError(
335345
"Invalid configuration for xgrammar logits processor")

0 commit comments

Comments
 (0)