support regex in xgrammar backend (#2983)

This commit is contained in:
Enrique Shockwave authored and GitHub committed 2025-01-20 04:34:41 +08:00
1 parent 2c05f81f15
commit 3bcf5ecea7
7 files changed
+200 -8

No files matched your search

@@ -19,6 +19,7 @@ from typing import List, Tuple
import torch
from xgrammar import (
CompiledGrammar,
Grammar,
GrammarCompiler,
GrammarMatcher,
TokenizerInfo,
@@ -133,10 +134,13 @@ class XGrammarGrammarBackend(BaseGrammarBackend):
logging.warning(f"Skip invalid ebnf: ebnf={key_string}, {e=}")
return None
elif key_type == "regex":
logger.warning(
"regex hasn't been supported by xgrammar yet. This is skipped."
)
return None
try:
ctx = self.grammar_compiler.compile_grammar(
Grammar.from_regex(key_string)
)
except RuntimeError as e:
logging.warning(f"Skip invalid regex: regex={key_string}, {e=}")
return None
else:
raise ValueError(f"Invalid key_type: {key_type}")