Skip to content

Add Mistral Medium 3.5 training chat template - #7940

Merged
albertvillanova merged 7 commits into
mainfrom
mistral-medium-3-5-template
Oct 29, 2026
Merged

albertvillanova merged 7 commits into
mainfrom
mistral-medium-3-5-template

Conversation

@qgallouedec

@qgallouedec qgallouedec commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Adds Mistral Medium 3.5's chat template (mistralai/Mistral-Medium-3.5-128B) and its training variant, so assistant_only_loss=True works for it. The original is already prefix-preserving: the training template only wraps the assistant turn (content, tool calls and </s>) in {% generation %} markers.

It also fixes processors that return tensors from apply_chat_template (PixtralProcessor defaults to return_tensors="pt"): get_training_chat_template died with "Boolean value of Tensor with more than one value is ambiguous" and is_chat_template_stop_token_trained returned False. Same unbatching in GRPO's and Distillation's _get_tool_suffix_ids.

  • New tiny model trl-internal-testing/tiny-Mistral3ForConditionalGeneration (script added), used in the tool-calling and training-template tests.

Note

Low Risk
Changes are scoped to chat-template plumbing and VLM processor edge-case handling, with no auth or training-core logic altered beyond safer ID normalization.

Overview
Adds Mistral Medium 3.5 (mistralai/Mistral-Medium-3.5-128B) to TRL’s bundled chat templates: a reference mistral_medium_3_5.jinja and a training variant that only wraps the assistant turn (content, tool calls, EOS) in {% generation %} markers, since the base template is already prefix-preserving for GRPO tools.

Pixtral / Mistral processors can return torch.Tensor IDs from apply_chat_template even when lists are expected. The PR normalizes those tensors to lists in prefix-preserving checks, stop-token training checks, and GRPO/Distillation _get_tool_suffix_ids, fixing template selection and tool-suffix logic for this family.

Docs and check_chat_templates.py list the new family; a tiny Mistral3 hub script and test parametrization exercise tool-calling and training-template behavior (transformers β‰₯ 5.0, vision marks).

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
πŸ“ Code Review βœ… Completed 2026-10-27T14:08:51.516192Z e3753dd PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with πŸ‘€ while any review is running, comments if it has suggestions, and reacts with πŸ‘ once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ’‘ Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e3753dd2e8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with πŸ‘.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1257 to +1258
if chat_template == mistral_medium_3_5_chat_template:
return mistral_medium_3_5_training_chat_template

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Register Mistral response parsing before enabling tool training

When this processor is passed to GRPOTrainer or DistillationTrainer with tools, the constructors call add_response_schema before reaching this training-template mapping. The new tiny processor is copied directly from the checkpoint without adding a response_template or response_schema, while add_response_schema has no branch for mistral_medium_3_5_chat_template; initialization therefore raises Unrecognized chat template and never reaches either this mapping or the newly fixed _get_tool_suffix_ids path. Add the corresponding response parser mapping and cover the trainer's public initialization path rather than only supports_tool_calling.

AGENTS.md reference: AGENTS.md:L66-L70

Useful? React with πŸ‘Β / πŸ‘Ž.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this PR. A response parser alone moves the failure further down: GRPO's prompt tokenization also breaks on Pixtral processors, which return tensors, so the tool loop then fails on Tensor + list. Supporting GRPO with tools on Mistral 3 needs both, plus a place in the GRPO VLM tests, so it gets its own PR. This one is about SFT (the training template) and the template checks.

@bot-ci-comment

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@albertvillanova albertvillanova left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I checked the stored template against the Hub, the training-template diff, and the new tests on an H100. With the tensor-to-list conversion removed from chat_template_utils.py, 12 of the 13 mistral3 tests fail, so the fix is needed and covered. With assistant_only_loss=True, the mask covers both assistant turns and their </s>, and nothing from the system or user turns.

Two comments:

  1. The tiny model is now FP8, and with the current transformers release it can't be trained: LoRA fails with "quantized with fp8 but that quantization method do not support training", and full fine-tuning with "cannot perform fine-tuning on purely quantized models". This PR only loads the processor, so nothing breaks here. With huggingface/transformers#50031, LoRA on the unmodified tiny trains (SFT with assistant_only_loss=True), so the GRPO tests planned in the follow-up can use a PEFT config and be gated on the transformers release that ships it.

  2. Nit on the skip reason "Ministral 3 was introduced in transformers-5.0.0": these tests only load the processor, and what needs 5.0 there is the tokenizer. The tiny's tokenizer class is TokenizersBackend, added in 5.0.0 (huggingface/transformers#40936); on 4.57.6, AutoProcessor.from_pretrained fails with "Tokenizer class TokenizersBackend does not exist". Maybe "Mistral Medium 3.5 tokenizer requires transformers>=5.0.0", like the GLM4 and LFM2.5 params next to it?

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit b7aa518. Configure here.

@albertvillanova
albertvillanova merged commit f66d9e8 into main Oct 29, 2026
13 checks passed
@albertvillanova
albertvillanova deleted the mistral-medium-3-5-template branch October 29, 2026 09:21
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.

2 participants