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.

Studio: keep tool-call arguments when training on tool-calling datasets (#12487)

Nilay666ad7b24255
diff --git a/studio/backend/tests/test_chat_template_tool_call_rows.py b/studio/backend/tests/test_chat_template_tool_call_rows.py
new file mode 100644
index 00000000000..67805cf75da
--- /dev/null
+++ b/studio/backend/tests/test_chat_template_tool_call_rows.py
@@ -0,0 +1,223 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import json
+from pathlib import Path
+
+import pytest
+from datasets import Dataset
+
+from utils.datasets import apply_chat_template_to_dataset
+from utils.datasets.chat_templates import keep_renderable_chat_template
+
+_GEMMA4_TEMPLATE = (
+ Path(__file__).resolve().parent.parent / "assets" / "chat_templates" / "gemma-4.jinja"
+)
+
+_QWEN35_TEMPLATE = """
+{%- for message in messages %}
+{{- '<|im_start|>' + message.role + '\\n' + (message.content or '') }}
+{%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
+{%- for tool_call in message.tool_calls %}
+{%- if tool_call.function is defined %}{%- set tool_call = tool_call.function %}{%- endif %}
+{{- '<tool_call>\\n<function=' + tool_call.name + '>\\n' }}
+{%- for args_name, args_value in tool_call.arguments | items %}
+{{- '<parameter=' + args_name + '>\\n' + args_value | string + '\\n</parameter>\\n' }}
+{%- endfor %}
+{{- '</function>\\n</tool_call>' }}
+{%- endfor %}
+{%- endif %}
+{{- '<|im_end|>\\n' }}
+{%- endfor %}
+"""
+
+_LLAMA3_TEMPLATE = """
+{%- for message in messages %}
+{%- if not (message.role == 'tool' or 'tool_calls' in message) %}
+{{- '<|start_header_id|>' + message.role + '<|end_header_id|>\\n\\n' + message.content + '<|eot_id|>' }}
+{%- elif 'tool_calls' in message %}
+{%- if not message.tool_calls|length == 1 %}{{- raise_exception('one tool call per message') }}{%- endif %}
+{%- set tool_call = message.tool_calls[0].function %}
+{{- '<|start_header_id|>assistant<|end_header_id|>\\n\\n{"name": "' + tool_call.name + '", "parameters": ' + tool_call.arguments | tojson + '}<|eot_id|>' }}
+{%- else %}
+{{- '<|start_header_id|>ipython<|end_header_id|>\\n\\n' + message.content + '<|eot_id|>' }}
+{%- endif %}
+{%- endfor %}
+"""
+
+_DEEPSEEK_TEMPLATE = """
+{%- for message in messages %}
+{%- if message['role'] == 'user' %}{{- '<User>' + message['content'] }}{%- endif %}
+{%- if message['role'] == 'assistant' and message['content'] is none %}
+{%- for tool in message['tool_calls'] %}
+{{- '<call>' + tool['function']['name'] + '\\n' + tool['function']['arguments'] + '</call>' }}
+{%- endfor %}
+{%- endif %}
+{%- if message['role'] == 'assistant' and message['content'] is not none %}{{- '<Assistant>' + message['content'] }}{%- endif %}
+{%- if message['role'] == 'tool' %}{{- '<output>' + message['content'] }}{%- endif %}
+{%- endfor %}
+"""
+
+
+class _JinjaTokenizer:
+ eos_token = ""
+
+ def __init__(self, template):
+ self.chat_template = template
+
+ def apply_chat_template(
+ self,
+ conversation,
+ tokenize = False,
+ add_generation_prompt = False,
+ **_kwargs,
+ ):
+ jinja2 = pytest.importorskip("jinja2")
+ sandbox = pytest.importorskip("jinja2.sandbox")
+
+ def _raise(message):
+ raise jinja2.exceptions.TemplateError(message)
+
+ env = sandbox.ImmutableSandboxedEnvironment(trim_blocks = True, lstrip_blocks = True)
+ env.filters["tojson"] = lambda value, **opts: json.dumps(value, **opts)
+ env.globals["raise_exception"] = _raise
+ return env.from_string(self.chat_template).render(
+ messages = conversation,
+ add_generation_prompt = add_generation_prompt,
+ bos_token = "",
+ )
+
+
+def _tool_call_row(arguments):
+ return [
+ {"role": "user", "content": "Weather in Paris?"},
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [
+ {
+ "id": "call_0",
+ "type": "function",
+ "function": {"name": "get_weather", "arguments": arguments},
+ }
+ ],
+ },
+ {"role": "tool", "content": "21C", "tool_call_id": "call_0"},
+ {"role": "assistant", "content": "It is 21C in Paris."},
+ ]
+
+
+def _plain_row():
+ return [{"role": "user", "content": "hi"}, {"role": "assistant", "content": "Hello!"}]
+
+
+def _format(rows, template):
+ dataset_info = {
+ "dataset": Dataset.from_list([{"messages": row} for row in rows]),
+ "detected_format": "chatml_messages",
+ "final_format": "chatml_messages",
+ "chat_column": "messages",
+ "is_standardized": True,
+ "warnings": [],
+ }
+ return apply_chat_template_to_dataset(dataset_info, _JinjaTokenizer(template), num_proc = 1)
+
+
+def test_json_string_tool_arguments_are_rendered_as_parameters():
+ result = _format([_tool_call_row('{"city": "Paris", "unit": null}')], _QWEN35_TEMPLATE)
+
+ assert result["success"] is True
+ text = result["dataset"][0]["text"]
+ assert "<parameter=city>\nParis\n</parameter>" in text
+ assert "<parameter=unit>\nNone\n</parameter>" in text
+
+
+def test_gemma4_learns_its_own_tool_call_format():
+ result = _format(
+ [_tool_call_row('{"city": "Paris"}')], _GEMMA4_TEMPLATE.read_text(encoding = "utf-8")
+ )
+
+ assert result["success"] is True
+ assert 'call:get_weather{city:<|"|>Paris<|"|>}' in result["dataset"][0]["text"]
+
+
+def test_tool_call_dataset_with_null_filled_keys_renders_on_llama3():
+ rows = [_plain_row(), _tool_call_row('{"city": "Paris"}')]
+
+ result = _format(rows, _LLAMA3_TEMPLATE)
+
+ assert result["success"] is True, result["errors"]
+ texts = result["dataset"]["text"]
+ assert len(texts) == 2
+ assert "<|start_header_id|>assistant<|end_header_id|>\n\nHello!" in texts[0]
+ assert '{"name": "get_weather", "parameters": {"city": "Paris"}}' in texts[1]
+
+
+def test_dict_arguments_do_not_gain_other_tools_null_parameters():
+ weather = _tool_call_row({"city": "Paris"})
+ search = _tool_call_row({"query": "cats"})
+ search[1]["tool_calls"][0]["function"]["name"] = "web_search"
+
+ result = _format([weather, search], _QWEN35_TEMPLATE)
+
+ assert result["success"] is True
+ weather_text, search_text = result["dataset"]["text"]
+ assert "<parameter=query>" not in weather_text
+ assert "<parameter=city>" not in search_text
+
+
+def test_non_json_tool_arguments_are_kept_verbatim():
+ result = _format([_tool_call_row("not json")], _LLAMA3_TEMPLATE)
+
+ assert result["success"] is True
+ assert '"parameters": "not json"' in result["dataset"][0]["text"]
+
+
+def test_tool_call_turn_with_null_content_and_string_arguments_still_renders():
+ row = _tool_call_row('{"city": "Paris"}')
+ row[1]["content"] = None
+
+ result = _format([row, _plain_row()], _DEEPSEEK_TEMPLATE)
+
+ assert result["success"] is True, result["errors"]
+ assert len(result["dataset"]) == 2
+ assert '<call>get_weather\n{"city": "Paris"}</call>' in result["dataset"][0]["text"]
+
+
+def test_null_content_is_dropped_before_the_row_as_loaded_is_tried():
+ template = (
+ "{%- for message in messages %}"
+ "{%- if message.content is defined %}[{{ message.content }}]{%- else %}-{%- endif %}"
+ "{%- endfor %}"
+ )
+ null_content = _tool_call_row('{"city": "Paris"}')
+ null_content[1]["content"] = None
+
+ result = _format([null_content, _tool_call_row('{"city": "Paris"}')], template)
+
+ assert result["dataset"]["text"] == [
+ "[Weather in Paris?]-[21C][It is 21C in Paris.]",
+ "[Weather in Paris?][][21C][It is 21C in Paris.]",
+ ]
+
+
+def test_dropped_row_reports_the_template_error_of_the_cleaned_row():
+ parallel = _tool_call_row('{"city": "Paris"}')
+ parallel[1]["tool_calls"].append({**parallel[1]["tool_calls"][0], "id": "call_1"})
+
+ result = _format([_plain_row(), parallel], _LLAMA3_TEMPLATE)
+
+ assert result["success"] is True
+ assert "one tool call per message" in result["dropped_rows_warning"]
+
+
+def test_template_probe_counts_rows_after_cleaning():
+ tokenizer = _JinjaTokenizer(_LLAMA3_TEMPLATE)
+ dataset = Dataset.from_list(
+ [{"messages": _plain_row()}, {"messages": _tool_call_row('{"city": "Paris"}')}]
+ )
+
+ note = keep_renderable_chat_template(tokenizer, dataset, "messages", _QWEN35_TEMPLATE)
+
+ assert note is None
+ assert tokenizer.chat_template == _LLAMA3_TEMPLATE
diff --git a/studio/backend/utils/datasets/chat_templates.py b/studio/backend/utils/datasets/chat_templates.py
index 68a6a210e7c..741a0b7d47c 100644
--- a/studio/backend/utils/datasets/chat_templates.py
+++ b/studio/backend/utils/datasets/chat_templates.py
@@ -163,15 +163,43 @@ def _set_chat_template(tokenizer, chat_template):
inner.chat_template = chat_template
+def _drop_none_values(value):
+ # A loaded dict cannot tell an explicit null from a key another row added, so dict-typed
+ # arguments lose their nulls; JSON-string arguments keep them.
+ if isinstance(value, dict):
+ return {key: _drop_none_values(item) for key, item in value.items() if item is not None}
+ if isinstance(value, list):
+ return [_drop_none_values(item) for item in value]
+ return value
+
+
+def _render_conversation(tokenizer, conversation):
+ from core.inference.chat_template_helpers import _normalize_tool_call_arguments
+
+ attempts = []
+ for messages in (_drop_none_values(conversation), conversation):
+ for attempt in (_normalize_tool_call_arguments(messages), messages):
+ if not any(attempt is seen for seen in attempts):
+ attempts.append(attempt)
+ first_error = None
+ for attempt in attempts:
+ try:
+ return tokenizer.apply_chat_template(
+ attempt, tokenize = False, add_generation_prompt = False
+ )
+ except Exception as error:
+ # The row as loaded is kept for templates that need a None content (DeepSeek V3), but its
+ # error is usually a key the loader filled with None, so report the cleaned row's.
+ if first_error is None:
+ first_error = error
+ raise first_error
+
+
def _count_renderable(tokenizer, conversations):
rendered = 0
for conversation in conversations:
try:
- tokenizer.apply_chat_template(
- conversation,
- tokenize = False,
- add_generation_prompt = False,
- )
+ _render_conversation(tokenizer, conversation)
rendered += 1
except Exception:
pass
@@ -493,11 +521,7 @@ def apply_chat_template_to_dataset(
for convo in convos:
try:
- text = tokenizer.apply_chat_template(
- convo,
- tokenize = False,
- add_generation_prompt = False
- )
+ text = _render_conversation(tokenizer, convo)
if remove_bos_prefix:
text = text.removeprefix('<bos>')