Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ The test suite compiles and renders the chat templates from these models:
| gemma-4-26B-A4B-it | Gemma 4 |
| GLM-5.2 | GLM 5 |
| gpt-oss-20b | GPT-OSS |
| Inkling-2-Mini | Inkling 2 |
| Kimi-K3 | Kimi K3 |
| laguna | Laguna |
| LFM2.5-VL-1.6B | LFM2.5 |
Expand All @@ -164,6 +165,13 @@ tool definitions. The unmodified upstream template is included in the
byte-for-byte comparisons against Python Jinja2. These tests validate template
rendering, not model inference.

Inkling 2 coverage uses the unmodified template embedded in
`ggml-org/Inkling-2-Mini-GGUF/Inkling-2-Mini-Q8_0.gguf`. It includes parallel
tool calls and out-of-order replies matched by call ID, named assistant messages,
reasoning-effort defaults and bounds, and image markers and unsupported audio.
The template SHA-256 is
`53a9035f3255945704b5857657cfb00c1165090c0641c4f782a0cbf268cc16dc`.

## Provisioning Compatibility Tests

The self-contained 5G network provisioning template lives separately in
Expand Down
131 changes: 131 additions & 0 deletions jinja_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,137 @@ func TestRender_KimiK3LlamaCpp(t *testing.T) {
})
}

// The unmodified template is tokenizer.chat_template from
// ggml-org/Inkling-2-Mini-GGUF/Inkling-2-Mini-Q8_0.gguf. The template SHA-256 is
// 53a9035f3255945704b5857657cfb00c1165090c0641c4f782a0cbf268cc16dc.
// TestRequestsAgainstTemplates also compares it against Python Jinja2.
func TestRender_Inkling2_ToolRoundTrip(t *testing.T) {
tmpl, err := jinja.Compile(chatTemplates["Inkling-2-Mini"])
if err != nil {
t.Fatalf("compile: %v", err)
}
req := loadRequests(t)["kronk-req-inkling"]
data := buildRenderData(req)
data["reasoning_effort"] = 0.37
got, err := tmpl.Render(data)
if err != nil {
t.Fatalf("render: %v", err)
}
for _, fragment := range []string{
`<|message_system|>tool_declare<|content_xml|>[{"description":"Analyze a note.","name":"analyze","parameters":{"properties":{"count":{"type":"integer"},"options":{"type":"object"},"text":{"type":"string"}},"type":"object"},"type":"function"},`,
`<|message_system|><|content_text|>Keep the original argument values.<|end_message|><|message_system|><|content_text|>Thinking effort level: 0.37<|end_message|><|message_user|>`,
`<|message_model|><|content_thinking|>I can run both tools.<|end_message|><|message_model|>analyze<|content_invoke_tool_json|>{"name":"analyze","args":{"count":9007199254740993,"options":{"a":[2,1],"z":true},"text":"café & <tag>\nnext line"}}<|end_message|>`,
`<|message_model|>lookup<|content_invoke_tool_json|>{"name":"lookup","args":{"id":17}}<|end_message|><|content_model_end_sampling|><|message_tool|>lookup<|content_text|>Ready.<|end_message|><|message_tool|>analyze<|content_text|>Analysis complete.<|end_message|>`,
} {
if !strings.Contains(got, fragment) {
t.Errorf("output missing %q", fragment)
}
}
if want := `<|message_model|>reporter<|content_text|>Both finished.<|end_message|><|content_model_end_sampling|>`; !strings.HasSuffix(got, want) {
t.Errorf("ending: got %q, want suffix %q", got, want)
}
}

