Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: save the chat template a base model was trained with (#12240)

Nilay419f447ce4ad
diff --git a/studio/backend/core/training/trainer.py b/studio/backend/core/training/trainer.py
index ec24f7ba5e7..ad2d3bbecbe 100644
--- a/studio/backend/core/training/trainer.py
+++ b/studio/backend/core/training/trainer.py
@@ -76,6 +76,7 @@ from utils.models.model_identity import restore_hf_cache_repo_identity
from utils.models.unsloth_mirror import unsloth_public_mirror
from utils.models.model_config import _env_offline
from utils.datasets import format_and_template_dataset
+from utils.datasets.chat_templates import get_training_chat_template
from utils.datasets.completion_masking import apply_completion_masking
from utils.datasets.iterable import is_streaming_dataset as detect_streaming_dataset
from utils.datasets.raw_text import prepare_raw_text_dataset, resolve_column_names
@@ -4032,6 +4033,9 @@ class UnslothTrainer:
str(dataset.get("final_format", "")).lower() if isinstance(dataset, dict) else ""
)
raw_text_mode = dataset_final_format == "raw_text"
+ self.tokenizer = get_training_chat_template(
+ self.tokenizer, self.model_name, dataset_final_format
+ )
data_collator = None
if is_deepseek_ocr:
diff --git a/studio/backend/tests/test_training_saves_dataset_chat_template.py b/studio/backend/tests/test_training_saves_dataset_chat_template.py
new file mode 100644
index 00000000000..67e1995af2b
--- /dev/null
+++ b/studio/backend/tests/test_training_saves_dataset_chat_template.py
@@ -0,0 +1,178 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+from __future__ import annotations
+
+import contextlib
+import sys
+from pathlib import Path
+
+_BACKEND = Path(__file__).resolve().parent.parent
+if str(_BACKEND) not in sys.path:
+ sys.path.insert(0, str(_BACKEND))
+
+import importlib # noqa: E402
+import types # noqa: E402
+from unittest.mock import MagicMock # noqa: E402
+
+import jinja2 # noqa: E402
+import pytest # noqa: E402
+from datasets import Dataset # noqa: E402
+
+
+_STUBBED: list[str] = []
+
+
+def _stub_if_missing(name, attrs):
+ if name in sys.modules:
+ return
+ try:
+ importlib.import_module(name)
+ return
+ except Exception: # noqa: BLE001 - stub unusable imports
+ pass
+ _STUBBED.append(name)
+ mod = types.ModuleType(name)
+ mod.__spec__ = None
+ for attr in attrs:
+ setattr(mod, attr, MagicMock())
+ sys.modules[name] = mod
+ parent, _, child = name.rpartition(".")
+ if parent and parent in sys.modules:
+ setattr(sys.modules[parent], child, mod)
+
+
+_stub_if_missing("unsloth", ("FastLanguageModel", "FastVisionModel", "is_bfloat16_supported"))
+_stub_if_missing("unsloth.chat_templates", ("get_chat_template",))
+_stub_if_missing("trl", ("SFTTrainer", "SFTConfig"))
+
+from core.training import trainer as tmod # noqa: E402
+from utils.datasets import format_and_template_dataset # noqa: E402
+
+for _name in reversed(_STUBBED):
+ sys.modules.pop(_name, None)
+
+
+BASE_EOS = "<|end_of_text|>"
+
+
+class _BaseModelTokenizer:
+ eos_token = BASE_EOS
+ chat_template = None
+
+ def apply_chat_template(self, conversation, **_kwargs):
+ if not self.chat_template:
+ raise ValueError(
+ "Cannot use chat template functions because tokenizer.chat_template is not set"
+ )
+ if self.chat_template == "alpaca":
+ raise ValueError("alpaca renders instruction rows, not conversations")
+ return "".join(f"<|im_start|>{t['role']}\n{t['content']}<|im_end|>\n" for t in conversation)
+
+
+def _get_chat_template(
+ tokenizer,
+ chat_template = "chatml",
+ **_kwargs,
+):
+ rebuilt = _BaseModelTokenizer()
+ rebuilt.chat_template = chat_template
+ if chat_template == "chatml":
+ rebuilt.eos_token = "<|im_end|>"
+ return rebuilt
+
+
+def trainer(monkeypatch):
+ unsloth = types.ModuleType("unsloth")
+ unsloth.chat_templates = types.ModuleType("unsloth.chat_templates")
+ unsloth.chat_templates.get_chat_template = _get_chat_template
+ monkeypatch.setitem(sys.modules, "unsloth", unsloth)
+ monkeypatch.setitem(sys.modules, "unsloth.chat_templates", unsloth.chat_templates)
+
+ monkeypatch.setattr(tmod, "should_use_mlx_training_backend", lambda *a, **k: False)
+ t = tmod.UnslothTrainer()
+ t.model_name = "unsloth/Llama-3.2-1B"
+ t.tokenizer = _BaseModelTokenizer()
+ return t
+
+
+def _train_with(
+ trainer,
+ dataset,
+ tmp_path,
+ monkeypatch,
+ own_template = None,
+):
+ captured = {}
+ monkeypatch.setattr(tmod, "SFTConfig", lambda **kwargs: kwargs, raising = False)
+ monkeypatch.setattr(tmod, "SFTTrainer", lambda **kwargs: captured.update(kwargs), raising = False)
+ monkeypatch.setattr(tmod, "resolve_output_dir", lambda p: tmp_path, raising = True)
+
+ trainer.tokenizer.chat_template = own_template
+ dataset_info = format_and_template_dataset(
+ dataset,
+ model_name = trainer.model_name,
+ tokenizer = trainer.tokenizer,
+ num_proc = 1,
+ )
+ assert dataset_info["success"], dataset_info["errors"]
+
+ # load_model swaps in the checkpoint's own tokenizer after the dataset is formatted.
+ trainer.model = object()
+ trainer.tokenizer = _BaseModelTokenizer()
+ trainer.tokenizer.chat_template = own_template
+
+ with contextlib.suppress(AttributeError, TypeError, ValueError, KeyError):
+ trainer._train_worker(
+ dataset_info,
+ batch_size = 1,
+ gradient_accumulation_steps = 1,
+ max_steps = 1,
+ output_dir = str(tmp_path),
+ )
+ assert captured, "SFTTrainer was never built, so this asserts nothing"
+ assert captured["tokenizer"] is trainer.tokenizer
+ return dataset_info
+
+
+CONVERSATION = Dataset.from_dict(
+ {"messages": [[{"role": "user", "content": "Hi"}, {"role": "assistant", "content": "Hello!"}]]}
+)
+
+
+def test_base_model_trains_and_saves_with_the_chatml_template_its_rows_used(
+ trainer, tmp_path, monkeypatch
+):
+ dataset_info = _train_with(trainer, CONVERSATION, tmp_path, monkeypatch)
+
+ assert dataset_info["dataset"]["text"][0].startswith("<|im_start|>user\nHi<|im_end|>")
+ assert trainer.tokenizer.chat_template == "chatml"
+ assert trainer.tokenizer.eos_token == "<|im_end|>"
+
+
+def test_base_model_trains_and_saves_with_the_alpaca_template_its_rows_used(
+ trainer, tmp_path, monkeypatch
+):
+ rows = Dataset.from_dict({"instruction": ["Say hi"], "input": [""], "output": ["Hello!"]})
+
+ dataset_info = _train_with(trainer, rows, tmp_path, monkeypatch)
+
+ row = dataset_info["dataset"]["text"][0]
+ assert row.endswith(f"Hello!{BASE_EOS}")
+ template = jinja2.Environment().from_string(trainer.tokenizer.chat_template)
+ render = lambda messages, **kw: template.render(
+ messages = messages, bos_token = "", eos_token = BASE_EOS, **kw
+ )
+ user = {"role": "user", "content": "Say hi"}
+ assert render([user, {"role": "assistant", "content": "Hello!"}]) == row
+ prompt = render([user], add_generation_prompt = True)
+ assert row.startswith(prompt) and row[len(prompt) :] == f"Hello!{BASE_EOS}"
+ assert trainer.tokenizer.eos_token == BASE_EOS
+
+
+def test_a_model_with_its_own_template_keeps_it(trainer, tmp_path, monkeypatch):
+ _train_with(trainer, CONVERSATION, tmp_path, monkeypatch, own_template = "own-template")
+
+ assert trainer.tokenizer.chat_template == "own-template"
+ assert trainer.tokenizer.eos_token == BASE_EOS
diff --git a/studio/backend/utils/datasets/chat_templates.py b/studio/backend/utils/datasets/chat_templates.py
index 2a4633930e2..68a6a210e7c 100644
--- a/studio/backend/utils/datasets/chat_templates.py
+++ b/studio/backend/utils/datasets/chat_templates.py
@@ -26,6 +26,27 @@ DEFAULT_ALPACA_TEMPLATE = """Below is an instruction that describes a task, pair
### Response:
{}"""
+# Renders a single-turn conversation byte-identical to a DEFAULT_ALPACA_TEMPLATE row with an empty input.
+STUDIO_ALPACA_CHAT_TEMPLATE = (
+ "{{ bos_token }}"
+ "{% if messages[0]['role'] == 'system' %}"
+ "{{ messages[0]['content'] + '\\n\\n' }}{% set loop_messages = messages[1:] %}"
+ "{% else %}"
+ "{{ '" + DEFAULT_ALPACA_TEMPLATE.split("\n\n", 1)[0] + "\\n\\n' }}{% set loop_messages = messages %}"
+ "{% endif %}"
+ "{% for message in loop_messages %}"
+ "{% if message['role'] == 'user' %}"
+ "{{ '### Instruction:\\n' + message['content'] + '\\n\\n### Input:\\n\\n\\n' }}"
+ "{% elif message['role'] == 'assistant' %}"
+ "{{ '### Response:\\n' + message['content'] + eos_token }}"
+ "{% if not loop.last %}{{ '\\n\\n' }}{% endif %}"
+ "{% else %}"
+ "{{ raise_exception('Only user and assistant roles are supported!') }}"
+ "{% endif %}"
+ "{% endfor %}"
+ "{% if add_generation_prompt %}{{ '### Response:\\n' }}{% endif %}"
+)
+
_TEMPLATE_ERROR_COLUMN = "__chat_template_error"
# Rows per batch when scanning or filtering the error column, so neither pass
@@ -112,6 +133,28 @@ def get_tokenizer_chat_template(tokenizer, model_name):
return tokenizer
+def get_training_chat_template(tokenizer, model_name, final_format):
+ if getattr(tokenizer, "chat_template", None):
+ return tokenizer
+ if final_format in ("chatml_messages", "chatml_conversations"):
+ return get_tokenizer_chat_template(tokenizer, model_name)
+ if final_format != "alpaca":
+ return tokenizer
+ try:
+ from unsloth.chat_templates import get_chat_template
+ tokenizer = get_chat_template(
+ tokenizer,
+ chat_template = "alpaca",
+ **_chat_template_kwargs(),
+ )
+ # Unsloth's "alpaca" template words the preamble differently and has no Input section.
+ _set_chat_template(tokenizer, STUDIO_ALPACA_CHAT_TEMPLATE)
+ logger.info(f"📝 Set alpaca chat template on tokenizer for model saving")
+ except Exception as e:
+ logger.info(f"⚠️ Could not set alpaca template on tokenizer: {e}")
+ return tokenizer
+
+
def _set_chat_template(tokenizer, chat_template):
"""Set on processor and tokenizer; does not undo ``get_chat_template`` EOS remapping (Gemma 1/2)."""
tokenizer.chat_template = chat_template
@@ -362,18 +405,7 @@ def apply_chat_template_to_dataset(
# ALPACA FORMAT
if final_format == "alpaca":
- # Set the alpaca chat template if unset, so it is saved for inference.
- if not (hasattr(tokenizer, 'chat_template') and tokenizer.chat_template):
- try:
- from unsloth.chat_templates import get_chat_template
- tokenizer = get_chat_template(
- tokenizer,
- chat_template = "alpaca",
- **_chat_template_kwargs(),
- )
- logger.info(f"📝 Set alpaca chat template on tokenizer for model saving")
- except Exception as e:
- logger.info(f"⚠️ Could not set alpaca template on tokenizer: {e}")
+ tokenizer = get_training_chat_template(tokenizer, model_name, final_format)
def _format_alpaca(examples):
texts = []