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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Strip Llama 3's bos_token set statement when deduping a chat template (#17316)
Vineeth Sai Varikuntlaad201d786c13
diff --git a/tests/python/test_gemma4_base_bos_token.py b/tests/python/test_gemma4_base_bos_token.pyindex 4a45d7db281..790833241cc 100644--- a/tests/python/test_gemma4_base_bos_token.py+++ b/tests/python/test_gemma4_base_bos_token.py@@ -258,6 +258,18 @@ LLAMA2_TEMPLATE = ()+# Llama 3 adds bos_token to the first turn through a set statement, not a `{{ }}` expression.+LLAMA3_TEMPLATE = (+ "{% set loop_messages = messages %}"+ "{% for message in loop_messages %}"+ "{% set content = '<|start_header_id|>' + message['role'] + '<|end_header_id|>\\n\\n'"+ " + message['content'] | trim + '<|eot_id|>' %}"+ "{% if loop.index0 == 0 %}{% set content = bos_token + content %}{% endif %}"+ "{{ content }}"+ "{% endfor %}"+)++def _render(template, **kwargs):jinja2 = pytest.importorskip("jinja2")return jinja2.Template(template).render(@@ -289,6 +301,19 @@ def test_dedupe_leaves_a_renderable_template_for_an_expression_bos():assert _render(tok.chat_template) == "[INST] hi [/INST]"+def test_stripping_bos_from_a_set_statement_removes_it():+ stripped = tu._strip_bos_from_chat_template_text(LLAMA3_TEMPLATE)+ assert "bos_token" not in stripped+ assert _render(stripped) == "<|start_header_id|>user<|end_header_id|>\n\nhi<|eot_id|>"+++def test_dedupe_removes_bos_set_in_a_statement():+ tok = _gemma4_base(add_bos_token = True, chat_template = LLAMA3_TEMPLATE)+ tu._dedupe_bos_chat_template(tok)+ assert tu._chat_template_emits_bos(tok) is False+ assert _render(tok.chat_template).startswith("<|start_header_id|>")++def test_export_helper_strips_dict_chat_template_without_crash():tok = _gemma4_base(add_bos_token = True,diff --git a/unsloth/tokenizer_utils.py b/unsloth/tokenizer_utils.pyindex ceef1e5e72f..257a20dcc42 100644--- a/unsloth/tokenizer_utils.py+++ b/unsloth/tokenizer_utils.py@@ -199,8 +199,8 @@ def _strip_bos_from_chat_template_text(chat_template):if not isinstance(chat_template, str) or not chat_template:return chat_templatestripped = re.sub(r"\{[\s\-]*\{[\s\-]*bos\_token[\s\-]*\}[\s\-]*\}", "", chat_template, count = 1)- # Keep the opening `{{` for Llama 2 expressions so removing bos_token does not leave a dangling `}}`.- return re.sub(r"(\{[\s\-]*\{[\s\-]*)bos\_token[\s\-]*\+[\s\-]*", r"\1", stripped, count = 1)+ # Drop only the `bos_token +` term: Llama 2's `{{ }}` and Llama 3's `{% set %}` must survive.+ return re.sub(r"\bbos\_token\s*\+\s*", "", stripped, count = 1)def _dedupe_bos_chat_template(tokenizer):