prince-canuma commited on
Commit
40d43b0
·
verified ·
1 Parent(s): 6fe8c3c

Fix chat template: emit multimodal placeholders in tool messages

Browse files

Mirrors the upstream google/gemma-4-*-it commit fcf2302 - adds emission of
<|image|>, <|audio|>, and <|video|> placeholders for tool-message content
parts so multimodal tool responses are not silently dropped.

Files changed (1) hide show
  1. chat_template.jinja +25 -9
chat_template.jinja CHANGED
@@ -1,9 +1,9 @@
1
- {%- macro format_parameters(properties, required) -%}
2
  {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
3
  {%- set ns = namespace(found_first=false) -%}
4
  {%- for key, value in properties | dictsort -%}
5
  {%- set add_comma = false -%}
6
- {%- if key not in standard_keys -%}
7
  {%- if ns.found_first %},{% endif -%}
8
  {%- set ns.found_first = true -%}
9
  {{ key }}:{
@@ -65,7 +65,7 @@
65
  {%- elif value is mapping -%}
66
  {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
67
  properties:{
68
- {{- format_parameters(value, value['required'] | default([])) -}}
69
  }
70
  {%- endif -%}
71
  {%- if value['required'] -%}
@@ -178,18 +178,21 @@
178
  {#- Handle System/Tool Definitions Block -#}
179
  {%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
180
  {{- '<|turn>system\n' -}}
181
-
182
  {#- Inject Thinking token at the very top of the FIRST system turn -#}
183
  {%- if enable_thinking is defined and enable_thinking -%}
184
  {{- '<|think|>\n' -}}
185
  {%- set ns.prev_message_type = 'think' -%}
186
  {%- endif -%}
187
-
188
  {%- if messages[0]['role'] in ['system', 'developer'] -%}
189
- {{- messages[0]['content'] | trim -}}
 
 
 
 
 
 
190
  {%- set loop_messages = messages[1:] -%}
191
  {%- endif -%}
192
-
193
  {%- if tools -%}
194
  {%- for tool in tools %}
195
  {{- '<|tool>' -}}
@@ -198,7 +201,6 @@
198
  {%- endfor %}
199
  {%- set ns.prev_message_type = 'tool' -%}
200
  {%- endif -%}
201
-
202
  {{- '<turn|>\n' -}}
203
  {%- endif %}
204
 
@@ -293,6 +295,15 @@
293
  {%- endif -%}
294
  {%- endfor -%}
295
  {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
 
 
 
 
 
 
 
 
 
296
  {%- else -%}
297
  {{- format_tool_response_block(ns_tname.name, tool_body) -}}
298
  {%- endif -%}
@@ -302,6 +313,7 @@
302
  {%- endfor -%}
303
  {%- endif -%}
304
 
 
305
  {%- if message['content'] is string -%}
306
  {%- if role == 'model' -%}
307
  {{- strip_thinking(message['content']) -}}
@@ -328,10 +340,14 @@
328
  {%- endif -%}
329
  {%- endfor -%}
330
  {%- endif -%}
 
 
 
 
331
 
332
  {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
333
  {{- '<|tool_response>' -}}
334
- {%- elif not (ns_tr_out.flag and not message.get('content')) -%}
335
  {{- '<turn|>\n' -}}
336
  {%- endif -%}
337
  {%- endif -%}
 
1
+ {%- macro format_parameters(properties, required, filter_keys=false) -%}
2
  {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
3
  {%- set ns = namespace(found_first=false) -%}
4
  {%- for key, value in properties | dictsort -%}
5
  {%- set add_comma = false -%}
6
+ {%- if not filter_keys or key not in standard_keys -%}
7
  {%- if ns.found_first %},{% endif -%}
8
  {%- set ns.found_first = true -%}
9
  {{ key }}:{
 
65
  {%- elif value is mapping -%}
66
  {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
67
  properties:{
68
+ {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
69
  }
70
  {%- endif -%}
71
  {%- if value['required'] -%}
 
178
  {#- Handle System/Tool Definitions Block -#}
179
  {%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
180
  {{- '<|turn>system\n' -}}
 
181
  {#- Inject Thinking token at the very top of the FIRST system turn -#}
182
  {%- if enable_thinking is defined and enable_thinking -%}
183
  {{- '<|think|>\n' -}}
184
  {%- set ns.prev_message_type = 'think' -%}
185
  {%- endif -%}
 
186
  {%- if messages[0]['role'] in ['system', 'developer'] -%}
187
+ {%- if messages[0]['content'] is string -%}
188
+ {{- messages[0]['content'] | trim -}}
189
+ {%- elif messages[0]['content'] is sequence -%}
190
+ {%- for item in messages[0]['content'] -%}
191
+ {{- item['text'] | trim + ' '-}}
192
+ {%- endfor -%}
193
+ {%- endif -%}
194
  {%- set loop_messages = messages[1:] -%}
195
  {%- endif -%}
 
196
  {%- if tools -%}
197
  {%- for tool in tools %}
198
  {{- '<|tool>' -}}
 
201
  {%- endfor %}
202
  {%- set ns.prev_message_type = 'tool' -%}
203
  {%- endif -%}
 
204
  {{- '<turn|>\n' -}}
205
  {%- endif %}
206
 
 
295
  {%- endif -%}
296
  {%- endfor -%}
297
  {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
298
+ {%- for part in tool_body -%}
299
+ {%- if part.get('type') == 'image' -%}
300
+ {{- '<|image|>' -}}
301
+ {%- elif part.get('type') == 'audio' -%}
302
+ {{- '<|audio|>' -}}
303
+ {%- elif part.get('type') == 'video' -%}
304
+ {{- '<|video|>' -}}
305
+ {%- endif -%}
306
+ {%- endfor -%}
307
  {%- else -%}
308
  {{- format_tool_response_block(ns_tname.name, tool_body) -}}
309
  {%- endif -%}
 
313
  {%- endfor -%}
314
  {%- endif -%}
315
 
316
+ {%- set captured_content -%}
317
  {%- if message['content'] is string -%}
318
  {%- if role == 'model' -%}
319
  {{- strip_thinking(message['content']) -}}
 
340
  {%- endif -%}
341
  {%- endfor -%}
342
  {%- endif -%}
343
+ {%- endset -%}
344
+
345
+ {{- captured_content -}}
346
+ {%- set has_content = captured_content | trim | length > 0 -%}
347
 
348
  {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
349
  {{- '<|tool_response>' -}}
350
+ {%- elif not (ns_tr_out.flag and not has_content) -%}
351
  {{- '<turn|>\n' -}}
352
  {%- endif -%}
353
  {%- endif -%}