func TestRender_Inkling2_ReasoningEffort(t *testing.T) {
tmpl, err := jinja.Compile(chatTemplates["Inkling-2-Mini"])
if err != nil {
t.Fatalf("compile: %v", err)
}
for _, tt := range []struct {
name string
kwargs map[string]any
want string
wantErr string
}{
{name: "default", want: "0.9"},
{name: "null", kwargs: map[string]any{"reasoning_effort": nil}, want: "0.9"},
{name: "none", kwargs: map[string]any{"reasoning_effort": "none"}, want: "0"},
{name: "minimal", kwargs: map[string]any{"reasoning_effort": "minimal"}, want: "0.1"},
{name: "low", kwargs: map[string]any{"reasoning_effort": "low"}, want: "0.2"},
{name: "medium", kwargs: map[string]any{"reasoning_effort": "medium"}, want: "0.7"},
{name: "high with whitespace", kwargs: map[string]any{"reasoning_effort": " high "}, want: "0.9"},
{name: "xhigh", kwargs: map[string]any{"reasoning_effort": "xhigh"}, want: "0.99"},
{name: "max", kwargs: map[string]any{"reasoning_effort": "max"}, want: "0.99"},
{name: "numeric zero", kwargs: map[string]any{"reasoning_effort": 0.0}, want: "0"},
{name: "numeric upper bound", kwargs: map[string]any{"reasoning_effort": 0.99}, want: "0.99"},
{name: "below lower bound", kwargs: map[string]any{"reasoning_effort": -0.01}, wantErr: "reasoning_effort must be in [0.0, 0.99]"},
{name: "above upper bound", kwargs: map[string]any{"reasoning_effort": 1.0}, wantErr: "reasoning_effort must be in [0.0, 0.99]"},
{name: "unknown", kwargs: map[string]any{"reasoning_effort": "extreme"}, wantErr: "Unknown reasoning_effort: extreme"},
} {
t.Run(tt.name, func(t *testing.T) {
data := map[string]any{
"messages": []map[string]any{
{"role": "system", "content": "System."},
{"role": "user", "content": "Hello."},
},
"add_generation_prompt": true,
}
maps.Copy(data, tt.kwargs)
// Reusing the compiled template must not retain effort_emitted.
for range 2 {
got, err := tmpl.Render(data)
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("render: got %v, want error containing %q", err, tt.wantErr)
}
continue
}
if err != nil {
t.Fatalf("render: %v", err)
}
want := "<|message_system|><|content_text|>System.<|end_message|><|message_system|><|content_text|>Thinking effort level: " + tt.want + "<|end_message|><|message_user|><|content_text|>Hello.<|end_message|>"
if got != want {
t.Errorf("output: got %q, want %q", got, want)
}
}
})
}
}

func TestRender_Inkling2_Media(t *testing.T) {
tmpl, err := jinja.Compile(chatTemplates["Inkling-2-Mini"])
if err != nil {
t.Fatalf("compile: %v", err)
}
for _, tt := range []struct {
name, role, part string
wantErr string
}{
{name: "user image", role: "user", part: "image_url"},
{name: "tool image", role: "tool", part: "input_image"},
{name: "assistant image", role: "assistant", part: "image", wantErr: "Image content is only supported for user and tool messages"},
{name: "audio", role: "user", part: "input_audio", wantErr: "Inkling 2 does not accept audio input"},
} {
t.Run(tt.name, func(t *testing.T) {
got, err := tmpl.Render(map[string]any{
"reasoning_effort": "none",
"messages": []map[string]any{{"role": tt.role, "content": []any{
map[string]any{"type": "text", "text": "Before."},
map[string]any{"type": tt.part},
map[string]any{"type": "input_text", "text": "After."},
}}},
})
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("render: got %v, want error containing %q", err, tt.wantErr)
}
return
}
if err != nil {
t.Fatalf("render: %v", err)
}
role := "<|message_" + tt.role + "|>"
want := "<|message_system|><|content_text|>Thinking effort level: 0<|end_message|>" +
role + "<|content_text|>Before.<|end_message|>" +
role + "<|content_image|><|unused_200054|><|end_message|>" +
role + "<|content_text|>After.<|end_message|>"
if got != want {
t.Errorf("output: got %q, want %q", got, want)
}
})
}
}

