mistral_common.integrations.chat_templates.template_generator
TemplateConfig(version, spm=False, image_support=False, audio_support=False, thinking_support=False, plain_thinking_support=False, use_special_token_variables=False, model_settings_fields=None, reasoning_effort_values=None, reasoning_effort_default=None)
dataclass
Configuration for generating a chat template.
This class encapsulates all the configuration options required to generate a Jinja2 chat template that formats conversation messages for Mistral models. The template handles message roles, special tokens, tool calls, and multimodal content.
Attributes:
| Name | Type | Description |
|---|---|---|
version |
TokenizerVersion
|
The tokenizer version (e.g., v1, v2, v3, v7, v11, v13, v15). Determines special token formatting, tool call syntax, and available features. |
spm |
bool
|
Whether to use SentencePiece tokenizer formatting. When |
image_support |
bool
|
Whether to enable image chunk processing in user messages. Adds [IMG] token support. Requires version v3+. Mutually exclusive with audio. |
audio_support |
bool
|
Whether to enable audio chunk processing in user messages. Adds [AUDIO] token support. Requires version v7+. Mutually exclusive with image. |
thinking_support |
bool
|
Whether to enable thinking chunks in system and assistant messages. Adds [THINK]/[/THINK] token support. Requires version v13+. |
plain_thinking_support |
bool
|
Whether to enable plain text thinking chunks using
|
use_special_token_variables |
bool
|
Whether to emit BOS/EOS as Jinja variable references
( |
model_settings_fields |
frozenset[str] | None
|
Model settings fields to render. |
reasoning_effort_values |
tuple[str, ...] | None
|
Reasoning effort values exposed on requests. Required when the
|
reasoning_effort_default |
str | None
|
Exposed reasoning effort rendered when the request
leaves the effort unset. Required when the |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the configuration is invalid (e.g., conflicting options like spm with v11+, image and audio together, or version requirements not met). |
Examples:
>>> config = TemplateConfig(version=TokenizerVersion.v3, image_support=True)
>>> config.image_support
True
>>> config.version == TokenizerVersion.v3
True
any_thinking_support
property
Whether any form of thinking support is enabled.
bos_expr
property
Jinja expression for the BOS token.
eos_expr
property
Jinja expression for the EOS token.
build_chat_template(config)
Generate a complete chat template based on configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TemplateConfig
|
Template configuration specifying version and features. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The complete Jinja2 template as a string. |
Examples:
>>> config = TemplateConfig(version=TokenizerVersion.v3, image_support=True, use_special_token_variables=True)
>>> template = build_chat_template(config)
>>> "bos_token" in template
True