xgrammar.GrammarCompiler

class xgrammar.GrammarCompiler(tokenizer_info: TokenizerInfo, *, max_threads: int = 8, cache_enabled: bool = True, cache_limit_bytes: int = -1)[source]

Bases: XGRObject

The compiler for grammars. It is associated with a certain tokenizer info, and compiles grammars into CompiledGrammar with the tokenizer info. It allows parallel compilation with multiple threads, and has a cache to store the compilation result, avoiding compiling the same grammar multiple times.

Parameters:
  • tokenizer_info (TokenizerInfo) – The tokenizer info.

  • max_threads (int, default: 8) – The maximum number of threads used to compile the grammar.

  • cache_enabled (bool, default: True) – Whether to enable the cache.

  • cache_limit_bytes (int, default: -1) – The maximum memory usage for the cache in the specified unit. Note that the actual memory usage may slightly exceed this value.

__init__(tokenizer_info: TokenizerInfo, *, max_threads: int = 8, cache_enabled: bool = True, cache_limit_bytes: int = -1)[source]

Methods

__init__(tokenizer_info, *[, max_threads, ...])

clear_cache()

Clear all cached compiled grammars.

compile_builtin_json_grammar()

Get CompiledGrammar from the standard JSON.

compile_grammar()

compile_json_schema(schema, *[, ...])

Get CompiledGrammar from the specified JSON schema and format.

compile_regex(regex)

Get CompiledGrammar from the specified regex.

compile_structural_tag(tags, triggers)

Compile a grammar from structural tags.

get_cache_size_bytes()

The approximate memory usage of the cache in bytes.

Attributes

cache_limit_bytes

The maximum memory usage for the cache in bytes.

property cache_limit_bytes: int

The maximum memory usage for the cache in bytes. Returns -1 if the cache has no memory limit.

clear_cache() None[source]

Clear all cached compiled grammars.

compile_builtin_json_grammar() CompiledGrammar[source]

Get CompiledGrammar from the standard JSON.

Returns:

compiled_grammar – The compiled grammar.

Return type:

CompiledGrammar

compile_grammar(ebnf_string: str, *, root_rule_name: str = 'root') CompiledGrammar[source]
compile_grammar(grammar: Grammar) CompiledGrammar
compile_json_schema(schema: Union[str, Type[BaseModel], Dict[str, Any]], *, any_whitespace: bool = True, indent: Optional[int] = None, separators: Optional[Tuple[str, str]] = None, strict_mode: bool = True) CompiledGrammar[source]

Get CompiledGrammar from the specified JSON schema and format. The indent and separators parameters follow the same convention as in json.dumps().

Parameters:
  • schema (Union[str, Type[BaseModel], Dict[str, Any]]) – The schema string or Pydantic model or JSON schema dict.

  • indent (Optional[int], default: None) – The number of spaces for indentation. If None, the output will be in one line.

  • separators (Optional[Tuple[str, str]], default: None) – Two separators used in the schema: comma and colon. Examples: (“,”, “:”), (”, “, “: “). If None, the default separators will be used: (“,”, “: “) when the indent is not None, and (”, “, “: “) otherwise.

  • strict_mode (bool, default: True) –

    Whether to use strict mode. In strict mode, the generated grammar will not allow properties and items that is not specified in the schema. This is equivalent to setting unevaluatedProperties and unevaluatedItems to false.

    This helps LLM to generate accurate output in the grammar-guided generation with JSON schema.

Returns:

compiled_grammar – The compiled grammar.

Return type:

CompiledGrammar

compile_regex(regex: str) CompiledGrammar[source]

Get CompiledGrammar from the specified regex.

Parameters:

regex (str) – The regex string.

Returns:

compiled_grammar – The compiled grammar.

Return type:

CompiledGrammar

compile_structural_tag(tags: List[StructuralTagItem], triggers: List[str]) CompiledGrammar[source]

Compile a grammar from structural tags. See Grammar.from_structural_tag() for more details.

Parameters:
Returns:

compiled_grammar – The compiled grammar.

Return type:

CompiledGrammar

get_cache_size_bytes() int[source]

The approximate memory usage of the cache in bytes.