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.

Let the qwen3 chat template render tool-call turns with null content (#17175)

Vineeth Sai Varikuntla9fdbdd55670a
diff --git a/studio/backend/utils/datasets/chat_templates.py b/studio/backend/utils/datasets/chat_templates.py
index 9bf1e7da752..02768ce705d 100644
--- a/studio/backend/utils/datasets/chat_templates.py
+++ b/studio/backend/utils/datasets/chat_templates.py
@@ -362,7 +362,7 @@ def _thinking_as_reasoning_content(tokenizer, conversation):
def _render_with_empty_call_content(tokenizer, messages, tools_kwargs):
- # Unsloth's qwen3 reads content unguarded; kept only if unique per-call probes render, as names may appear elsewhere.
+ # For templates reading content unguarded; kept only if unique per-call probes render, as names may appear elsewhere.
from core.inference.chat_template_helpers import _normalize_tool_call_arguments
def is_null_call(message):
diff --git a/tests/python/test_qwen3_template_null_content.py b/tests/python/test_qwen3_template_null_content.py
new file mode 100644
index 00000000000..e4c78e56498
--- /dev/null
+++ b/tests/python/test_qwen3_template_null_content.py
@@ -0,0 +1,66 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+import pytest
+from tokenizers import Tokenizer, models
+from transformers import PreTrainedTokenizerFast
+
+from unsloth.chat_templates import get_chat_template
+
+
[email protected](autouse = True)
+def _scratch_cwd(tmp_path, monkeypatch):
+ monkeypatch.chdir(tmp_path)
+
+
+def _render(name, messages):
+ vocab = {"<unk>": 0, "<s>": 1, "</s>": 2, "<pad>": 3}
+ tokenizer = get_chat_template(
+ PreTrainedTokenizerFast(
+ tokenizer_object = Tokenizer(models.WordLevel(vocab, unk_token = "<unk>")),
+ bos_token = "<s>",
+ eos_token = "</s>",
+ unk_token = "<unk>",
+ pad_token = "<pad>",
+ ),
+ name,
+ )
+ return tokenizer.apply_chat_template(messages, tokenize = False)
+
+
+CALL = {"type": "function", "function": {"name": "get_weather", "arguments": {"city": "Paris"}}}
+ASK = {"role": "user", "content": "Weather?"}
+RESULT = {"role": "tool", "content": "sunny"}
+ANSWER = {"role": "assistant", "content": "It is sunny."}
+
+
+def _conversations(call_turn):
+ return {
+ "tool_flow": [ASK, call_turn, RESULT, ANSWER],
+ "ends_on_call": [ASK, call_turn],
+ }
+
+
[email protected]("name", ["qwen3", "qwen-3"])
[email protected]("flow", ["tool_flow", "ends_on_call"])
[email protected]("missing", [False, True], ids = ["null_content", "no_content_key"])
+def test_tool_call_turn_without_content_renders_like_empty_content(name, flow, missing):
+ call_turn = {"role": "assistant", "tool_calls": [CALL]}
+ if not missing:
+ call_turn["content"] = None
+ empty = {"role": "assistant", "content": "", "tool_calls": [CALL]}
+ text = _render(name, _conversations(call_turn)[flow])
+ assert text == _render(name, _conversations(empty)[flow])
+ assert '<tool_call>\n{"name": "get_weather", "arguments": {"city": "Paris"}}' in text
+
+
+def test_tool_flow_output_is_unchanged():
+ empty = {"role": "assistant", "content": "", "tool_calls": [CALL]}
+ assert _render("qwen3", _conversations(empty)["tool_flow"]) == (
+ "<|im_start|>user\nWeather?<|im_end|>\n"
+ "<|im_start|>assistant\n"
+ '<tool_call>\n{"name": "get_weather", "arguments": {"city": "Paris"}}\n</tool_call>'
+ "<|im_end|>\n"
+ "<|im_start|>user\n<tool_response>\nsunny\n</tool_response><|im_end|>\n"
+ "<|im_start|>assistant\n<think>\n\n</think>\n\nIt is sunny.<|im_end|>\n"
+ )
diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.py
index 7a005ce880f..709b53137c3 100644
--- a/unsloth/chat_templates.py
+++ b/unsloth/chat_templates.py
@@ -962,7 +962,7 @@ qwen3_template = \
{%- for forward_message in messages %}
{%- set index = (messages|length - 1) - loop.index0 %}
{%- set message = messages[index] %}
- {%- set current_content = message.content if message.content is not none else '' %}
+ {%- set current_content = message.content if message.content is string else '' %}
{%- set tool_start = '<tool_response>' %}
{%- set tool_start_length = tool_start|length %}
{%- set start_of_message = current_content[:tool_start_length] %}
@@ -982,12 +982,12 @@ qwen3_template = \
{%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
{{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
{%- elif message.role == "assistant" %}
- {%- set content = message.content %}
+ {%- set content = message.content if message.content is string else '' %}
{%- set reasoning_content = '' %}
{%- if message.reasoning_content is defined and message.reasoning_content is not none %}
{%- set reasoning_content = message.reasoning_content %}
{%- else %}
- {%- if '</think>' in message.content %}
+ {%- if '</think>' in content %}
{%- set content = (message.content.split('</think>')|last).lstrip('\n') %}
{%- set reasoning_content = (message.content.split('</think>')|first).rstrip('\n') %}
{%- set reasoning_content = (reasoning_content.split('<think>')|last).lstrip('\n') %}