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.

Qwen 2.5 template: use the chosen system message when tools are passed (#16932)

Vineeth Sai Varikuntla300409b88e37
diff --git a/tests/python/test_get_chat_template_escaping.py b/tests/python/test_get_chat_template_escaping.py
index ea95a19a292..7d457bce293 100644
--- a/tests/python/test_get_chat_template_escaping.py
+++ b/tests/python/test_get_chat_template_escaping.py
@@ -33,13 +33,26 @@ MESSAGES = [
]
-def _render(template):
+TOOLS = [
+ {
+ "type": "function",
+ "function": {
+ "name": "get_weather",
+ "description": "Get weather",
+ "parameters": {"type": "object", "properties": {}},
+ },
+ }
+]
+
+
+def _render(template, **kwargs):
environment = ImmutableSandboxedEnvironment(trim_blocks = True, lstrip_blocks = True)
return environment.from_string(template).render(
messages = [{"role": "user", "content": "Hi"}],
bos_token = "<s>",
eos_token = "</s>",
add_generation_prompt = False,
+ **kwargs,
)
@@ -62,6 +75,18 @@ def test_default_system_message_renders_verbatim(name):
assert default in _render(template)
[email protected]("name", TEMPLATES_WITH_SYSTEM_MESSAGE)
+def test_system_message_is_used_when_tools_are_passed(name):
+ template, _ = _change_system_message(CHAT_TEMPLATES[name][0], name, "You are a pirate.")
+ assert "You are a pirate." in _render(template, tools = TOOLS)
+
+
[email protected]("name", TEMPLATES_WITH_SYSTEM_MESSAGE)
+def test_default_system_message_is_used_when_tools_are_passed(name):
+ template, _ = _change_system_message(CHAT_TEMPLATES[name][0], name, None)
+ assert DEFAULT_SYSTEM_MESSAGE[name] in _render(template, tools = TOOLS)
+
+
@pytest.mark.parametrize("name", ["vicuna", "vicuna_old", "vicuna old"])
def test_vicuna_default_has_a_plain_apostrophe(name):
assert "\\" not in DEFAULT_SYSTEM_MESSAGE[name]
diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.py
index 877c6462bea..7a005ce880f 100644
--- a/unsloth/chat_templates.py
+++ b/unsloth/chat_templates.py
@@ -778,7 +778,7 @@ qwen25_template = \
{%- if messages[0][\'role\'] == \'system\' %}
{{- messages[0][\'content\'] }}
{%- else %}
- {{- \'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.\' }}
+ {{- \'{system_message}\' }}
{%- endif %}
{{- "\\n\\n# Tools\\n\\nYou may call one or more functions to assist with the user query.\\n\\nYou are provided with function signatures within <tools></tools> XML tags:\\n<tools>" }}
{%- for tool in tools %}