mistral_common.guidance.grammar_factory
GrammarFactory(tokenizer)
Generates Lark grammars that constrain model output for a given tokenizer.
Grammars cover tool calls, JSON schema output, and thinking sections, and are rendered from tokenizer-version-specific jinja templates using the tokenizer's special tokens.
Requires llguidance and jinja2 to be installed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer
|
MistralTokenizer
|
The Mistral tokenizer to generate grammars for. Must be a
Tekken tokenizer with version >= v11 (see
|
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the tokenizer is not supported. |
ImportError
|
If llguidance or jinja2 is not installed. |
Source code in src/mistral_common/guidance/grammar_factory.py
llg_tokenizer
property
The llguidance tokenizer used to validate grammars.
Returns:
| Type | Description |
|---|---|
LLTokenizer
|
The LLTokenizer instance adapted from this Mistral tokenizer. |
get_grammar_for_chat_request(request, template)
Resolve a chat request to its lark grammar in one pass.
Returns None when the request needs no grammar and raises
InvalidRequestException for inconsistent request combinations.
template is used as given; callers select their custom or default
template before calling this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
ChatCompletionRequest
|
Chat completion request to resolve. |
required |
template
|
str
|
Jinja template used to render the grammar. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The rendered lark grammar, or |
Raises:
| Type | Description |
|---|---|
InvalidRequestException
|
If the request contains an incompatible tool choice or response format. |
Source code in src/mistral_common/guidance/grammar_factory.py
get_lark_for_json_schema(template, json_schema)
Return a lark grammar that only accepts JSON matching the given schema.
Convenience wrapper around get_lark_from_jinja that disables tool calls
and text alternatives, constraining output to the JSON schema alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template
|
str
|
Jinja template to render, as obtained from |
required |
json_schema
|
dict[str, Any]
|
The JSON schema the output must conform to. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The rendered lark grammar string that only matches the given JSON schema. |
Source code in src/mistral_common/guidance/grammar_factory.py
get_lark_from_jinja(template, mode, tools, json_schema, parallel_tool_calls, json_only, force_tool_names)
Render a lark grammar from a jinja template.
This is the general entry point for grammar generation. Combines tool call, JSON schema, and thinking sections according to the requested mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template
|
str
|
Jinja template to render, as obtained from |
required |
mode
|
ToolChoiceEnum
|
The tool choice. |
required |
tools
|
list[Tool] | None
|
The list of tools available. Required when mode is any/required; ignored when mode is none. |
required |
json_schema
|
dict[str, Any] | None
|
Optional JSON schema additionally allowed by the grammar,
unioned with tool call and text alternatives. If |
required |
parallel_tool_calls
|
bool
|
If |
required |
json_only
|
bool
|
If |
required |
force_tool_names
|
bool
|
If |
required |
Returns:
| Type | Description |
|---|---|
str
|
The rendered lark grammar string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/mistral_common/guidance/grammar_factory.py
is_supported(tokenizer)
staticmethod
Check whether the given tokenizer is supported by guidance.
Guidance requires a Tekken tokenizer with version >= v11.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer
|
MistralTokenizer
|
The Mistral tokenizer to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/mistral_common/guidance/grammar_factory.py
select_jinja_template()
Selects and returns the appropriate jinja template content.
Selection derives from the tokenizer version and presence of think tokens:
- Returns the think template when the tokenizer version is >= v13 and both
[THINK] and [/THINK] special tokens are registered.
- Returns the base template in all other cases.
Returns:
| Type | Description |
|---|---|
str
|
The jinja template content as a string. |