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.

get_chat_template: apply mapping when the role key is not renamed (#16425)

Vineeth Sai Varikuntlae1034a1d62ed
diff --git a/tests/python/test_get_chat_template_sharegpt_mapping.py b/tests/python/test_get_chat_template_sharegpt_mapping.py
index 6fc47fb67df..d387bf97fd2 100644
--- a/tests/python/test_get_chat_template_sharegpt_mapping.py
+++ b/tests/python/test_get_chat_template_sharegpt_mapping.py
@@ -100,3 +100,59 @@ def test_custom_template_reading_sharegpt_keys_keeps_working():
mapped = get_chat_template(_tokenizer(), (template, "eos_token"), mapping = SHAREGPT)
assert _render(mapped, sharegpt, False) == "<s>User: Hi\nBot: Hello!</s>\nUser: Bye\n"
+
+
[email protected]("name", ["llama-3.1", "chatml"])
+def test_mapping_that_keeps_the_role_key_still_renames_role_values(name):
+ mapping = {"role": "role", "content": "content", "user": "human", "assistant": "gpt"}
+ renamed = [{"role": SHAREGPT_ROLES[role], "content": text} for role, text in CONVERSATION]
+ role_content = [{"role": role, "content": text} for role, text in CONVERSATION]
+
+ expected = _render(get_chat_template(_tokenizer(), name), role_content, True)
+ mapped = get_chat_template(_tokenizer(), name, mapping = mapping)
+
+ assert _render(mapped, renamed, True) == expected
+ assert _render(mapped, role_content, True) == expected
+
+
[email protected]("name", ["llama-3.1", "chatml"])
+def test_mapping_that_only_renames_the_content_key(name):
+ mapping = {"role": "role", "content": "text", "user": "user", "assistant": "assistant"}
+ renamed = [{"role": role, "text": text} for role, text in CONVERSATION]
+ role_content = [{"role": role, "content": text} for role, text in CONVERSATION]
+
+ expected = _render(get_chat_template(_tokenizer(), name), role_content, True)
+ mapped = get_chat_template(_tokenizer(), name, mapping = mapping)
+
+ assert _render(mapped, renamed, True) == expected
+ assert _render(mapped, role_content, True) == expected
+
+
[email protected]("name", ["gpt-oss", "qwen3-instruct"])
+def test_sharegpt_mapping_with_content_less_tool_call_first(name):
+ call = [{"type": "function", "function": {"name": "f", "arguments": {}}}]
+ sharegpt = [{"from": "gpt", "tool_calls": call}, {"from": "human", "value": "ok"}]
+ role_content = [{"role": "assistant", "tool_calls": call}, {"role": "user", "content": "ok"}]
+
+ expected = _render(get_chat_template(_tokenizer(), name), role_content, False)
+ mapped = get_chat_template(_tokenizer(), name, mapping = SHAREGPT)
+
+ assert "ok" in expected
+ assert _render(mapped, sharegpt, False) == expected
+
+
[email protected]("name", ["llama-3.1", "qwen3-instruct"])
+def test_content_only_mapping_keeps_canonical_tool_call_first_history(name):
+ call = [{"type": "function", "function": {"name": "f", "arguments": {}}}]
+ canonical = [
+ {"role": "assistant", "tool_calls": call},
+ {"role": "tool", "content": "42"},
+ {"role": "user", "content": "ok"},
+ ]
+ mapping = {"role": "role", "content": "text", "user": "user", "assistant": "assistant"}
+
+ expected = _render(get_chat_template(_tokenizer(), name), canonical, False)
+ mapped = get_chat_template(_tokenizer(), name, mapping = mapping)
+
+ assert "42" in expected and "ok" in expected
+ assert _render(mapped, canonical, False) == expected
diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.py
index d7466b05bae..6dca6642994 100644
--- a/unsloth/chat_templates.py
+++ b/unsloth/chat_templates.py
@@ -2692,13 +2692,14 @@ def get_chat_template(
for key in ("role", "content", "user", "assistant")
)
chat_template = \
- "{%- if 'role' not in messages[0] -%}" + \
+ "{%- if " + role + " in messages[0] -%}" + \
"{%- set sharegpt = namespace(messages = []) -%}" + \
"{%- for message in messages -%}" + \
"{%- set role = {" + user + " : 'user', " + assistant + " : 'assistant'}" + \
".get(message[" + role + "], message[" + role + "]) -%}" + \
"{%- set sharegpt.messages = sharegpt.messages + " + \
- "[dict(message, role = role, content = message[" + content + "])] -%}" + \
+ "[dict(message, role = role, content = message[" + content + "]) if " + content + " in message " + \
+ "else dict(message, role = role)] -%}" + \
"{%- endfor -%}" + \
"{%- set messages = sharegpt.messages -%}" + \
"{%- endif %}" + \