DJLougen commited on
Commit
827f4c3
·
verified ·
1 Parent(s): caf033d

chat_template: robust to any client (Claude Code, OpenCode, Pi, etc.). Merges multi/mid-conversation system messages, drops no-user-query hard-fail, tolerates unknown content shapes, unknown roles, missing tool args, system+vision combos.

Browse files
Files changed (1) hide show
  1. chat_template.jinja +55 -42
chat_template.jinja CHANGED
@@ -5,43 +5,56 @@
5
  {{- content }}
6
  {%- elif content is iterable and content is not mapping %}
7
  {%- for item in content %}
8
- {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
9
- {%- if is_system_content %}
10
- {{- raise_exception('System message cannot contain images.') }}
11
- {%- endif %}
12
- {%- if do_vision_count %}
13
- {%- set image_count.value = image_count.value + 1 %}
14
- {%- endif %}
15
- {%- if add_vision_id %}
16
- {{- 'Picture ' ~ image_count.value ~ ': ' }}
17
- {%- endif %}
18
- {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
19
- {%- elif 'video' in item or item.type == 'video' %}
20
- {%- if is_system_content %}
21
- {{- raise_exception('System message cannot contain videos.') }}
22
- {%- endif %}
23
- {%- if do_vision_count %}
24
- {%- set video_count.value = video_count.value + 1 %}
25
  {%- endif %}
26
- {%- if add_vision_id %}
27
- {{- 'Video ' ~ video_count.value ~ ': ' }}
 
 
 
 
 
 
 
28
  {%- endif %}
29
- {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
30
- {%- elif 'text' in item %}
31
  {{- item.text }}
32
- {%- else %}
33
- {{- raise_exception('Unexpected item type in content.') }}
34
  {%- endif %}
35
  {%- endfor %}
36
  {%- elif content is none or content is undefined %}
37
  {{- '' }}
38
  {%- else %}
39
- {{- raise_exception('Unexpected content type.') }}
40
  {%- endif %}
41
  {%- endmacro %}
42
  {%- if not messages %}
43
  {{- raise_exception('No messages provided.') }}
44
  {%- endif %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  {%- if tools and tools is iterable and tools is not mapping %}
46
  {{- '<|im_start|>system\n' }}
47
  {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
@@ -51,19 +64,18 @@
51
  {%- endfor %}
52
  {{- "\n</tools>" }}
53
  {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
54
- {%- if messages[0].role == 'system' %}
55
- {%- set content = render_content(messages[0].content, false, true)|trim %}
56
- {%- if content %}
57
- {{- '\n\n' + content }}
58
- {%- endif %}
59
  {%- endif %}
60
  {{- '<|im_end|>\n' }}
61
  {%- else %}
62
- {%- if messages[0].role == 'system' %}
63
- {%- set content = render_content(messages[0].content, false, true)|trim %}
64
- {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
65
  {%- endif %}
66
  {%- endif %}
 
 
 
67
  {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
68
  {%- for message in messages[::-1] %}
69
  {%- set index = (messages|length - 1) - loop.index0 %}
@@ -75,20 +87,16 @@
75
  {%- endif %}
76
  {%- endif %}
77
  {%- endfor %}
78
- {%- if ns.multi_step_tool %}
79
- {{- raise_exception('No user query found in messages.') }}
80
- {%- endif %}
81
  {%- for message in messages %}
82
  {%- set content = render_content(message.content, true)|trim %}
83
  {%- if message.role == "system" %}
84
- {%- if not loop.first %}
85
- {{- raise_exception('System message must be at the beginning.') }}
86
- {%- endif %}
87
  {%- elif message.role == "user" %}
88
  {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
89
  {%- elif message.role == "assistant" %}
90
  {%- set reasoning_content = '' %}
91
- {%- if message.reasoning_content is string %}
92
  {%- set reasoning_content = message.reasoning_content %}
93
  {%- else %}
94
  {%- if '</think>' in content %}
@@ -116,7 +124,7 @@
116
  {%- else %}
117
  {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
118
  {%- endif %}
119
- {%- if tool_call.arguments is defined %}
120
  {%- for args_name, args_value in tool_call.arguments|items %}
121
  {{- '<parameter=' + args_name + '>\n' }}
122
  {%- set args_value = args_value | string if args_value is string else args_value | tojson | safe %}
@@ -131,6 +139,8 @@
131
  {%- elif message.role == "tool" %}
132
  {%- if loop.previtem and loop.previtem.role != "tool" %}
133
  {{- '<|im_start|>user' }}
 
 
134
  {%- endif %}
135
  {{- '\n<tool_response>\n' }}
136
  {{- content }}
@@ -141,7 +151,10 @@
141
  {{- '<|im_end|>\n' }}
142
  {%- endif %}
143
  {%- else %}
144
- {{- raise_exception('Unexpected message role.') }}
 
 
 
145
  {%- endif %}
146
  {%- endfor %}
147
  {%- if add_generation_prompt %}
@@ -151,4 +164,4 @@
151
  {%- else %}
152
  {{- '<think>\n' }}
153
  {%- endif %}
154
- {%- endif %}
 
5
  {{- content }}
6
  {%- elif content is iterable and content is not mapping %}
7
  {%- for item in content %}
8
+ {%- if item is mapping and ('image' in item or 'image_url' in item or item.type == 'image') %}
9
+ {%- if not is_system_content %}
10
+ {%- if do_vision_count %}
11
+ {%- set image_count.value = image_count.value + 1 %}
12
+ {%- endif %}
13
+ {%- if add_vision_id %}
14
+ {{- 'Picture ' ~ image_count.value ~ ': ' }}
15
+ {%- endif %}
16
+ {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
 
 
 
 
 
 
 
 
17
  {%- endif %}
18
+ {%- elif item is mapping and ('video' in item or item.type == 'video') %}
19
+ {%- if not is_system_content %}
20
+ {%- if do_vision_count %}
21
+ {%- set video_count.value = video_count.value + 1 %}
22
+ {%- endif %}
23
+ {%- if add_vision_id %}
24
+ {{- 'Video ' ~ video_count.value ~ ': ' }}
25
+ {%- endif %}
26
+ {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
27
  {%- endif %}
28
+ {%- elif item is mapping and 'text' in item %}
 
29
  {{- item.text }}
30
+ {%- elif item is string %}
31
+ {{- item }}
32
  {%- endif %}
33
  {%- endfor %}
34
  {%- elif content is none or content is undefined %}
35
  {{- '' }}
36
  {%- else %}
37
+ {{- content | string }}
38
  {%- endif %}
39
  {%- endmacro %}
40
  {%- if not messages %}
41
  {{- raise_exception('No messages provided.') }}
42
  {%- endif %}
43
+ {#- Collect ALL system messages into a single leading system block. -#}
44
+ {#- Tolerates: multiple system messages, system messages mid-conversation, no system message at all. -#}
45
+ {%- set sys_ns = namespace(content='') %}
46
+ {%- for message in messages %}
47
+ {%- if message.role == 'system' %}
48
+ {%- set part = render_content(message.content, false, true)|trim %}
49
+ {%- if part %}
50
+ {%- if sys_ns.content %}
51
+ {%- set sys_ns.content = sys_ns.content + '\n\n' + part %}
52
+ {%- else %}
53
+ {%- set sys_ns.content = part %}
54
+ {%- endif %}
55
+ {%- endif %}
56
+ {%- endif %}
57
+ {%- endfor %}
58
  {%- if tools and tools is iterable and tools is not mapping %}
59
  {{- '<|im_start|>system\n' }}
60
  {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
 
64
  {%- endfor %}
65
  {{- "\n</tools>" }}
66
  {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
67
+ {%- if sys_ns.content %}
68
+ {{- '\n\n' + sys_ns.content }}
 
 
 
69
  {%- endif %}
70
  {{- '<|im_end|>\n' }}
71
  {%- else %}
72
+ {%- if sys_ns.content %}
73
+ {{- '<|im_start|>system\n' + sys_ns.content + '<|im_end|>\n' }}
 
74
  {%- endif %}
75
  {%- endif %}
76
+ {#- Find the index of the last real user query (not a tool response). -#}
77
+ {#- If none exists, last_query_index stays at messages|length-1 so no assistant turn ever satisfies -#}
78
+ {#- (loop.index0 > last_query_index), meaning all assistant <think> blocks are stripped — safe default. -#}
79
  {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
80
  {%- for message in messages[::-1] %}
81
  {%- set index = (messages|length - 1) - loop.index0 %}
 
87
  {%- endif %}
88
  {%- endif %}
89
  {%- endfor %}
90
+ {#- (Removed the "No user query found" hard-fail — some clients send system+assistant only.) -#}
 
 
91
  {%- for message in messages %}
92
  {%- set content = render_content(message.content, true)|trim %}
93
  {%- if message.role == "system" %}
94
+ {#- already merged into the leading system block above -#}
 
 
95
  {%- elif message.role == "user" %}
96
  {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
97
  {%- elif message.role == "assistant" %}
98
  {%- set reasoning_content = '' %}
99
+ {%- if message.reasoning_content is defined and message.reasoning_content is string %}
100
  {%- set reasoning_content = message.reasoning_content %}
101
  {%- else %}
102
  {%- if '</think>' in content %}
 
124
  {%- else %}
125
  {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
126
  {%- endif %}
127
+ {%- if tool_call.arguments is defined and tool_call.arguments %}
128
  {%- for args_name, args_value in tool_call.arguments|items %}
129
  {{- '<parameter=' + args_name + '>\n' }}
130
  {%- set args_value = args_value | string if args_value is string else args_value | tojson | safe %}
 
139
  {%- elif message.role == "tool" %}
140
  {%- if loop.previtem and loop.previtem.role != "tool" %}
141
  {{- '<|im_start|>user' }}
142
+ {%- elif not loop.previtem %}
143
+ {{- '<|im_start|>user' }}
144
  {%- endif %}
145
  {{- '\n<tool_response>\n' }}
146
  {{- content }}
 
151
  {{- '<|im_end|>\n' }}
152
  {%- endif %}
153
  {%- else %}
154
+ {#- Unknown role (e.g. "developer", "function", custom roles): render as user-style block so context is preserved instead of crashing. -#}
155
+ {%- if content %}
156
+ {{- '<|im_start|>user\n' + content + '<|im_end|>\n' }}
157
+ {%- endif %}
158
  {%- endif %}
159
  {%- endfor %}
160
  {%- if add_generation_prompt %}
 
164
  {%- else %}
165
  {{- '<think>\n' }}
166
  {%- endif %}
167
+ {%- endif %}