Skip to content

Commit 9f55443

Browse files
terrytangyuanlulmer
authored andcommitted
[Bugfix] Guided decoding falls back to outlines when fails to import xgrammar (vllm-project#12976)
Signed-off-by: Yuan Tang <terrytangyuan@gmail.com> Signed-off-by: Louis Ulmer <ulmerlouis@gmail.com>
1 parent ee7c0cb commit 9f55443

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

vllm/model_executor/guided_decoding/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def maybe_backend_fallback(
4040
guided_params.backend = "outlines"
4141

4242
if guided_params.backend == "xgrammar":
43+
from vllm.model_executor.guided_decoding.xgrammar_decoding import (
44+
xgr_installed)
4345
# xgrammar only has x86 wheels for linux, fallback to outlines
4446
from vllm.platforms import current_platform
4547
if current_platform.get_cpu_architecture() is not CpuArchEnum.X86:
@@ -77,6 +79,13 @@ def maybe_backend_fallback(
7779
"Falling back to use outlines instead.")
7880
guided_params.backend = "outlines"
7981

82+
# If the xgrammar module cannot be imported successfully,
83+
# we should still allow users to use guided decoding with a fallback.
84+
elif not xgr_installed:
85+
logger.warning("xgrammar module cannot be imported successfully. "
86+
"Falling back to use outlines instead.")
87+
guided_params.backend = "outlines"
88+
8089
if (guided_params.backend == "outlines"
8190
and guided_params.json_object is not None):
8291
# outlines doesn't support json_object, fallback to xgrammar

vllm/model_executor/guided_decoding/xgrammar_decoding.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
try:
1515
import xgrammar as xgr
1616
from xgrammar.base import _core as xgr_core
17+
xgr_installed = True
1718
except ImportError:
19+
xgr_installed = False
1820
pass
1921

2022
from vllm.model_executor.guided_decoding.utils import (convert_lark_to_gbnf,

0 commit comments

Comments
 (0)