mistral_common.integrations.chat_templates.chat_templates
convert_tokenizer_to_chat_template(tokenizer_file, system_prompt=None, use_special_token_variables=True, plain_thinking_support=None)
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.
thinking_support (special-token thinking) is auto-detected by checking whether
both begin_think and end_think special tokens are registered in the underlying
tokenizer via its public is_special method.
plain_thinking_support cannot be reliably auto-detected: the tokenizer file
carries no signal that distinguishes a reasoning v11 model from a non-reasoning
one. For example, mistralai/Magistral-Small-2506 (reasoning, plain
<think>/</think> tags) and mistralai/Mistral-Small-3.2-24B-Instruct-2506
(non-reasoning) ship tekken.json files with identical config, vocab,
special_tokens and multimodal sections. When left as
None (the default), the heuristic version == TokenizerVersion.v11 and not
audio_supportis used, which over-triggers on non-reasoning v11 models. Passplain_thinking_support=False` to suppress the guess for such a model.
Passing plain_thinking_support=True cannot widen the set of templates that
can be produced: TemplateConfig only accepts plain thinking on a v11
tokenizer without audio, which is exactly the condition under which the
heuristic already returns True. The value is accepted so callers can state
the intent explicitly, but on any other tokenizer it raises rather than
forcing the feature on.
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
|
plain_thinking_support
|
bool | None
|
Override for plain-text thinking chunk support.
|
None
|
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. |
ValueError
|
If |
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. |