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.

Render a static prefix when default_system_message is None (#8117)

Vineeth Sai Varikuntla31775230919e
diff --git a/tests/python/test_construct_chat_template_validation.py b/tests/python/test_construct_chat_template_validation.py
index 0202d99430b..ad6cba07ecc 100644
--- a/tests/python/test_construct_chat_template_validation.py
+++ b/tests/python/test_construct_chat_template_validation.py
@@ -201,6 +201,20 @@ def test_static_prefix_without_system_still_rejects_system_message():
)
[email protected]("default_system_message", [None, "You are helpful."])
+def test_static_prefix_without_system_renders_in_every_conversation(default_system_message):
+ """A static prefix must render regardless of the default system message."""
+ modelfile, jinja_template, _, _ = construct_chat_template(
+ tokenizer = _SuccessFakeTokenizer(),
+ chat_template = _NO_SYSTEM_CHAT_TEMPLATE,
+ default_system_message = default_system_message,
+ extra_eos_tokens = ["</s>"],
+ )
+ rendered = _render(jinja_template, [{"role": "user", "content": "Hi"}])
+ assert rendered.startswith("PREAMBLE\n"), rendered
+ assert "PREAMBLE\n" in modelfile.split("TEMPLATE ")[1]
+
+
def test_auto_appended_eos_prefers_the_tokenizer_eos_deterministically():
"""When the template has no EOS after {OUTPUT}, construct_chat_template appends one
itself and picks `extra_eos_tokens[0]`. `extra_eos_tokens.insert(0, tokenizer.eos_token)`
@@ -317,3 +331,22 @@ def test_bos_token_with_quote_or_backslash_is_not_emitted_twice():
):
rendered = _render(jinja_template, messages, bos_token = bos)
assert rendered.count(bos) == 1, rendered
+
+
+def test_bos_only_prefix_still_rejects_system_message():
+ bos = _BosFakeTokenizer.bos_token
+ _, jinja_template, _, _ = construct_chat_template(
+ tokenizer = _BosFakeTokenizer(),
+ chat_template = bos + _NO_SYSTEM_CHAT_TEMPLATE.removeprefix("PREAMBLE\n"),
+ default_system_message = None,
+ extra_eos_tokens = ["</s>"],
+ )
+ with pytest.raises(RuntimeError, match = "Only user and assistant roles are supported!"):
+ _render(
+ jinja_template,
+ [
+ {"role": "system", "content": "Be terse."},
+ {"role": "user", "content": "Hi"},
+ ],
+ bos_token = bos,
+ )
diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.py
index eee361d2c4a..34710c4812c 100644
--- a/unsloth/chat_templates.py
+++ b/unsloth/chat_templates.py
@@ -2702,6 +2702,7 @@ extra_eos_tokens = None,
system_part = system_part.replace(tokenizer.bos_token, "", 1)
partial_system = process(system_part, "{SYSTEM}", "messages[0]['content']")
partial_system = partial_system.replace("{SYSTEM}", "")
+ system_expr = partial_system
if "{SYSTEM}" in partial_system:
if default_system_message is None:
@@ -2726,7 +2727,12 @@ extra_eos_tokens = None,
"{% set loop_messages = messages %}"\
"{% endif %}"
else:
- partial_system += "{% endif %}"
+ # Emit a static prefix on both branches so the collapse below
+ # makes it unconditional.
+ partial_system += "{% else %}"\
+ "{{ " + system_expr + " }}"\
+ "{% set loop_messages = messages %}"\
+ "{% endif %}"
jinja_template = partial_system + jinja_template
@@ -2743,7 +2749,7 @@ extra_eos_tokens = None,
# Check if system part is the same!
jinja_template = re.sub(
- r"\{\% if messages\[0\]\['role'\] \=\= 'system' \%\}\{\{ '(.+?)' \}\}"\
+ r"\{\% if messages\[0\]\['role'\] \=\= 'system' \%\}\{\{ '(.*?)' \}\}"\
r"\{\% set loop\_messages \= messages\[1\:\] \%\}"\
r"\{\% else \%\}\{\{ '\1' \}\}\{\% set loop\_messages \= messages \%\}\{\% endif \%\}"\
r"\{\% for message in loop\_messages \%\}",