func TestRegression(t *testing.T) {
for name, content := range chatTemplates {
t.Run(name, func(t *testing.T) {
Expand Down
30 changes: 30 additions & 0 deletions testdata/requests/kronk-req-inkling.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"chat_template_kwargs": {"Inkling-2-Mini": {"reasoning_effort": 0.37}},
"messages": [
{"role": "system", "content": "Keep the original argument values."},
{"role": "user", "content": "Analyze the note and look up its status."},
{
"role": "assistant",
"content": "",
"reasoning_content": "I can run both tools.",
"tool_calls": [
{
"id": "note",
"type": "function",
"function": {
"name": "analyze",
"arguments": "{\"count\":9007199254740993,\"options\":{\"a\":[2,1],\"z\":true},\"text\":\"café & <tag>\\nnext line\"}"
}
},
{"id": "status", "type": "function", "function": {"name": "lookup", "arguments": {"id": 17}}}
]
},
{"role": "tool", "tool_call_id": "status", "content": "Ready."},
{"role": "tool", "tool_call_id": "note", "content": "Analysis complete."},
{"role": "assistant", "name": "reporter", "content": "Both finished."}
],
"tools": [
{"function": {"description": "Analyze a note.", "name": "analyze", "parameters": {"properties": {"count": {"type": "integer"}, "options": {"type": "object"}, "text": {"type": "string"}}, "type": "object"}}, "type": "function"},
{"function": {"description": "Look up a status.", "name": "lookup", "parameters": {"properties": {"id": {"type": "integer"}}, "type": "object"}}, "type": "function"}
]
}
137 changes: 137 additions & 0 deletions testdata/templates/Inkling-2-Mini.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{%- set effort_map = {"none": 0.0, "minimal": 0.1, "low": 0.2, "medium": 0.7, "high": 0.9, "xhigh": 0.99, "max": 0.99} -%}
{%- set role_token = {"user": "<|message_user|>", "assistant": "<|message_model|>", "system": "<|message_system|>", "tool": "<|message_tool|>"} -%}

{%- macro emit_thinking_effort() -%}
{%- set eff = reasoning_effort if reasoning_effort is defined and reasoning_effort is not none else 0.9 -%}
{%- if eff is string -%}
{%- set key = eff | trim -%}
{%- if key not in effort_map -%}
{{- raise_exception("Unknown reasoning_effort: " ~ eff) -}}
{%- endif -%}
{%- set num = effort_map[key] -%}
{%- else -%}
{%- set num = eff | float -%}
{%- endif -%}
{%- if num < 0.0 or num > 0.99 -%}
{{- raise_exception("reasoning_effort must be in [0.0, 0.99]") -}}
{%- endif -%}
{{- "<|message_system|><|content_text|>Thinking effort level: " -}}
{%- if num == 0.0 -%}0{%- else -%}{{ num }}{%- endif -%}
{{- "<|end_message|>" -}}
{%- endmacro -%}

{%- if tools -%}
{%- set tool_state = namespace(specs=[]) -%}
{%- for tool in tools -%}
{%- set fn = tool.function if tool.function is defined else tool -%}
{%- set spec = {
"description": (fn.description if fn.description is defined and fn.description else ""),
"name": fn.name,
"parameters": (fn.parameters if fn.parameters is defined and fn.parameters else {}),
"type": (tool.type if tool.type is defined and tool.type else "function"),
} -%}
{%- set tool_state.specs = tool_state.specs + [spec] -%}
{%- endfor -%}
{{- "<|message_system|>tool_declare<|content_xml|>" -}}
{{- tool_state.specs | tojson(sort_keys=true, separators=(",", ":")) -}}
{{- "<|end_message|>" -}}
{%- endif -%}

{%- set state = namespace(effort_emitted=false) -%}
{%- for message in messages -%}
{%- if message.role not in role_token -%}
{{- raise_exception("Unknown message role: " ~ message.role) -}}
{%- endif -%}
{%- if not state.effort_emitted and message.role != "system" -%}
{{- emit_thinking_effort() -}}
{%- set state.effort_emitted = true -%}
{%- endif -%}

{%- set header = namespace(text=role_token[message.role]) -%}
{%- if message.role == "tool" -%}
{%- if message.name is defined and message.name -%}
{%- set header.text = header.text ~ message.name -%}
{%- elif message.tool_call_id is defined and message.tool_call_id -%}
{%- for prev in messages -%}
{%- if prev.role == "assistant" and prev.tool_calls -%}
{%- for tc in prev.tool_calls -%}
{%- if tc.id is defined and tc.id == message.tool_call_id and tc.function.name is defined -%}
{%- set header.text = header.text ~ tc.function.name -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- elif message.role == "assistant" and message.name is defined and message.name -%}
{%- set header.text = header.text ~ message.name -%}
{%- endif -%}
{%- set rtok = header.text -%}
{%- set rendered -%}

{%- if message.role == "assistant" and message.reasoning_content is defined and message.reasoning_content -%}
{{- rtok ~ "<|content_thinking|>" ~ message.reasoning_content ~ "<|end_message|>" -}}
{%- endif -%}

{%- if message.content is string and (message.content or not (message.role == "assistant" and (message.tool_calls or message.reasoning_content))) -%}
{{- rtok ~ "<|content_text|>" ~ message.content ~ "<|end_message|>" -}}
{%- elif message.content -%}
{%- for part in message.content -%}
{%- if part is string -%}
{{- rtok ~ "<|content_text|>" ~ part ~ "<|end_message|>" -}}
{%- elif part.type is not defined or part.type in ("text", "input_text") -%}
{%- set text_part = (part.text if part.text is defined and part.text is string else "") -%}
{{- rtok ~ "<|content_text|>" ~ text_part ~ "<|end_message|>" -}}
{%- elif part.type in ("image", "input_image", "image_url") -%}
{%- if message.role not in ("user", "tool") -%}
{{- raise_exception("Image content is only supported for user and tool messages") -}}
{%- endif -%}
{{- rtok ~ "<|content_image|><|unused_200054|><|end_message|>" -}}
{%- elif part.type in ("audio", "input_audio", "audio_url") -%}
{{- raise_exception("Inkling 2 does not accept audio input") -}}
{%- else -%}
{{- raise_exception("Unsupported content part type: " ~ part.type) -}}
{%- endif -%}
{%- endfor -%}
{%- endif -%}

{%- if message.role == "assistant" and message.tool_calls -%}
{%- for tc in message.tool_calls -%}
{%- set fn = tc.function -%}
{%- if fn.name is not defined or fn.name is not string -%}
{{- raise_exception("tool call function name must be a string") -}}
{%- endif -%}
{%- if message.name is defined and message.name and message.name != fn.name -%}
{{- raise_exception("assistant name " ~ message.name ~ " does not match its tool call " ~ fn.name) -}}
{%- endif -%}
{%- set args = fn.arguments if fn.arguments is defined and fn.arguments else {} -%}
{%- if args is string -%}
{{- raise_exception("tool call arguments must be a parsed object, not a JSON string; canonicalize upstream") -}}
{%- endif -%}
{%- if args is not mapping -%}
{{- raise_exception("tool call arguments must be an object") -}}
{%- endif -%}
{{- "<|message_model|>" ~ fn.name ~ "<|content_invoke_tool_json|>" -}}
{{- '{"name":' ~ (fn.name | tojson(sort_keys=true, separators=(",", ":"))) ~ ',"args":' -}}
{{- "{" -}}
{%- for key, value in args | dictsort(case_sensitive=true) -%}
{{- (key | tojson) ~ ":" ~ (value | tojson(separators=(",", ":"))) -}}
{%- if not loop.last -%},{%- endif -%}
{%- endfor -%}
{{- "}}<|end_message|>" -}}
{%- endfor -%}
{%- endif -%}

{%- if message.role == "assistant" -%}
{{- "<|content_model_end_sampling|>" -}}
{%- endif -%}
{%- endset -%}
{%- if message.role == "assistant" -%}
{%- generation -%}{{- rendered -}}{%- endgeneration -%}
{%- else -%}
{{- rendered -}}
{%- endif -%}
{%- endfor -%}

{%- if not state.effort_emitted -%}
{{- emit_thinking_effort() -}}
{%- endif -%}
Loading