Skip to content

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 True, adds spaces after special tokens. Not supported for versions v11+ or with audio.

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 <think>/</think> tags instead of special tokens. Only available for v11. Mutually exclusive with thinking_support.

use_special_token_variables bool

Whether to emit BOS/EOS as Jinja variable references (bos_token/eos_token) or as literal string values ('<s>'/'</s>'). When True, the template expects bos_token and eos_token to be passed as render kwargs.

model_settings_fields frozenset[str] | None

Model settings fields to render. None selects the automatic fields for the tokenizer version (reasoning_effort for v15 and above, none below). An explicit set must be non-empty, requires v15 or above, and may only contain values from MODEL_SETTINGS_FIELD_CHOICES.

reasoning_effort_values tuple[str, ...] | None

Reasoning effort values exposed on requests. Required when the reasoning_effort model settings field is enabled.

reasoning_effort_default str | None

Exposed reasoning effort rendered when the request leaves the effort unset. Required when the reasoning_effort model settings field is enabled, and must be one of reasoning_effort_values.

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
Source code in src/mistral_common/integrations/chat_templates/template_generator.py
def build_chat_template(config: TemplateConfig) -> str:
    r"""Generate a complete chat template based on configuration.

    Args:
        config: Template configuration specifying version and features.

    Returns:
        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
    """
    if config.version == TokenizerVersion.v1:
        return _generate_v1_template(config)

    parts = []
    parts.append(_generate_header(config))
    parts.append(_generate_system_prompt_handling(config))
    parts.append(_generate_available_tools_definition(config))

    macros = _generate_macros(config)
    if macros:
        parts.append(macros)

    parts.append(_generate_message_aggregation(config))
    parts.append(_generate_alternation_check(config))
    parts.append("")
    parts.append(_generate_message_loop(config))
    parts.append("")

    return "\n".join(parts)