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.

feat: apply default chat template for base models without tokenizer chat_template

Roland Tannousb32ad350c528
diff --git a/studio/backend/utils/datasets/chat_templates.py b/studio/backend/utils/datasets/chat_templates.py
index a868109f5d0..e3b1bef3159 100644
--- a/studio/backend/utils/datasets/chat_templates.py
+++ b/studio/backend/utils/datasets/chat_templates.py
@@ -60,7 +60,24 @@ def get_tokenizer_chat_template(tokenizer, model_name):
print(f"⚠️ Failed to apply Unsloth template '{matched_template}': {e}")
print(f" Falling back to tokenizer's default chat template")
else:
- print(f"📝 Using tokenizer's default chat template (no Unsloth template match)")
+ # Check if tokenizer actually has a chat_template set
+ has_chat_template = (
+ hasattr(tokenizer, 'chat_template')
+ and tokenizer.chat_template is not None
+ )
+ if has_chat_template:
+ print(f"📝 Using tokenizer's own chat template (no Unsloth template match)")
+ else:
+ # Base model with no chat template — apply default ChatML
+ print(f"📝 No chat template found — applying default ChatML template (base model)")
+ try:
+ tokenizer = get_chat_template(
+ tokenizer,
+ chat_template="chatml",
+ )
+ except Exception as e:
+ print(f"⚠️ Failed to apply default ChatML template: {e}")
+ print(f" Falling back to tokenizer as-is")
return tokenizer
@@ -227,6 +244,16 @@ def apply_chat_template_to_dataset(
# ALPACA FORMAT
if final_format == "alpaca":
+ # Set alpaca chat template on tokenizer for saving (if not already set)
+ # This ensures the template is saved with the model 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")
+ print(f"📝 Set alpaca chat template on tokenizer for model saving")
+ except Exception as e:
+ print(f"⚠️ Could not set alpaca template on tokenizer: {e}")
+
# Use custom template if provided
def _format_alpaca_custom(examples):
texts = []