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_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=False)
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
|
ToolChoice
|
The tool choice. |
required |
tools
|
list[Tool] | None
|
The list of tools available. Required when mode is any/required or a NamedToolChoice; 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 |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The rendered lark grammar string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a NamedToolChoice references a tool not in tools, or mode is any/required with no tools provided. |
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(reasoning=None)
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reasoning
|
bool | None
|
Deprecated and ignored. Template selection is determined solely by the tokenizer version and presence of think tokens. Will be removed in 1.13.0. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The jinja template content as a string. |