Skip to content

feat: add Mistral training chat template with generation markers - #6689

Open
RudrenduPaul wants to merge 1 commit into
huggingface:mainfrom
RudrenduPaul:feat/mistral-training-chat-template
Open

RudrenduPaul wants to merge 1 commit into
huggingface:mainfrom
RudrenduPaul:feat/mistral-training-chat-template

Conversation

@RudrenduPaul

@RudrenduPaul RudrenduPaul commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds a training-compatible variant of the Mistral chat template (as shipped by mistralai/Mistral-7B-Instruct-v0.3 and related checkpoints) with {% generation %} / {% endgeneration %} markers, enabling return_assistant_tokens_mask=True for assistant-only loss masking in SFT training.

Contributes to #5471 (Tracking: add {% generation %} chat templates for common model families).

Files changed:

  • trl/chat_templates/mistral.jinja — original Mistral chat template (source of truth for the exact-match comparison in get_training_chat_template()); verified byte-identical to the live mistralai/Mistral-7B-Instruct-v0.3 tokenizer's chat_template
  • trl/chat_templates/mistral_training.jinja — training variant: same text output as the original, with {% generation %} / {% endgeneration %} wrapping both assistant branches (the [TOOL_CALLS] ... branch and the plain-content branch)
  • trl/chat_template_utils.py — loads both templates; adds a Mistral branch (alphabetically between LLaMA 3 and Phi-3) in get_training_chat_template(); updates the docstring's supported-model list
  • scripts/generate_tiny_models/for_causal_lm/mistral_for_causal_lm_0_3.py — generator script for a trl-internal-testing/tiny-MistralForCausalLM-0.3 fixture, following the existing mistral_for_causal_lm_0_1.py / _0_2.py pattern

Template design: Mistral's v0.3 template supports tool calling ([AVAILABLE_TOOLS] / [TOOL_CALLS] / [TOOL_RESULTS]), but every tool_call.id (and tool_call_id) must be a 9-character alphanumeric string or the template raises. TRL's supports_tool_calling() probe uses a generic tool call with no id, so it hits that raise and correctly classifies this template as not supporting tool calling — which means get_training_chat_template()'s prefix_ok check short-circuits to True and no prefix-preservation fix is required, only the generation markers.

Testing note: No trl-internal-testing/tiny-Mistral* fixture currently carries the v0.3-era tool-calling template (the existing tiny-MistralForCausalLM-0.1 / -0.2 fixtures carry the older, simpler v0.1/v0.2 template, which has no tool-calling support at all and predates this one). I've included the generator script above so a maintainer can push trl-internal-testing/tiny-MistralForCausalLM-0.3 (same flow as noted earlier in #5471 for other new-model fixtures); happy to add the TestGetTrainingChatTemplate / TestSupportsToolCalling parametrize entries in a follow-up once it's live.

In the meantime, I validated the new template directly against the real mistralai/Mistral-7B-Instruct-v0.3 tokenizer, mirroring the existing test suite's own checks:

  • mistral.jinja is byte-identical to the live chat_template
  • supports_tool_calling() is False (as expected, see above)
  • get_training_chat_template() returns the new patched template, which carries {% generation %} markers
  • Rendered text is unchanged before/after patching for: single user turn (with/without generation prompt), multi-turn plain-content conversations, assistant turns with tool_calls, and system-message + tools conversations
  • is_chat_template_stop_token_trained() passes
  • return_assistant_tokens_mask=True produces masks that decode to exactly the assistant-authored spans, for both plain-content and tool-call turns

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline, Pull Request section?
  • Was this discussed/approved via a GitHub issue? Please add a link to it if that's the case. — Tracking: Add {% generation %} chat templates for common model families #5471
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests? — no fixture exists yet for this template (see testing note below); happy to add the parametrize entries once trl-internal-testing/tiny-MistralForCausalLM-0.3 is pushed

AI writing disclosure

  • No AI usage: the PR was written entirely by a human.
  • AI-assisted: some parts were suggested or improved by AI, but the PR was written and reviewed by a human.
  • AI-generated: the PR was mostly or fully generated by an AI tool.

Who can review?

@qgallouedec (opened #5471)


Note

Low Risk
Changes are limited to chat-template assets, routing in chat_template_utils.py, and a dev-only tiny model script; no runtime training or auth paths are modified.

Overview
Adds Mistral Instruct v0.3 chat templates so TRL can patch SFT training the same way as other model families (#5471).

mistral.jinja mirrors the upstream mistralai/Mistral-7B-Instruct-v0.3 template ([INST], [AVAILABLE_TOOLS], [TOOL_CALLS], [TOOL_RESULTS]). mistral_training.jinja is identical for rendered text but wraps both assistant paths (plain content and tool_calls) in {% generation %} / {% endgeneration %} for return_assistant_tokens_mask=True. No prefix-preservation patch is added because supports_tool_calling() fails on the stock probe (missing 9-char tool_call.id).

get_training_chat_template() in chat_template_utils.py loads both templates and returns the training variant when the tokenizer template matches mistral.jinja; docs in trl/chat_templates/README.md are updated accordingly.

A mistral_for_causal_lm_0_3.py tiny-model generator script is added for a future trl-internal-testing/tiny-MistralForCausalLM-0.3 fixture; automated test parametrization is not in this PR.

Reviewed by Cursor Bugbot for commit e1c0895. Bugbot is set up for automated code reviews on this repo. Configure here.

Adds a training-compatible variant of the Mistral chat template (as shipped
by mistralai/Mistral-7B-Instruct-v0.3) with {% generation %} / {% endgeneration
%} markers, enabling return_assistant_tokens_mask=True for assistant-only
loss masking in SFT training. No prefix-preservation fix is needed: the
generic supports_tool_calling() probe omits tool_call.id, which this
template requires (9-char alphanumeric), so it already raises and is
correctly classified as not supporting tool calling.

Also adds the tiny-model generator script for
trl-internal-testing/tiny-MistralForCausalLM-0.3 so the new dispatch branch
can get automated test coverage once a maintainer pushes it.

Contributes to huggingface#5471.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant