xgr.CompiledGrammar

class xgrammar.CompiledGrammar[source]

This is the primary object to store compiled grammar.

A CompiledGrammar can be used to construct GrammarMatcher to generate token masks efficiently.

Notes

Do not construct this class directly, instead use GrammarCompiler to construct the object.

Attributes:

grammar

The original grammar.

tokenizer_info

The tokenizer info associated with the compiled grammar.

memory_size_bytes

The approximate memory usage of the compiled grammar in bytes.

Methods:

serialize_json()

Serialize the compiled grammar to a JSON string.

deserialize_json(json_str, tokenizer_info)

Deserialize the compiled grammar from a JSON string and tokenizer info.

property grammar: Grammar

The original grammar.

property tokenizer_info: TokenizerInfo

The tokenizer info associated with the compiled grammar.

property memory_size_bytes: int

The approximate memory usage of the compiled grammar in bytes.

serialize_json() str[source]

Serialize the compiled grammar to a JSON string.

static deserialize_json(json_str: str, tokenizer_info: TokenizerInfo) CompiledGrammar[source]

Deserialize the compiled grammar from a JSON string and tokenizer info.