mistral_common.integrations.chat_templates.chat_templates
convert_tokenizer_to_chat_template(tokenizer_file, system_prompt=None, use_special_token_variables=True)
Load a tokenizer file and auto-detect its capabilities to generate a matching chat template.
Loads the tokenizer via MistralTokenizer.from_file, inspects the resulting
instruct tokenizer to determine version, backend (SentencePiece vs Tekken),
and supported modalities, then delegates to generate_chat_template with the
detected flags.
The plain_thinking_support flag is set heuristically: a v11 tokenizer without
an audio encoder uses plain <think>/</think> text tags instead of special
[THINK]/[/THINK] tokens. When audio is present on a v11 tokenizer,
plain_thinking_support is set to False because the two are mutually exclusive.
thinking_support (special-token thinking) is detected by checking whether both
begin_think and end_think special tokens are registered in the underlying
tokenizer via its public is_special method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer_file
|
str | Path
|
Path to the tokenizer file (tekken JSON or SentencePiece |
required |
system_prompt
|
str | None
|
Optional default system prompt to embed in the template.
When not |
None
|
use_special_token_variables
|
bool
|
Whether to emit BOS/EOS as Jinja variable
references ( |
True
|
Returns:
| Type | Description |
|---|---|
str
|
The generated Jinja2 chat template string matching the tokenizer's capabilities. |
Raises:
| Type | Description |
|---|---|
TokenizerException
|
If the tokenizer file is not recognized or invalid. |
Source code in src/mistral_common/integrations/chat_templates/chat_templates.py
generate_chat_template(spm, tokenizer_version, image_support, audio_support, thinking_support, default_system_prompt, plain_thinking_support, use_special_token_variables)
Generate a chat template based on configuration.
Programmatically generates a Jinja2 chat template string that formats conversation messages for Mistral models. The generated template handles message roles, special tokens, tool calls, and multimodal content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spm
|
bool
|
Whether to use SentencePiece tokenizer. |
required |
tokenizer_version
|
TokenizerVersion
|
The tokenizer version. |
required |
image_support
|
bool
|
Whether to support image chunks. |
required |
audio_support
|
bool
|
Whether to support audio chunks. |
required |
thinking_support
|
bool
|
Whether to support thinking chunks with special tokens. |
required |
default_system_prompt
|
str | None
|
Optional default system prompt to embed. |
required |
plain_thinking_support
|
bool
|
Whether to support thinking chunks with plain
|
required |
use_special_token_variables
|
bool
|
Whether to emit BOS/EOS as Jinja variable
references ( |
required |
Returns:
| Type | Description |
|---|---|
str
|
The generated Jinja2 template as a string. |