From 232e786cbe32e75fe8a85b0a3af528e312ba290e Mon Sep 17 00:00:00 2001 From: Fizza-Mukhtar Date: Sat, 7 Mar 2026 07:35:33 -0800 Subject: [PATCH 1/3] fix: handle missing add_generation_prompt in saved LoRA tokenizers --- unsloth/tokenizer_utils.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/unsloth/tokenizer_utils.py b/unsloth/tokenizer_utils.py index c445879df7..956ce21345 100644 --- a/unsloth/tokenizer_utils.py +++ b/unsloth/tokenizer_utils.py @@ -658,6 +658,21 @@ def _fix_chat_template(chat_template): dash = "-" if chosen_end.startswith("{%-") else "" + # Case 1: nothing after endfor - template was saved without generation prompt + if ( + not after_endfor.strip() + and "add_generation_prompt" not in chat_template + and "<|im_start|>" in chat_template + ): + after_endfor = ( + "{%" + dash + " if add_generation_prompt %}" + + "{{ '<|im_start|>assistant\\n' }}" + + "{%" + dash + " endif %}" + ) + chat_template = chat_template[: where + len(chosen_end)] + after_endfor + return chat_template + + # Case 2: original logic - something after endfor starting with {{ if ( "{%" + dash + " if" not in after_endfor and "{%" + dash + " set " not in after_endfor @@ -731,11 +746,14 @@ def fix_chat_template(tokenizer): "{% if add_generation_prompt %}" not in new_chat_template and "{%- if add_generation_prompt %}" not in new_chat_template ): - raise RuntimeError( + # Could not auto-fix - warn but don't crash so user can still + # load their saved LoRA adapter for inference/chat + logger.warning_once( f"Unsloth: The tokenizer `{tokenizer.name_or_path}`\n" - "does not have a {% if add_generation_prompt %} for generation purposes.\n" - f"Please file a bug report to the maintainers of `{tokenizer.name_or_path}` - thanks!" + "does not have a {{% if add_generation_prompt %}} for generation purposes.\n" + "Generation may not work correctly. Please notify the model maintainers." ) + return chat_template else: logger.warning_once( "Unsloth: We successfully patched the tokenizer to add a {% if add_generation_prompt %} to the chat_template.\n" From 334848d8d852a8f48f2d984b9db3811ea5b080a3 Mon Sep 17 00:00:00 2001 From: Fizza-Mukhtar Date: Sat, 7 Mar 2026 07:43:47 -0800 Subject: [PATCH 2/3] fix: use actual newline in ChatML generation prompt template --- unsloth/tokenizer_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unsloth/tokenizer_utils.py b/unsloth/tokenizer_utils.py index 956ce21345..f65bac61f5 100644 --- a/unsloth/tokenizer_utils.py +++ b/unsloth/tokenizer_utils.py @@ -666,7 +666,7 @@ def _fix_chat_template(chat_template): ): after_endfor = ( "{%" + dash + " if add_generation_prompt %}" - + "{{ '<|im_start|>assistant\\n' }}" + + "{{ '<|im_start|>assistant\n' }}" + "{%" + dash + " endif %}" ) chat_template = chat_template[: where + len(chosen_end)] + after_endfor From 661194623e3eb631f4e7946f5b21afebc321df3a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Mar 2026 15:44:08 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- unsloth/tokenizer_utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/unsloth/tokenizer_utils.py b/unsloth/tokenizer_utils.py index f65bac61f5..97fcd1365c 100644 --- a/unsloth/tokenizer_utils.py +++ b/unsloth/tokenizer_utils.py @@ -665,9 +665,13 @@ def _fix_chat_template(chat_template): and "<|im_start|>" in chat_template ): after_endfor = ( - "{%" + dash + " if add_generation_prompt %}" + "{%" + + dash + + " if add_generation_prompt %}" + "{{ '<|im_start|>assistant\n' }}" - + "{%" + dash + " endif %}" + + "{%" + + dash + + " endif %}" ) chat_template = chat_template[: where + len(chosen_end)] + after_endfor return chat_template