Add option to disable any_whitespace for xgrammar and llguidance backends. (#8919)

Co-authored-by: Chang Su <chang.s.su@oracle.com>
This commit is contained in:
Lorenzo Lu
2025-10-11 22:24:58 +08:00
committed by GitHub
co-authored by Chang Su
parent 5061b8fd3e
commit b5dcfd4154
5 changed files with 21 additions and 4 deletions
@@ -167,6 +167,7 @@ class XGrammarGrammarBackend(BaseGrammarBackend):
tokenizer,
vocab_size: int,
model_eos_token_ids: Optional[List[int]] = None,
any_whitespace: bool = True,
):
super().__init__()
@@ -188,6 +189,7 @@ class XGrammarGrammarBackend(BaseGrammarBackend):
self.grammar_compiler = GrammarCompiler(tokenizer_info=tokenizer_info)
self.vocab_size = vocab_size
self.override_stop_tokens = override_stop_tokens
self.any_whitespace = any_whitespace
def _from_context(
self, ctx: CompiledGrammar, key_string: str, grammar_stats: GrammarStats
@@ -212,7 +214,9 @@ class XGrammarGrammarBackend(BaseGrammarBackend):
# Note: This builtin JSON grammar includes *all* valid JSON (including, for example, arrays at the root)
ctx = self.grammar_compiler.compile_builtin_json_grammar()
else:
ctx = self.grammar_compiler.compile_json_schema(schema=key_string)
ctx = self.grammar_compiler.compile_json_schema(
schema=key_string, any_whitespace=self.any_whitespace
)
except (RuntimeError, json.decoder.JSONDecodeError) as e:
logging.error(f"Hit invalid json_schema: {key_string=}, {e=}")