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
Llama 3.x and GPT-OSS templates: treat tool_calls None as no tool call (#17176)
Vineeth Sai Varikuntla211f0ee222a1
diff --git a/tests/python/test_chat_template_tool_calls_none.py b/tests/python/test_chat_template_tool_calls_none.pynew file mode 100644index 00000000000..75dc6699b29--- /dev/null+++ b/tests/python/test_chat_template_tool_calls_none.py@@ -0,0 +1,80 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.+"""datasets pads plain messages with `tool_calls: None`; templates must test the value."""++import pytest+from tokenizers import Tokenizer, models+from transformers import PreTrainedTokenizerFast++from unsloth.chat_templates import get_chat_template+++TEMPLATES = ["llama-3.1", "llama-3.2", "llama-3.3", "gpt-oss"]+CALL = [{"type": "function", "function": {"name": "get_weather", "arguments": {"city": "Paris"}}}]+PLAIN = [+ {"role": "system", "content": "Be brief."},+ {"role": "user", "content": "Hi"},+ {"role": "assistant", "content": "Hello!"},+ {"role": "user", "content": "Bye"},+ {"role": "assistant", "content": "Bye!"},+]+TOOL_FLOW = [+ {"role": "user", "content": "weather?"},+ {"role": "assistant", "content": None, "tool_calls": CALL},+ {"role": "tool", "content": "sunny"},+ {"role": "assistant", "content": "It is sunny."},+]++[email protected](autouse = True)+def _scratch_cwd(tmp_path, monkeypatch):+ # get_chat_template writes a scratch dir under CWD; isolate it per test and xdist worker.+ monkeypatch.chdir(tmp_path)+++def _tokenizer(name):+ vocab = {"<unk>": 0, "<s>": 1, "</s>": 2, "<pad>": 3}+ tokenizer = PreTrainedTokenizerFast(+ tokenizer_object = Tokenizer(models.WordLevel(vocab, unk_token = "<unk>")),+ bos_token = "<s>",+ eos_token = "</s>",+ unk_token = "<unk>",+ pad_token = "<pad>",+ )+ return get_chat_template(tokenizer, name)+++def _render(tokenizer, messages, add_generation_prompt):+ return tokenizer.apply_chat_template(+ messages, tokenize = False, add_generation_prompt = add_generation_prompt+ )++[email protected]("filler", [None, []], ids = ["none", "empty_list"])[email protected]("add_generation_prompt", [False, True])[email protected]("conversation", [PLAIN, TOOL_FLOW], ids = ["plain", "tool_flow"])[email protected]("name", TEMPLATES)+def test_blank_tool_calls_match_a_missing_key(name, conversation, add_generation_prompt, filler):+ tokenizer = _tokenizer(name)+ padded = [dict(m, tool_calls = m.get("tool_calls", filler)) for m in conversation]++ expected = _render(tokenizer, conversation, add_generation_prompt)++ assert ("get_weather" in expected) == (conversation is TOOL_FLOW)+ assert _render(tokenizer, padded, add_generation_prompt) == expected+++def test_gpt_oss_drops_the_tool_call_analysis_when_a_final_answer_follows():+ conversation = [+ TOOL_FLOW[0],+ {"role": "assistant", "content": None, "thinking": "Check it.", "tool_calls": CALL},+ TOOL_FLOW[2],+ TOOL_FLOW[3],+ ]+ padded = [dict(m, tool_calls = m.get("tool_calls")) for m in conversation]+ tokenizer = _tokenizer("gpt-oss")++ expected = _render(tokenizer, conversation, False)++ assert "Check it." not in expected+ assert _render(tokenizer, padded, False) == expecteddiff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.pyindex 709b53137c3..97624a04119 100644--- a/unsloth/chat_templates.py+++ b/unsloth/chat_templates.py@@ -711,9 +711,9 @@ llama31_template = \{%- endif %}{%- for message in messages %}- {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %}+ {%- if not (message.role == 'ipython' or message.role == 'tool' or message.tool_calls) %}{{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] + '<|eot_id|>' }}- {%- elif 'tool_calls' in message %}+ {%- elif message.tool_calls %}{%- if not message.tool_calls|length == 1 %}{{- raise_exception("This model only supports single tool-calls at once!") }}{%- endif %}@@ -2112,13 +2112,13 @@ gptoss_template = \{{- raise_exception("You have passed a message containing <|channel|> tags in the thinking field. Instead of doing this, you should pass analysis messages (the string between '<|message|>' and '<|end|>') in the 'thinking' field, and final messages (the string between '<|message|>' and '<|end|>') in the 'content' field.") }}{%- endif %}{%- endif %}- {%- if "tool_calls" in message %}+ {%- if message.tool_calls %}{#- We need very careful handling here - we want to drop the tool call analysis message if the model #}{#- has output a later <|final|> message, but otherwise we want to retain it. This is the only case #}{#- when we render CoT/analysis messages in inference. #}{%- set future_final_message = namespace(found=false) %}{%- for future_message in loop_messages[loop.index:] %}- {%- if future_message.role == 'assistant' and "tool_calls" not in future_message %}+ {%- if future_message.role == 'assistant' and not future_message.tool_calls %}{%- set future_final_message.found = true %}{%- endif %}{%- endfor %}