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 Gemma-4 chat template format tool-calling conversations (#14015)

Nilay32d53835d0ea
diff --git a/tests/test_gemma4_chat_template.py b/tests/test_gemma4_chat_template.py
index b66bd665ca1..8c2c8be5d2e 100644
--- a/tests/test_gemma4_chat_template.py
+++ b/tests/test_gemma4_chat_template.py
@@ -169,3 +169,107 @@ def test_base_template_has_no_channel_thought_injection():
out = _render("gemma4_template", msgs, add_generation_prompt = True)
assert out.endswith("<|turn>model\n")
assert "<|channel>thought" not in out
+
+
+TOOLS = [
+ {
+ "type": "function",
+ "function": {
+ "name": "get_weather",
+ "description": "Get the weather",
+ "parameters": {
+ "type": "object",
+ "properties": {"city": {"type": "string", "description": "City name"}},
+ "required": ["city"],
+ },
+ },
+ }
+]
+TOOL_CONVO = [
+ {"role": "user", "content": "Weather in Paris?"},
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": [
+ {
+ "id": "c1",
+ "type": "function",
+ "function": {"name": "get_weather", "arguments": {"city": "Paris"}},
+ }
+ ],
+ },
+ {"role": "tool", "tool_call_id": "c1", "content": "sunny"},
+ {"role": "assistant", "content": "It is sunny."},
+]
+TOOL_SYSTEM = (
+ '<|turn>system\n<|tool>declaration:get_weather{description:<|"|>Get the weather<|"|>,'
+ 'parameters:{properties:{city:{description:<|"|>City name<|"|>,type:<|"|>STRING<|"|>}},'
+ 'required:[<|"|>city<|"|>],type:<|"|>OBJECT<|"|>}}<tool|><turn|>\n'
+)
+TOOL_ROUND = (
+ "<|turn>user\nWeather in Paris?<turn|>\n<|turn>model\n"
+ '<|tool_call>call:get_weather{city:<|"|>Paris<|"|>}<tool_call|>'
+ '<|tool_response>response:get_weather{value:<|"|>sunny<|"|>}<tool_response|>'
+)
+
+
+def _render_hf(template_name, messages, **kwargs):
+ env = Environment(trim_blocks = True, lstrip_blocks = True)
+ env.globals["raise_exception"] = lambda msg: (_ for _ in ()).throw(TemplateError(msg))
+ ctx = {"messages": messages, "add_generation_prompt": False, "tools": None}
+ ctx.update(kwargs)
+ return env.from_string(_extract_template(template_name)).render(**ctx)
+
+
[email protected]("name", ["gemma4_template", "gemma4_thinking_template"])
+def test_tool_calling_conversation_renders_like_gemma4(name):
+ out = _render_hf(name, TOOL_CONVO, tools = TOOLS)
+ assert out == TOOL_SYSTEM + TOOL_ROUND + "It is sunny.<turn|>\n"
+
+
[email protected]("name", ["gemma4_template", "gemma4_thinking_template"])
+def test_generation_after_tool_result_continues_the_model_turn(name):
+ out = _render_hf(name, TOOL_CONVO[:3], tools = TOOLS, add_generation_prompt = True)
+ assert out == TOOL_SYSTEM + TOOL_ROUND
+
+
[email protected]("name", ["gemma4_template", "gemma4_thinking_template"])
+def test_tool_responses_on_the_assistant_message_render_like_tool_messages(name):
+ call = TOOL_CONVO[1]["tool_calls"]
+ merged = [
+ TOOL_CONVO[0],
+ {
+ "role": "assistant",
+ "tool_calls": call,
+ "tool_responses": [{"name": "get_weather", "response": "sunny"}],
+ "content": "It is sunny.",
+ },
+ ]
+ assert _render_hf(name, merged, tools = TOOLS) == _render_hf(name, TOOL_CONVO, tools = TOOLS)
+ pending = [TOOL_CONVO[0], {k: v for k, v in merged[1].items() if k != "content"}]
+ out = _render_hf(name, pending, tools = TOOLS, add_generation_prompt = True)
+ assert out == TOOL_SYSTEM + TOOL_ROUND
+
+
+ "name, opener",
+ [
+ ("gemma4_template", "<|turn>model\n"),
+ ("gemma4_thinking_template", "<|turn>model\n<|channel>thought\n<channel|>"),
+ ],
+)
+def test_a_closed_tool_turn_reopens_the_model_turn(name, opener):
+ msgs = [dict(m) for m in TOOL_CONVO[:3]]
+ msgs[1]["content"] = "Let me check."
+ out = _render_hf(name, msgs, tools = TOOLS, add_generation_prompt = True)
+ assert out.endswith("Let me check.<turn|>\n" + opener)
+
+
[email protected]("name", ["gemma4_template", "gemma4_thinking_template"])
+def test_object_tool_result_renders_its_fields(name):
+ msgs = [dict(m) for m in TOOL_CONVO[:3]]
+ msgs[2]["content"] = {"temperature": 18, "sky": "sunny"}
+ out = _render_hf(name, msgs, tools = TOOLS, add_generation_prompt = True)
+ assert out.endswith(
+ '<|tool_response>response:get_weather{sky:<|"|>sunny<|"|>,temperature:18}<tool_response|>'
+ )
diff --git a/tests/test_gemma4_empty_thought.py b/tests/test_gemma4_empty_thought.py
index f8e67aebb5d..f5a622d5637 100644
--- a/tests/test_gemma4_empty_thought.py
+++ b/tests/test_gemma4_empty_thought.py
@@ -215,6 +215,42 @@ def test_only_model_turns_change():
assert new.replace(EMPTY, "") == old
+def test_tool_round_gets_one_empty_channel_and_stays_a_prefix():
+ call = {"id": "c1", "type": "function", "function": {"name": "f", "arguments": {"x": 1}}}
+ msgs = [
+ {"role": "user", "content": "Hi"},
+ {"role": "assistant", "content": None, "tool_calls": [call]},
+ {"role": "tool", "tool_call_id": "c1", "content": "ok"},
+ {"role": "assistant", "content": "Done."},
+ ]
+ out = _render(NS["gemma4_empty_thought_template"], msgs)
+ assert out == (
+ "<bos><|turn>user\nHi<turn|>\n"
+ f"<|turn>model\n{EMPTY}<|tool_call>call:f{{x:1}}<tool_call|>"
+ '<|tool_response>response:f{value:<|"|>ok<|"|>}<tool_response|>Done.<turn|>\n'
+ )
+ prompt = _render(NS["gemma4_empty_thought_template"], msgs[:3], add_generation_prompt = True)
+ assert out == prompt + "Done.<turn|>\n"
+
+
+def test_reasoning_after_a_tool_result_is_kept_and_stays_a_prefix():
+ call = {"id": "c1", "type": "function", "function": {"name": "f", "arguments": {"x": 1}}}
+ msgs = [
+ {"role": "user", "content": "Hi"},
+ {"role": "assistant", "content": None, "tool_calls": [call]},
+ {"role": "tool", "tool_call_id": "c1", "content": "ok"},
+ {"role": "assistant", "content": "Done.", "reasoning_content": "r"},
+ ]
+ tmpl = NS["gemma4_empty_thought_template"]
+ out = _render(tmpl, msgs)
+ assert out.endswith("<tool_response|><|channel>thought\nr\n<channel|>Done.<turn|>\n")
+ assert out.count(EMPTY) == 1
+ full = _render(tmpl, msgs, enable_thinking = True)
+ prompt = _render(tmpl, msgs[:3], enable_thinking = True, add_generation_prompt = True)
+ assert prompt.endswith("<tool_response|><|channel>thought\n")
+ assert full.startswith(prompt)
+
+
def test_roles_must_alternate_still_raises():
with pytest.raises(TemplateError):
_render(NS["gemma4_empty_thought_template"], [{"role": "assistant", "content": "x"}])
diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.py
index b2ff8f5cc91..f2c0583df8f 100644
--- a/unsloth/chat_templates.py
+++ b/unsloth/chat_templates.py
@@ -917,7 +917,156 @@ DEFAULT_SYSTEM_MESSAGE["gemma3n"] = None
# Gemma-4 uses <|turn>role\n...<turn|>\n format
# =========================================== Gemma-4
gemma4_template = \
-"""{%- macro strip_thinking(text) -%}
+"""{%- macro format_parameters(properties, required, filter_keys=false) -%}
+ {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
+ {%- set ns = namespace(found_first=false) -%}
+ {%- for key, value in properties | dictsort -%}
+ {%- set add_comma = false -%}
+ {%- if not filter_keys or key not in standard_keys -%}
+ {%- if ns.found_first %},{% endif -%}
+ {%- set ns.found_first = true -%}
+ {{ key }}:{
+ {%- if value['description'] -%}
+ description:<|"|>{{ value['description'] }}<|"|>
+ {%- set add_comma = true -%}
+ {%- endif -%}
+ {%- if value['type'] | upper == 'STRING' -%}
+ {%- if value['enum'] -%}
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
+ enum:{{ format_argument(value['enum']) }}
+ {%- endif -%}
+ {%- elif value['type'] | upper == 'ARRAY' -%}
+ {%- if value['items'] is mapping and value['items'] -%}
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
+ items:{
+ {%- set ns_items = namespace(found_first=false) -%}
+ {%- for item_key, item_value in value['items'] | dictsort -%}
+ {%- if item_value is not none -%}
+ {%- if ns_items.found_first %},{% endif -%}
+ {%- set ns_items.found_first = true -%}
+ {%- if item_key == 'properties' -%}
+ properties:{
+ {%- if item_value is mapping -%}
+ {{- format_parameters(item_value, value['items']['required'] | default([])) -}}
+ {%- endif -%}
+ }
+ {%- elif item_key == 'required' -%}
+ required:[
+ {%- for req_item in item_value -%}
+ <|"|>{{- req_item -}}<|"|>
+ {%- if not loop.last %},{% endif -%}
+ {%- endfor -%}
+ ]
+ {%- elif item_key == 'type' -%}
+ {%- if item_value is string -%}
+ type:{{ format_argument(item_value | upper) }}
+ {%- else -%}
+ type:{{ format_argument(item_value | map('upper') | list) }}
+ {%- endif -%}
+ {%- else -%}
+ {{ item_key }}:{{ format_argument(item_value) }}
+ {%- endif -%}
+ {%- endif -%}
+ {%- endfor -%}
+ }
+ {%- endif -%}
+ {%- endif -%}
+ {%- if value['nullable'] %}
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
+ nullable:true
+ {%- endif -%}
+ {%- if value['type'] | upper == 'OBJECT' -%}
+ {%- if value['properties'] is defined and value['properties'] is mapping -%}
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
+ properties:{
+ {{- format_parameters(value['properties'], value['required'] | default([])) -}}
+ }
+ {%- elif value is mapping -%}
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
+ properties:{
+ {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
+ }
+ {%- endif -%}
+ {%- if value['required'] -%}
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
+ required:[
+ {%- for item in value['required'] | default([]) -%}
+ <|"|>{{- item -}}<|"|>
+ {%- if not loop.last %},{% endif -%}
+ {%- endfor -%}
+ ]
+ {%- endif -%}
+ {%- endif -%}
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
+ type:<|"|>{{ value['type'] | upper }}<|"|>}
+ {%- endif -%}
+ {%- endfor -%}
+{%- endmacro -%}
+{%- macro format_function_declaration(tool_data) -%}
+ declaration:{{- tool_data['function']['name'] -}}{description:<|"|>{{- tool_data['function']['description'] -}}<|"|>
+ {%- set params = tool_data['function']['parameters'] -%}
+ {%- if params -%}
+ ,parameters:{
+ {%- if params['properties'] -%}
+ properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
+ {%- endif -%}
+ {%- if params['required'] -%}
+ required:[
+ {%- for item in params['required'] -%}
+ <|"|>{{- item -}}<|"|>
+ {{- ',' if not loop.last -}}
+ {%- endfor -%}
+ ],
+ {%- endif -%}
+ {%- if params['type'] -%}
+ type:<|"|>{{- params['type'] | upper -}}<|"|>}
+ {%- endif -%}
+ {%- endif -%}
+ {%- if 'response' in tool_data['function'] -%}
+ {%- set response_declaration = tool_data['function']['response'] -%}
+ ,response:{
+ {%- if response_declaration['description'] -%}
+ description:<|"|>{{- response_declaration['description'] -}}<|"|>,
+ {%- endif -%}
+ {%- if response_declaration['type'] | upper == 'OBJECT' -%}
+ type:<|"|>{{- response_declaration['type'] | upper -}}<|"|>}
+ {%- endif -%}
+ {%- endif -%}
+ }
+{%- endmacro -%}
+{%- macro format_argument(argument, escape_keys=True) -%}
+ {%- if argument is none -%}
+ {{- 'null' -}}
+ {%- elif argument is string -%}
+ {{- '<|"|>' + argument + '<|"|>' -}}
+ {%- elif argument is boolean -%}
+ {{- 'true' if argument else 'false' -}}
+ {%- elif argument is mapping -%}
+ {{- '{' -}}
+ {%- set ns = namespace(found_first=false) -%}
+ {%- for key, value in argument | dictsort -%}
+ {%- if ns.found_first %},{% endif -%}
+ {%- set ns.found_first = true -%}
+ {%- if escape_keys -%}
+ {{- '<|"|>' + key + '<|"|>' -}}
+ {%- else -%}
+ {{- key -}}
+ {%- endif -%}
+ :{{- format_argument(value, escape_keys=escape_keys) -}}
+ {%- endfor -%}
+ {{- '}' -}}
+ {%- elif argument is sequence -%}
+ {{- '[' -}}
+ {%- for item in argument -%}
+ {{- format_argument(item, escape_keys=escape_keys) -}}
+ {%- if not loop.last %},{% endif -%}
+ {%- endfor -%}
+ {{- ']' -}}
+ {%- else -%}
+ {{- argument -}}
+ {%- endif -%}
+{%- endmacro -%}
+{%- macro strip_thinking(text) -%}
{%- set ns = namespace(result='') -%}
{%- for part in text.split('<channel|>') -%}
{%- if '<|channel>' in part -%}
@@ -928,9 +1077,23 @@ gemma4_template = \
{%- endfor -%}
{{- ns.result | trim -}}
{%- endmacro -%}
+{%- macro format_tool_response_block(tool_name, response) -%}
+ {{- '<|tool_response>' -}}
+ {%- if response is mapping -%}
+ {{- 'response:' + tool_name + '{' -}}
+ {%- for key, value in response | dictsort -%}
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
+ {%- if not loop.last %},{% endif -%}
+ {%- endfor -%}
+ {{- '}' -}}
+ {%- else -%}
+ {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
+ {%- endif -%}
+ {{- '<tool_response|>' -}}
+{%- endmacro -%}
{%- set thinking = enable_thinking is defined and enable_thinking -%}
{%- set loop_messages = messages -%}
-{%- if messages[0]['role'] in ['system', 'developer'] or thinking -%}
+{%- if messages[0]['role'] in ['system', 'developer'] or thinking or (tools is defined and tools) -%}
{{ '<|turn>system\n' }}
{%- if thinking -%}
{{ '<|think|>\n' }}
@@ -939,10 +1102,18 @@ gemma4_template = \
{{ messages[0]['content'] | trim }}
{%- set loop_messages = messages[1:] -%}
{%- endif -%}
+ {%- if tools is defined and tools -%}
+ {%- for tool in tools -%}
+ {{- '<|tool>' + format_function_declaration(tool) | trim + '<tool|>' -}}
+ {%- endfor -%}
+ {%- endif -%}
{{ '<turn|>\n' }}
{%- endif -%}
+{%- set ns_tool = namespace(turns=0, tool_calls=false, tool_response=false) -%}
{%- for message in loop_messages -%}
- {%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}
+ {%- if message['role'] != 'tool' -%}
+ {%- set continues = message['role'] == 'assistant' and ns_tool.tool_calls -%}
+ {%- if not continues and (message['role'] == 'user') != (ns_tool.turns % 2 == 0) -%}
{{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}
{%- endif -%}
{%- if (message['role'] == 'assistant') -%}
@@ -950,7 +1121,77 @@ gemma4_template = \
{%- else -%}
{%- set role = message['role'] -%}
{%- endif -%}
- {{ '<|turn>' + role + '\n' }}
+ {%- if not continues -%}
+ {%- set ns_tool.turns = ns_tool.turns + 1 -%}
+ {{ '<|turn>' + role + '\n' }}
+ {%- endif -%}
+ {%- set ns_tool.tool_calls = message.get('tool_calls') -%}
+ {%- if ns_tool.tool_calls -%}
+ {%- for tool_call in ns_tool.tool_calls -%}
+ {%- set function = tool_call['function'] -%}
+ {{- '<|tool_call>call:' + function['name'] + '{' -}}
+ {%- if function['arguments'] is mapping -%}
+ {%- set ns_args = namespace(found_first=false) -%}
+ {%- for key, value in function['arguments'] | dictsort -%}
+ {%- if ns_args.found_first %},{% endif -%}
+ {%- set ns_args.found_first = true -%}
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
+ {%- endfor -%}
+ {%- elif function['arguments'] is string -%}
+ {%- set argstr = function['arguments'] | trim -%}
+ {%- if argstr[:1] == '{' and argstr[-1:] == '}' -%}
+ {{- argstr[1:-1] -}}
+ {%- else -%}
+ {{- function['arguments'] -}}
+ {%- endif -%}
+ {%- endif -%}
+ {{- '}<tool_call|>' -}}
+ {%- endfor -%}
+ {%- set ns_tool.tool_response = false -%}
+ {%- for tool_response in message.get('tool_responses') or [] -%}
+ {{- format_tool_response_block(tool_response['name'] | default('unknown', true), tool_response['response']) -}}
+ {%- set ns_tool.tool_response = true -%}
+ {%- endfor -%}
+ {%- set ns_scan = namespace(stopped=false, next_role=none) -%}
+ {%- for k in range(loop.index0 + 1, loop_messages | length) -%}
+ {%- if ns_scan.stopped -%}
+ {%- elif loop_messages[k]['role'] != 'tool' -%}
+ {%- set ns_scan.stopped = true -%}
+ {%- set ns_scan.next_role = loop_messages[k]['role'] -%}
+ {%- elif not message.get('tool_responses') -%}
+ {%- set follow = loop_messages[k] -%}
+ {%- set ns_tname = namespace(name=follow.get('name') or 'unknown') -%}
+ {%- for tc in ns_tool.tool_calls -%}
+ {%- if tc.get('id') == follow.get('tool_call_id') -%}
+ {%- set ns_tname.name = tc['function']['name'] -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- set tool_body = follow.get('content') -%}
+ {%- if tool_body is sequence and tool_body is not string and tool_body is not mapping -%}
+ {%- set ns_txt = namespace(s='') -%}
+ {%- for part in tool_body -%}
+ {%- if part.get('type') == 'text' -%}
+ {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
+ {%- for part in tool_body -%}
+ {%- if part.get('type') in ['image', 'image_url'] -%}
+ {{- '<|image|>' -}}
+ {%- elif part.get('type') in ['audio', 'input_audio'] -%}
+ {{- '<|audio|>' -}}
+ {%- elif part.get('type') == 'video' -%}
+ {{- '<|video|>' -}}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- else -%}
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
+ {%- endif -%}
+ {%- set ns_tool.tool_response = true -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- endif -%}
+ {%- set captured_content -%}
{%- if message['content'] is string -%}
{%- if role == "model" -%}
{{ strip_thinking(message['content']) }}
@@ -973,13 +1214,27 @@ gemma4_template = \
{%- endif -%}
{%- endif -%}
{%- endfor -%}
- {%- else -%}
+ {%- elif not ns_tool.tool_calls -%}
{{ raise_exception("Invalid content type") }}
{%- endif -%}
- {{ '<turn|>\n' }}
+ {%- endset -%}
+ {{- captured_content -}}
+ {%- if not ns_tool.tool_calls -%}
+ {{ '<turn|>\n' }}
+ {%- elif not ns_tool.tool_response -%}
+ {{- '<|tool_response>' -}}
+ {%- elif ns_scan.next_role != 'assistant' and (captured_content | trim | length > 0 or ns_scan.next_role) -%}
+ {{- '<turn|>\n' -}}
+ {%- set ns_tool.tool_calls = false -%}
+ {%- endif -%}
+ {%- endif -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
+ {%- if not ns_tool.tool_calls -%}
{{'<|turn>model\n'}}
+ {%- elif ns_tool.tool_response and thinking -%}
+ {{- '<|channel>thought\n' -}}
+ {%- endif -%}
{%- endif -%}
"""
@@ -996,7 +1251,156 @@ DEFAULT_SYSTEM_MESSAGE["gemma4"] = None
# Gemma-4 thinking template
gemma4_thinking_template = \
-"""{%- macro strip_thinking(text) -%}
+"""{%- macro format_parameters(properties, required, filter_keys=false) -%}
+ {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
+ {%- set ns = namespace(found_first=false) -%}
+ {%- for key, value in properties | dictsort -%}
+ {%- set add_comma = false -%}
+ {%- if not filter_keys or key not in standard_keys -%}
+ {%- if ns.found_first %},{% endif -%}
+ {%- set ns.found_first = true -%}
+ {{ key }}:{
+ {%- if value['description'] -%}
+ description:<|"|>{{ value['description'] }}<|"|>
+ {%- set add_comma = true -%}
+ {%- endif -%}
+ {%- if value['type'] | upper == 'STRING' -%}
+ {%- if value['enum'] -%}
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
+ enum:{{ format_argument(value['enum']) }}
+ {%- endif -%}
+ {%- elif value['type'] | upper == 'ARRAY' -%}
+ {%- if value['items'] is mapping and value['items'] -%}
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
+ items:{
+ {%- set ns_items = namespace(found_first=false) -%}
+ {%- for item_key, item_value in value['items'] | dictsort -%}
+ {%- if item_value is not none -%}
+ {%- if ns_items.found_first %},{% endif -%}
+ {%- set ns_items.found_first = true -%}
+ {%- if item_key == 'properties' -%}
+ properties:{
+ {%- if item_value is mapping -%}
+ {{- format_parameters(item_value, value['items']['required'] | default([])) -}}
+ {%- endif -%}
+ }
+ {%- elif item_key == 'required' -%}
+ required:[
+ {%- for req_item in item_value -%}
+ <|"|>{{- req_item -}}<|"|>
+ {%- if not loop.last %},{% endif -%}
+ {%- endfor -%}
+ ]
+ {%- elif item_key == 'type' -%}
+ {%- if item_value is string -%}
+ type:{{ format_argument(item_value | upper) }}
+ {%- else -%}
+ type:{{ format_argument(item_value | map('upper') | list) }}
+ {%- endif -%}
+ {%- else -%}
+ {{ item_key }}:{{ format_argument(item_value) }}
+ {%- endif -%}
+ {%- endif -%}
+ {%- endfor -%}
+ }
+ {%- endif -%}
+ {%- endif -%}
+ {%- if value['nullable'] %}
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
+ nullable:true
+ {%- endif -%}
+ {%- if value['type'] | upper == 'OBJECT' -%}
+ {%- if value['properties'] is defined and value['properties'] is mapping -%}
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
+ properties:{
+ {{- format_parameters(value['properties'], value['required'] | default([])) -}}
+ }
+ {%- elif value is mapping -%}
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
+ properties:{
+ {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
+ }
+ {%- endif -%}
+ {%- if value['required'] -%}
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
+ required:[
+ {%- for item in value['required'] | default([]) -%}
+ <|"|>{{- item -}}<|"|>
+ {%- if not loop.last %},{% endif -%}
+ {%- endfor -%}
+ ]
+ {%- endif -%}
+ {%- endif -%}
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
+ type:<|"|>{{ value['type'] | upper }}<|"|>}
+ {%- endif -%}
+ {%- endfor -%}
+{%- endmacro -%}
+{%- macro format_function_declaration(tool_data) -%}
+ declaration:{{- tool_data['function']['name'] -}}{description:<|"|>{{- tool_data['function']['description'] -}}<|"|>
+ {%- set params = tool_data['function']['parameters'] -%}
+ {%- if params -%}
+ ,parameters:{
+ {%- if params['properties'] -%}
+ properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
+ {%- endif -%}
+ {%- if params['required'] -%}
+ required:[
+ {%- for item in params['required'] -%}
+ <|"|>{{- item -}}<|"|>
+ {{- ',' if not loop.last -}}
+ {%- endfor -%}
+ ],
+ {%- endif -%}
+ {%- if params['type'] -%}
+ type:<|"|>{{- params['type'] | upper -}}<|"|>}
+ {%- endif -%}
+ {%- endif -%}
+ {%- if 'response' in tool_data['function'] -%}
+ {%- set response_declaration = tool_data['function']['response'] -%}
+ ,response:{
+ {%- if response_declaration['description'] -%}
+ description:<|"|>{{- response_declaration['description'] -}}<|"|>,
+ {%- endif -%}
+ {%- if response_declaration['type'] | upper == 'OBJECT' -%}
+ type:<|"|>{{- response_declaration['type'] | upper -}}<|"|>}
+ {%- endif -%}
+ {%- endif -%}
+ }
+{%- endmacro -%}
+{%- macro format_argument(argument, escape_keys=True) -%}
+ {%- if argument is none -%}
+ {{- 'null' -}}
+ {%- elif argument is string -%}
+ {{- '<|"|>' + argument + '<|"|>' -}}
+ {%- elif argument is boolean -%}
+ {{- 'true' if argument else 'false' -}}
+ {%- elif argument is mapping -%}
+ {{- '{' -}}
+ {%- set ns = namespace(found_first=false) -%}
+ {%- for key, value in argument | dictsort -%}
+ {%- if ns.found_first %},{% endif -%}
+ {%- set ns.found_first = true -%}
+ {%- if escape_keys -%}
+ {{- '<|"|>' + key + '<|"|>' -}}
+ {%- else -%}
+ {{- key -}}
+ {%- endif -%}
+ :{{- format_argument(value, escape_keys=escape_keys) -}}
+ {%- endfor -%}
+ {{- '}' -}}
+ {%- elif argument is sequence -%}
+ {{- '[' -}}
+ {%- for item in argument -%}
+ {{- format_argument(item, escape_keys=escape_keys) -}}
+ {%- if not loop.last %},{% endif -%}
+ {%- endfor -%}
+ {{- ']' -}}
+ {%- else -%}
+ {{- argument -}}
+ {%- endif -%}
+{%- endmacro -%}
+{%- macro strip_thinking(text) -%}
{%- set ns = namespace(result='') -%}
{%- for part in text.split('<channel|>') -%}
{%- if '<|channel>' in part -%}
@@ -1007,9 +1411,23 @@ gemma4_thinking_template = \
{%- endfor -%}
{{- ns.result | trim -}}
{%- endmacro -%}
+{%- macro format_tool_response_block(tool_name, response) -%}
+ {{- '<|tool_response>' -}}
+ {%- if response is mapping -%}
+ {{- 'response:' + tool_name + '{' -}}
+ {%- for key, value in response | dictsort -%}
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
+ {%- if not loop.last %},{% endif -%}
+ {%- endfor -%}
+ {{- '}' -}}
+ {%- else -%}
+ {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
+ {%- endif -%}
+ {{- '<tool_response|>' -}}
+{%- endmacro -%}
{%- set thinking = enable_thinking is defined and enable_thinking -%}
{%- set loop_messages = messages -%}
-{%- if messages[0]['role'] in ['system', 'developer'] or thinking -%}
+{%- if messages[0]['role'] in ['system', 'developer'] or thinking or (tools is defined and tools) -%}
{{ '<|turn>system\n' }}
{%- if thinking -%}
{{ '<|think|>\n' }}
@@ -1018,10 +1436,18 @@ gemma4_thinking_template = \
{{ messages[0]['content'] | trim }}
{%- set loop_messages = messages[1:] -%}
{%- endif -%}
+ {%- if tools is defined and tools -%}
+ {%- for tool in tools -%}
+ {{- '<|tool>' + format_function_declaration(tool) | trim + '<tool|>' -}}
+ {%- endfor -%}
+ {%- endif -%}
{{ '<turn|>\n' }}
{%- endif -%}
+{%- set ns_tool = namespace(turns=0, tool_calls=false, tool_response=false) -%}
{%- for message in loop_messages -%}
- {%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}
+ {%- if message['role'] != 'tool' -%}
+ {%- set continues = message['role'] == 'assistant' and ns_tool.tool_calls -%}
+ {%- if not continues and (message['role'] == 'user') != (ns_tool.turns % 2 == 0) -%}
{{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}
{%- endif -%}
{%- if (message['role'] == 'assistant') -%}
@@ -1029,7 +1455,77 @@ gemma4_thinking_template = \
{%- else -%}
{%- set role = message['role'] -%}
{%- endif -%}
- {{ '<|turn>' + role + '\n' }}
+ {%- if not continues -%}
+ {%- set ns_tool.turns = ns_tool.turns + 1 -%}
+ {{ '<|turn>' + role + '\n' }}
+ {%- endif -%}
+ {%- set ns_tool.tool_calls = message.get('tool_calls') -%}
+ {%- if ns_tool.tool_calls -%}
+ {%- for tool_call in ns_tool.tool_calls -%}
+ {%- set function = tool_call['function'] -%}
+ {{- '<|tool_call>call:' + function['name'] + '{' -}}
+ {%- if function['arguments'] is mapping -%}
+ {%- set ns_args = namespace(found_first=false) -%}
+ {%- for key, value in function['arguments'] | dictsort -%}
+ {%- if ns_args.found_first %},{% endif -%}
+ {%- set ns_args.found_first = true -%}
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
+ {%- endfor -%}
+ {%- elif function['arguments'] is string -%}
+ {%- set argstr = function['arguments'] | trim -%}
+ {%- if argstr[:1] == '{' and argstr[-1:] == '}' -%}
+ {{- argstr[1:-1] -}}
+ {%- else -%}
+ {{- function['arguments'] -}}
+ {%- endif -%}
+ {%- endif -%}
+ {{- '}<tool_call|>' -}}
+ {%- endfor -%}
+ {%- set ns_tool.tool_response = false -%}
+ {%- for tool_response in message.get('tool_responses') or [] -%}
+ {{- format_tool_response_block(tool_response['name'] | default('unknown', true), tool_response['response']) -}}
+ {%- set ns_tool.tool_response = true -%}
+ {%- endfor -%}
+ {%- set ns_scan = namespace(stopped=false, next_role=none) -%}
+ {%- for k in range(loop.index0 + 1, loop_messages | length) -%}
+ {%- if ns_scan.stopped -%}
+ {%- elif loop_messages[k]['role'] != 'tool' -%}
+ {%- set ns_scan.stopped = true -%}
+ {%- set ns_scan.next_role = loop_messages[k]['role'] -%}
+ {%- elif not message.get('tool_responses') -%}
+ {%- set follow = loop_messages[k] -%}
+ {%- set ns_tname = namespace(name=follow.get('name') or 'unknown') -%}
+ {%- for tc in ns_tool.tool_calls -%}
+ {%- if tc.get('id') == follow.get('tool_call_id') -%}
+ {%- set ns_tname.name = tc['function']['name'] -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- set tool_body = follow.get('content') -%}
+ {%- if tool_body is sequence and tool_body is not string and tool_body is not mapping -%}
+ {%- set ns_txt = namespace(s='') -%}
+ {%- for part in tool_body -%}
+ {%- if part.get('type') == 'text' -%}
+ {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
+ {%- for part in tool_body -%}
+ {%- if part.get('type') in ['image', 'image_url'] -%}
+ {{- '<|image|>' -}}
+ {%- elif part.get('type') in ['audio', 'input_audio'] -%}
+ {{- '<|audio|>' -}}
+ {%- elif part.get('type') == 'video' -%}
+ {{- '<|video|>' -}}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- else -%}
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
+ {%- endif -%}
+ {%- set ns_tool.tool_response = true -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- endif -%}
+ {%- set captured_content -%}
{%- if message['content'] is string -%}
{%- if role == "model" -%}
{{ strip_thinking(message['content']) }}
@@ -1052,16 +1548,30 @@ gemma4_thinking_template = \
{%- endif -%}
{%- endif -%}
{%- endfor -%}
- {%- else -%}
+ {%- elif not ns_tool.tool_calls -%}
{{ raise_exception("Invalid content type") }}
{%- endif -%}
- {{ '<turn|>\n' }}
+ {%- endset -%}
+ {{- captured_content -}}
+ {%- if not ns_tool.tool_calls -%}
+ {{ '<turn|>\n' }}
+ {%- elif not ns_tool.tool_response -%}
+ {{- '<|tool_response>' -}}
+ {%- elif ns_scan.next_role != 'assistant' and (captured_content | trim | length > 0 or ns_scan.next_role) -%}
+ {{- '<turn|>\n' -}}
+ {%- set ns_tool.tool_calls = false -%}
+ {%- endif -%}
+ {%- endif -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
+ {%- if not ns_tool.tool_calls -%}
{{'<|turn>model\n'}}
{%- if not thinking -%}
{{ '<|channel>thought\n<channel|>' }}
{%- endif -%}
+ {%- elif ns_tool.tool_response and thinking -%}
+ {{- '<|channel>thought\n' -}}
+ {%- endif -%}
{%- endif -%}
"""
@@ -1074,13 +1584,13 @@ DEFAULT_SYSTEM_MESSAGE["gemma4-thinking"] = None
# 26B-A4B / 31B generate after an empty thought channel their template omits from history; final-turn reasoning fills it.
_gemma4_model_turn = "{{ '<|turn>' + role + '\n' }}\n"
gemma4_empty_thought_template = gemma4_thinking_template.replace(
- _gemma4_model_turn,
- _gemma4_model_turn + \
+ _gemma4_model_turn + " {%- endif -%}\n",
+ _gemma4_model_turn + " {%- endif -%}\n" + \
""" {%- if role == "model" -%}
{%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
{%- if thinking_text and loop.index0 > ns_turn.last_user_idx -%}
{{ '<|channel>thought\n' + thinking_text + '\n<channel|>' }}
- {%- elif not thinking -%}
+ {%- elif not thinking and not continues -%}
{{ '<|channel>thought\n<channel|>' }}
{%- endif -%}
{%- endif -%}