Skip to content

mistral_common.integrations.hf_tokenizers.response_template

ThinkingMode

Bases: StrEnum

How the model encodes thinking in assistant messages.

Attributes:

Name Type Description
none

No thinking support.

special

[THINK]/[/THINK] special tokens (v13+).

plain

Plain <think>/</think> text (v11 only).

convert_tokenizer_to_response_template(tokenizer, plain_thinking_support)

Generate the Hugging Face response template for a loaded MistralTokenizer.

Special-token thinking is detected from the tokenizer's special tokens. Plain thinking cannot be reliably detected: reasoning and non-reasoning v11 models can have identical tokenizer files, so when plain_thinking_support is None the same heuristic as convert_tokenizer_to_chat_template applies (v11 without audio).

See https://huggingface.co/docs/transformers/en/chat_response_parsing for the Transformers response parsing schema.

Parameters:

Name Type Description Default
tokenizer MistralTokenizer

An already-loaded MistralTokenizer.

required
plain_thinking_support bool | None

Whether to emit plain-thinking parsing for v11, or None for the v11-without-audio heuristic. Pass False to suppress.

required

Returns:

Type Description
dict[str, Any]

A JSON-serializable mapping suitable for a tokenizer configuration.

Raises:

Type Description
ValueError

If plain thinking is requested for a non-v11 tokenizer or one with audio support.

Source code in src/mistral_common/integrations/hf_tokenizers/response_template.py
def convert_tokenizer_to_response_template(
    tokenizer: MistralTokenizer, plain_thinking_support: bool | None
) -> dict[str, Any]:
    r"""Generate the Hugging Face response template for a loaded `MistralTokenizer`.

    Special-token thinking is detected from the tokenizer's special tokens.
    Plain thinking cannot be reliably detected: reasoning and non-reasoning
    v11 models can have identical tokenizer files, so when
    `plain_thinking_support` is `None` the same heuristic as
    `convert_tokenizer_to_chat_template` applies (v11 without audio).

    See https://huggingface.co/docs/transformers/en/chat_response_parsing for the
    Transformers response parsing schema.

    Args:
        tokenizer: An already-loaded `MistralTokenizer`.
        plain_thinking_support: Whether to emit plain-thinking parsing for v11,
            or `None` for the v11-without-audio heuristic. Pass `False` to suppress.

    Returns:
        A JSON-serializable mapping suitable for a tokenizer configuration.

    Raises:
        ValueError: If plain thinking is requested for a non-v11 tokenizer or one
            with audio support.
    """
    instruct_tokenizer = tokenizer.instruct_tokenizer
    inner_tokenizer = instruct_tokenizer.tokenizer
    version = tokenizer.version
    audio_support = instruct_tokenizer.audio_encoder is not None
    has_special_thinking = inner_tokenizer.is_special(SpecialTokens.begin_think.value) and inner_tokenizer.is_special(
        SpecialTokens.end_think.value
    )

    if plain_thinking_support is None:
        resolve_plain_thinking_support = version == TokenizerVersion.v11 and not audio_support
    else:
        resolve_plain_thinking_support = plain_thinking_support

    if resolve_plain_thinking_support:
        if version != TokenizerVersion.v11:
            raise ValueError("Plain thinking support is only available for tokenizer version v11")
        if audio_support:
            raise ValueError("Plain thinking support is not available for tokenizers with audio support")
        thinking_mode = ThinkingMode.plain
    elif has_special_thinking:
        thinking_mode = ThinkingMode.special
    else:
        thinking_mode = ThinkingMode.none

    return generate_response_template(tokenizer_version=version, thinking_mode=thinking_mode)

generate_response_template(tokenizer_version, thinking_mode)

Generate the Hugging Face response_template mapping.

See https://huggingface.co/docs/transformers/en/chat_response_parsing for the Transformers response parsing schema.

Parameters:

Name Type Description Default
tokenizer_version TokenizerVersion

Tokenizer version used to select field formats.

required
thinking_mode ThinkingMode

Thinking encoding to emit; must be compatible with the version. Note the audio-compatibility rule for plain is only checked by convert_tokenizer_to_response_template, which sees the tokenizer.

required

Returns:

Type Description
dict[str, Any]

A JSON-serializable mapping suitable for a tokenizer configuration.

Raises:

Type Description
ValueError

If thinking_mode is unsupported for the tokenizer version.

Examples:

>>> generate_response_template(
...     tokenizer_version=TokenizerVersion.v13,
...     thinking_mode=ThinkingMode.special,
... )["defaults"]
{'role': 'assistant'}
Source code in src/mistral_common/integrations/hf_tokenizers/response_template.py
def generate_response_template(tokenizer_version: TokenizerVersion, thinking_mode: ThinkingMode) -> dict[str, Any]:
    r"""Generate the Hugging Face `response_template` mapping.

    See https://huggingface.co/docs/transformers/en/chat_response_parsing for the
    Transformers response parsing schema.

    Args:
        tokenizer_version: Tokenizer version used to select field formats.
        thinking_mode: Thinking encoding to emit; must be compatible with the version.
            Note the audio-compatibility rule for `plain` is only checked by
            `convert_tokenizer_to_response_template`, which sees the tokenizer.

    Returns:
        A JSON-serializable mapping suitable for a tokenizer configuration.

    Raises:
        ValueError: If `thinking_mode` is unsupported for the tokenizer version.

    Examples:
        >>> generate_response_template(
        ...     tokenizer_version=TokenizerVersion.v13,
        ...     thinking_mode=ThinkingMode.special,
        ... )["defaults"]
        {'role': 'assistant'}
    """
    supported = _thinking_mode(tokenizer_version=tokenizer_version)
    if thinking_mode is not ThinkingMode.none and thinking_mode is not supported:
        raise ValueError(f"Thinking mode {thinking_mode} is not supported for tokenizer version {tokenizer_version}")

    return _build_response_template(version=tokenizer_version, thinking_mode=thinking_mode)