Skip to content

Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

gemma4: restore e2b-style nothink prompt (#15560)

Devon Rifkinbf2a4217271f
diff --git a/model/renderers/gemma4.go b/model/renderers/gemma4.go
index 7b186af9e..82f5fe5b1 100644
--- a/model/renderers/gemma4.go
+++ b/model/renderers/gemma4.go
@@ -124,9 +124,6 @@ func (r *Gemma4Renderer) Render(messages []api.Message, tools []api.Tool, thinkV
// Generation prompt.
if prevMessageType != "tool_response" && prevMessageType != "tool_call" {
sb.WriteString("<|turn>model\n")
- if !hasThink {
- sb.WriteString("<|channel>thought\n<channel|>")
- }
}
return sb.String(), nil
diff --git a/model/renderers/gemma4_reference_test.go b/model/renderers/gemma4_reference_test.go
index 05e0517ec..5c6458ffd 100644
--- a/model/renderers/gemma4_reference_test.go
+++ b/model/renderers/gemma4_reference_test.go
@@ -1,14 +1,18 @@
package renderers
-// TestGemma4RendererMatchesReference verifies our renderer matches the HF
-// Jinja2 chat template exactly.
+// TestGemma4RendererMatchesReference verifies our renderer matches the checked-in
+// Gemma 4 reference template.
//
-// To regenerate expected values, save gemma4Jinja2Template (below) to
-// gemma4_chat_template.jinja2 and run:
+// Current upstream Gemma 4 chat templates differ by model size, so the checked-in
+// reference intentionally uses the shared baseline without an empty generation-time
+// thought channel until renderer selection is split by size.
+//
+// To regenerate expected values, save the E2B template to
+// gemma4_e2b_chat_template.jinja2 and run:
//
// python3 -c "
// from jinja2 import Environment; import json
-// tmpl = Environment().from_string(open('gemma4_chat_template.jinja2').read())
+// tmpl = Environment().from_string(open('gemma4_e2b_chat_template.jinja2').read())
// msgs = [{'role':'user','content':'Hello'}]
// print(repr(tmpl.render(messages=msgs, bos_token='<bos>', add_generation_prompt=True)))
// "
@@ -26,8 +30,13 @@ import (
"github.com/stretchr/testify/assert"
)
-// The full Jinja2 template is committed as testdata/gemma4_chat_template.jinja2.
-// Run with VERIFY_JINJA2=1 to verify expected values against the template using uv + Python.
+const (
+ gemma4E2BTemplate = "testdata/gemma4_e2b_chat_template.jinja2"
+ gemma431BTemplate = "testdata/gemma4_31b_chat_template.jinja2"
+)
+
+// The upstream Gemma 4 chat templates are committed by size under testdata/.
+// Run with VERIFY_JINJA2=1 to verify expected values against the E2B template using uv + Python.
func bashRefTool() []api.Tool {
return []api.Tool{{
@@ -665,7 +674,7 @@ func TestGemma4RendererMatchesReference(t *testing.T) {
{
name: "user_only",
messages: []api.Message{{Role: "user", Content: "Hello"}},
- expected: "<bos><|turn>user\nHello<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ expected: "<bos><|turn>user\nHello<turn|>\n<|turn>model\n",
},
{
name: "system_user",
@@ -673,7 +682,7 @@ func TestGemma4RendererMatchesReference(t *testing.T) {
{Role: "system", Content: "You are helpful."},
{Role: "user", Content: "Hi"},
},
- expected: "<bos><|turn>system\nYou are helpful.<turn|>\n<|turn>user\nHi<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ expected: "<bos><|turn>system\nYou are helpful.<turn|>\n<|turn>user\nHi<turn|>\n<|turn>model\n",
},
{
name: "developer_user",
@@ -681,13 +690,13 @@ func TestGemma4RendererMatchesReference(t *testing.T) {
{Role: "developer", Content: "You are helpful."},
{Role: "user", Content: "Hi"},
},
- expected: "<bos><|turn>system\nYou are helpful.<turn|>\n<|turn>user\nHi<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ expected: "<bos><|turn>system\nYou are helpful.<turn|>\n<|turn>user\nHi<turn|>\n<|turn>model\n",
},
{
name: "tools_no_system",
messages: []api.Message{{Role: "user", Content: "Hi"}},
tools: bashRefTool(),
- expected: "<bos><|turn>system\n" + bashDeclRef + "<turn|>\n<|turn>user\nHi<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ expected: "<bos><|turn>system\n" + bashDeclRef + "<turn|>\n<|turn>user\nHi<turn|>\n<|turn>model\n",
},
{
name: "system_tools",
@@ -696,7 +705,7 @@ func TestGemma4RendererMatchesReference(t *testing.T) {
{Role: "user", Content: "Hi"},
},
tools: bashRefTool(),
- expected: "<bos><|turn>system\nYou are helpful." + bashDeclRef + "<turn|>\n<|turn>user\nHi<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ expected: "<bos><|turn>system\nYou are helpful." + bashDeclRef + "<turn|>\n<|turn>user\nHi<turn|>\n<|turn>model\n",
},
{
name: "thinking_no_system",
@@ -730,6 +739,12 @@ func TestGemma4RendererMatchesReference(t *testing.T) {
think: thinkTrue(),
expected: "<bos><|turn>system\n<|think|>\nYou are helpful." + bashDeclRef + "<turn|>\n<|turn>user\nHi<turn|>\n<|turn>model\n",
},
+ {
+ name: "thinking_explicitly_disabled",
+ messages: []api.Message{{Role: "user", Content: "Hi"}},
+ think: thinkFalse(),
+ expected: "<bos><|turn>user\nHi<turn|>\n<|turn>model\n",
+ },
// === Message loop paths ===
{
@@ -744,7 +759,7 @@ func TestGemma4RendererMatchesReference(t *testing.T) {
"<|turn>user\nHi<turn|>\n" +
"<|turn>model\nHello!<turn|>\n" +
"<|turn>user\nMore<turn|>\n" +
- "<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>model\n",
},
{
// Tool call with structured args → tool response as separate <|turn>tool turn
@@ -806,7 +821,7 @@ func TestGemma4RendererMatchesReference(t *testing.T) {
"<|tool_response>response:bash{value:" + q + "file1.txt\nfile2.txt" + q + "}<tool_response|>" +
"Here are the files.<turn|>\n" +
"<|turn>user\nRead file1.txt<turn|>\n" +
- "<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>model\n",
},
{
// Multiple tool calls + multiple tool responses
@@ -841,7 +856,7 @@ func TestGemma4RendererMatchesReference(t *testing.T) {
expected: "<bos><|turn>user\nWhat is 2+2?<turn|>\n" +
"<|turn>model\n4<turn|>\n" +
"<|turn>user\nAnd 3+3?<turn|>\n" +
- "<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>model\n",
},
// === Additional edge cases ported from original tests ===
{
@@ -899,17 +914,17 @@ func TestGemma4RendererMatchesReference(t *testing.T) {
messages: []api.Message{{Role: "user", Content: "Test"}},
tools: modeTool(),
expected: "<bos><|turn>system\n" + modeDeclRef + "<turn|>\n" +
- "<|turn>user\nTest<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>user\nTest<turn|>\n<|turn>model\n",
},
{
name: "unicode_content",
messages: []api.Message{{Role: "user", Content: "こんにちは"}},
- expected: "<bos><|turn>user\nこんにちは<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ expected: "<bos><|turn>user\nこんにちは<turn|>\n<|turn>model\n",
},
{
name: "newlines_in_content",
messages: []api.Message{{Role: "user", Content: "Line 1\nLine 2\nLine 3"}},
- expected: "<bos><|turn>user\nLine 1\nLine 2\nLine 3<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ expected: "<bos><|turn>user\nLine 1\nLine 2\nLine 3<turn|>\n<|turn>model\n",
},
{
// Tool response (raw JSON) followed by user message
@@ -928,7 +943,7 @@ func TestGemma4RendererMatchesReference(t *testing.T) {
"<|turn>model\n<|tool_call>call:get_weather{city:" + q + "Tokyo" + q + "}<tool_call|>" +
"<|tool_response>response:get_weather{value:" + q + `{"temperature": 15, "weather": "sunny"}` + q + "}<tool_response|>" +
"<|turn>user\nThanks!<turn|>\n" +
- "<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>model\n",
},
// === Ordering and whitespace edge cases ===
{
@@ -951,7 +966,7 @@ func TestGemma4RendererMatchesReference(t *testing.T) {
// User content with whitespace is trimmed
name: "user_content_trimmed",
messages: []api.Message{{Role: "user", Content: " hello "}},
- expected: "<bos><|turn>user\nhello<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ expected: "<bos><|turn>user\nhello<turn|>\n<|turn>model\n",
},
{
// Empty tool call arguments
@@ -975,7 +990,7 @@ func TestGemma4RendererMatchesReference(t *testing.T) {
messages: []api.Message{{Role: "user", Content: "Create"}},
tools: nestedTool(),
expected: "<bos><|turn>system\n" + nestedDeclRef + "<turn|>\n" +
- "<|turn>user\nCreate<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>user\nCreate<turn|>\n<|turn>model\n",
},
{
// Array type in tool declaration
@@ -983,7 +998,7 @@ func TestGemma4RendererMatchesReference(t *testing.T) {
messages: []api.Message{{Role: "user", Content: "Batch"}},
tools: arrayTool(),
expected: "<bos><|turn>system\n" + arrayDeclRef + "<turn|>\n" +
- "<|turn>user\nBatch<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>user\nBatch<turn|>\n<|turn>model\n",
},
{
// Top-level typed union follows the template's odd stringified-list form.
@@ -995,8 +1010,7 @@ func TestGemma4RendererMatchesReference(t *testing.T) {
<|turn>user
Hi<turn|>
<|turn>model
-<|channel>thought
-<channel|>`,
+`,
},
{
// Assistant whitespace is trimmed (strip_thinking includes | trim)
@@ -1009,7 +1023,7 @@ Hi<turn|>
expected: "<bos><|turn>user\nHi<turn|>\n" +
"<|turn>model\nspaced<turn|>\n" +
"<|turn>user\nMore<turn|>\n" +
- "<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>model\n",
},
{
// Three sequential tool responses
@@ -1064,7 +1078,7 @@ Hi<turn|>
expected: "<bos><|turn>user\nHi<turn|>\n" +
"<|turn>model\nMiddleDone<turn|>\n" +
"<|turn>user\nMore<turn|>\n" +
- "<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>model\n",
},
{
// Property with no description — just type
@@ -1072,7 +1086,7 @@ Hi<turn|>
messages: []api.Message{{Role: "user", Content: "Count"}},
tools: countTool(),
expected: "<bos><|turn>system\n" + countDeclRef + "<turn|>\n" +
- "<|turn>user\nCount<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>user\nCount<turn|>\n<|turn>model\n",
},
{
// System message with leading/trailing whitespace is trimmed
@@ -1082,7 +1096,7 @@ Hi<turn|>
{Role: "user", Content: "Hi"},
},
expected: "<bos><|turn>system\nYou are helpful.<turn|>\n" +
- "<|turn>user\nHi<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>user\nHi<turn|>\n<|turn>model\n",
},
{
// Deeply nested map in tool call arguments (3 levels)
@@ -1144,7 +1158,7 @@ Hi<turn|>
messages: []api.Message{{Role: "user", Content: "Set"}},
tools: enumNoDescTool(),
expected: "<bos><|turn>system\n" + enumNoDescDeclRef + "<turn|>\n" +
- "<|turn>user\nSet<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>user\nSet<turn|>\n<|turn>model\n",
},
{
// System message that is only whitespace (trims to empty)
@@ -1154,7 +1168,7 @@ Hi<turn|>
{Role: "user", Content: "Hi"},
},
expected: "<bos><|turn>system\n<turn|>\n" +
- "<|turn>user\nHi<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>user\nHi<turn|>\n<|turn>model\n",
},
{
// Empty assistant content (empty string, not nil)
@@ -1167,7 +1181,7 @@ Hi<turn|>
expected: "<bos><|turn>user\nHi<turn|>\n" +
"<|turn>model\n<turn|>\n" +
"<|turn>user\nMore<turn|>\n" +
- "<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>model\n",
},
{
// Map argument with string keys (keys NOT escaped with <|"|>)
@@ -1193,7 +1207,7 @@ Hi<turn|>
messages: []api.Message{{Role: "user", Content: "Search"}},
tools: searchTool(),
expected: "<bos><|turn>system\n" + searchDeclRef + "<turn|>\n" +
- "<|turn>user\nSearch<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>user\nSearch<turn|>\n<|turn>model\n",
},
// === Round 3 coverage gaps ===
@@ -1221,7 +1235,7 @@ Hi<turn|>
{Role: "user", Content: "Hi"},
},
expected: "<bos><|turn>system\n<turn|>\n" +
- "<|turn>user\nHi<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>user\nHi<turn|>\n<|turn>model\n",
},
{
// Nested OBJECT property with required field
@@ -1229,7 +1243,7 @@ Hi<turn|>
messages: []api.Message{{Role: "user", Content: "Create"}},
tools: nestedRequiredTool(),
expected: "<bos><|turn>system\n" + nestedRequiredDeclRef + "<turn|>\n" +
- "<|turn>user\nCreate<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>user\nCreate<turn|>\n<|turn>model\n",
},
{
// Non-integer float in tool call argument
@@ -1256,7 +1270,7 @@ Hi<turn|>
},
expected: "<bos><|turn>user\nHi<turn|>\n" +
"<|turn>model\nResult<turn|>\n" +
- "<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>model\n",
},
{
// Tool content with newlines and leading/trailing whitespace trimmed
@@ -1280,7 +1294,7 @@ Hi<turn|>
messages: []api.Message{{Role: "user", Content: "Raw"}},
tools: rawTool(),
expected: "<bos><|turn>system\n" + rawDeclRef + "<turn|>\n" +
- "<|turn>user\nRaw<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>user\nRaw<turn|>\n<|turn>model\n",
},
{
// Multiple required fields at top level
@@ -1288,7 +1302,7 @@ Hi<turn|>
messages: []api.Message{{Role: "user", Content: "Move"}},
tools: moveTool(),
expected: "<bos><|turn>system\n" + moveDeclRef + "<turn|>\n" +
- "<|turn>user\nMove<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>user\nMove<turn|>\n<|turn>model\n",
},
{
// Assistant content that is ONLY thinking (strips to empty)
@@ -1301,7 +1315,7 @@ Hi<turn|>
expected: "<bos><|turn>user\nHi<turn|>\n" +
"<|turn>model\n<turn|>\n" +
"<|turn>user\nMore<turn|>\n" +
- "<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>model\n",
},
// === Round 4: final coverage gaps ===
@@ -1334,7 +1348,7 @@ Hi<turn|>
messages: []api.Message{{Role: "user", Content: "Tag"}},
tools: arrayNoItemsTool(),
expected: "<bos><|turn>system\n" + arrayNoItemsDeclRef + "<turn|>\n" +
- "<|turn>user\nTag<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>user\nTag<turn|>\n<|turn>model\n",
},
{
// OBJECT property without description but with nested properties
@@ -1342,7 +1356,7 @@ Hi<turn|>
messages: []api.Message{{Role: "user", Content: "Update"}},
tools: objectNoDescTool(),
expected: "<bos><|turn>system\n" + objectNoDescDeclRef + "<turn|>\n" +
- "<|turn>user\nUpdate<turn|>\n<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>user\nUpdate<turn|>\n<|turn>model\n",
},
// === Round 5: coding agent patterns ===
@@ -1372,7 +1386,7 @@ Hi<turn|>
"<|tool_response>response:bash{value:" + q + q + "}<tool_response|>" +
"Done.<turn|>\n" +
"<|turn>user\nThanks<turn|>\n" +
- "<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>model\n",
},
{
// Tool call with thinking that strips to real remaining content
@@ -1392,7 +1406,7 @@ Hi<turn|>
"<|tool_response>response:bash{value:" + q + "main.go\ngo.mod" + q + "}<tool_response|>" +
"Let me list the files.<turn|>\n" +
"<|turn>user\nOK<turn|>\n" +
- "<|turn>model\n<|channel>thought\n<channel|>",
+ "<|turn>model\n",
},
{
// Argument value containing newlines (multi-line script)
@@ -1720,12 +1734,35 @@ func TestGemma4RendererToolResponseWithoutNameOrIDUsesUnknown(t *testing.T) {
assert.NotContains(t, got, `response:read{value:<|"|>payload<|"|>}`)
}
+func TestGemma4SizeTemplateFixturesDifferAtGenerationPrompt(t *testing.T) {
+ e2b, err := os.ReadFile(gemma4E2BTemplate)
+ if err != nil {
+ t.Fatalf("failed to read %s: %v", gemma4E2BTemplate, err)
+ }
+
+ thirtyOneB, err := os.ReadFile(gemma431BTemplate)
+ if err != nil {
+ t.Fatalf("failed to read %s: %v", gemma431BTemplate, err)
+ }
+
+ assert.Contains(t, string(e2b), "{{- '<|turn>model\\n' -}}")
+ assert.NotContains(t, string(e2b), "{{- '<|channel>thought\\n<channel|>' -}}")
+ assert.Contains(t, string(thirtyOneB), "{{- '<|turn>model\\n' -}}")
+ assert.Contains(t, string(thirtyOneB), "{{- '<|channel>thought\\n<channel|>' -}}")
+}
+
// renderWithJinja2 shells out to uv + Python to render messages through the
-// Jinja2 chat template. Returns the rendered string.
+// E2B Jinja2 chat template. Returns the rendered string.
func renderWithJinja2(t *testing.T, messages []api.Message, tools []api.Tool, think *api.ThinkValue) string {
+ return renderWithJinja2Template(t, gemma4E2BTemplate, messages, tools, think)
+}
+
+// renderWithJinja2Template shells out to uv + Python to render messages through
+// the named Jinja2 chat template. Returns the rendered string.
+func renderWithJinja2Template(t *testing.T, templateRelPath string, messages []api.Message, tools []api.Tool, think *api.ThinkValue) string {
t.Helper()
- templatePath, err := filepath.Abs("testdata/gemma4_chat_template.jinja2")
+ templatePath, err := filepath.Abs(templateRelPath)
if err != nil {
t.Fatalf("failed to get template path: %v", err)
}
@@ -1814,3 +1851,7 @@ print(tmpl.render(**kwargs), end="")
func thinkTrue() *api.ThinkValue {
return &api.ThinkValue{Value: true}
}
+
+func thinkFalse() *api.ThinkValue {
+ return &api.ThinkValue{Value: false}
+}
diff --git a/model/renderers/testdata/gemma4_chat_template.jinja2 b/model/renderers/testdata/gemma4_31b_chat_template.jinja2
similarity index 100%
rename from model/renderers/testdata/gemma4_chat_template.jinja2
rename to model/renderers/testdata/gemma4_31b_chat_template.jinja2
diff --git a/model/renderers/testdata/gemma4_e2b_chat_template.jinja2 b/model/renderers/testdata/gemma4_e2b_chat_template.jinja2
new file mode 100644
index 000000000..6ec327b0b
--- /dev/null
+++ b/model/renderers/testdata/gemma4_e2b_chat_template.jinja2
@@ -0,0 +1,344 @@
+{%- macro format_parameters(properties, required) -%}
+ {%- 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 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([])) -}}
+ }
+ {%- 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 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 -%}
+ {%- set ns.result = ns.result + part.split('<|channel>')[0] -%}
+ {%- else -%}
+ {%- set ns.result = ns.result + part -%}
+ {%- endif -%}
+ {%- 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 ns = namespace(prev_message_type=None) -%}
+{%- set loop_messages = messages -%}
+{{- bos_token -}}
+{#- Handle System/Tool Definitions Block -#}
+{%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
+ {{- '<|turn>system\n' -}}
+
+ {#- Inject Thinking token at the very top of the FIRST system turn -#}
+ {%- if enable_thinking is defined and enable_thinking -%}
+ {{- '<|think|>\n' -}}
+ {%- set ns.prev_message_type = 'think' -%}
+ {%- endif -%}
+
+ {%- if messages[0]['role'] in ['system', 'developer'] -%}
+ {{- messages[0]['content'] | trim -}}
+ {%- set loop_messages = messages[1:] -%}
+ {%- endif -%}
+
+ {%- if tools -%}
+ {%- for tool in tools %}
+ {{- '<|tool>' -}}
+ {{- format_function_declaration(tool) | trim -}}
+ {{- '<tool|>' -}}
+ {%- endfor %}
+ {%- set ns.prev_message_type = 'tool' -%}
+ {%- endif -%}
+
+ {{- '<turn|>\n' -}}
+{%- endif %}
+
+{#- Pre-scan: find last user message index for reasoning guard -#}
+{%- set ns_turn = namespace(last_user_idx=-1) -%}
+{%- for i in range(loop_messages | length) -%}
+ {%- if loop_messages[i]['role'] == 'user' -%}
+ {%- set ns_turn.last_user_idx = i -%}
+ {%- endif -%}
+{%- endfor -%}
+
+{#- Loop through messages -#}
+{%- for message in loop_messages -%}
+ {%- if message['role'] != 'tool' -%}
+ {%- set ns.prev_message_type = None -%}
+ {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
+ {#- Detect continuation: suppress duplicate <|turn>model when previous non-tool message was also assistant -#}
+ {%- set prev_nt = namespace(role=None, found=false) -%}
+ {%- if loop.index0 > 0 -%}
+ {%- for j in range(loop.index0 - 1, -1, -1) -%}
+ {%- if not prev_nt.found -%}
+ {%- if loop_messages[j]['role'] != 'tool' -%}
+ {%- set prev_nt.role = loop_messages[j]['role'] -%}
+ {%- set prev_nt.found = true -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- endif -%}
+ {%- set continue_same_model_turn = (role == 'model' and prev_nt.role == 'assistant') -%}
+ {%- if not continue_same_model_turn -%}
+ {{- '<|turn>' + role + '\n' }}
+ {%- endif -%}
+
+ {#- Render reasoning/reasoning_content as thinking channel -#}
+ {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
+ {%- if thinking_text and loop.index0 > ns_turn.last_user_idx and message.get('tool_calls') -%}
+ {{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}}
+ {%- endif -%}
+
+ {%- if message['tool_calls'] -%}
+ {%- for tool_call in message['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 -%}
+ {{- function['arguments'] -}}
+ {%- endif -%}
+ {{- '}<tool_call|>' -}}
+ {%- endfor -%}
+ {%- set ns.prev_message_type = 'tool_call' -%}
+ {%- endif -%}
+
+ {%- set ns_tr_out = namespace(flag=false) -%}
+ {%- if message.get('tool_responses') -%}
+ {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
+ {%- for tool_response in message['tool_responses'] -%}
+ {{- format_tool_response_block(tool_response['name'] | default('unknown'), tool_response['response']) -}}
+ {%- set ns_tr_out.flag = true -%}
+ {%- set ns.prev_message_type = 'tool_response' -%}
+ {%- endfor -%}
+ {%- elif message.get('tool_calls') -%}
+ {#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}
+ {%- set ns_tool_scan = namespace(stopped=false) -%}
+ {%- for k in range(loop.index0 + 1, loop_messages | length) -%}
+ {%- if ns_tool_scan.stopped -%}
+ {%- elif loop_messages[k]['role'] != 'tool' -%}
+ {%- set ns_tool_scan.stopped = true -%}
+ {%- else -%}
+ {%- set follow = loop_messages[k] -%}
+ {#- Resolve tool_call_id to function name -#}
+ {%- set ns_tname = namespace(name=follow.get('name') | default('unknown')) -%}
+ {%- for tc in message['tool_calls'] -%}
+ {%- if tc.get('id') == follow.get('tool_call_id') -%}
+ {%- set ns_tname.name = tc['function']['name'] -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {#- Handle content as string or content-parts array -#}
+ {%- set tool_body = follow.get('content') -%}
+ {%- if tool_body is string -%}
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
+ {%- elif tool_body is sequence and tool_body is not string -%}
+ {%- 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) -}}
+ {%- else -%}
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
+ {%- endif -%}
+ {%- set ns_tr_out.flag = true -%}
+ {%- set ns.prev_message_type = 'tool_response' -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- endif -%}
+
+ {%- if message['content'] is string -%}
+ {%- if role == 'model' -%}
+ {{- strip_thinking(message['content']) -}}
+ {%- else -%}
+ {{- message['content'] | trim -}}
+ {%- endif -%}
+ {%- elif message['content'] is sequence -%}
+ {%- for item in message['content'] -%}
+ {%- if item['type'] == 'text' -%}
+ {%- if role == 'model' -%}
+ {{- strip_thinking(item['text']) -}}
+ {%- else -%}
+ {{- item['text'] | trim -}}
+ {%- endif -%}
+ {%- elif item['type'] == 'image' -%}
+ {{- '<|image|>' -}}
+ {%- set ns.prev_message_type = 'image' -%}
+ {%- elif item['type'] == 'audio' -%}
+ {{- '<|audio|>' -}}
+ {%- set ns.prev_message_type = 'audio' -%}
+ {%- elif item['type'] == 'video' -%}
+ {{- '<|video|>' -}}
+ {%- set ns.prev_message_type = 'video' -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- endif -%}
+
+ {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
+ {{- '<|tool_response>' -}}
+ {%- elif not (ns_tr_out.flag and not message.get('content')) -%}
+ {{- '<turn|>\n' -}}
+ {%- endif -%}
+ {%- endif -%}
+{%- endfor -%}
+
+{%- if add_generation_prompt -%}
+ {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
+ {{- '<|turn>model\n' -}}
+ {%- endif -%}
+{%- endif -%}