fix(chat_template): emit tool_response multimodal placeholders inside the block

#140
Files changed (1) hide show
  1. chat_template.jinja +18 -22
chat_template.jinja CHANGED
@@ -174,6 +174,23 @@
174
  {%- if not loop.last %},{% endif -%}
175
  {%- endfor -%}
176
  {{- '}' -}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  {%- else -%}
178
  {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
179
  {%- endif -%}
@@ -292,28 +309,7 @@
292
  {%- endfor -%}
293
  {#- Handle content as string or content-parts array -#}
294
  {%- set tool_body = follow.get('content') -%}
295
- {%- if tool_body is string -%}
296
- {{- format_tool_response_block(ns_tname.name, tool_body) -}}
297
- {%- elif tool_body is sequence and tool_body is not string -%}
298
- {%- set ns_txt = namespace(s='') -%}
299
- {%- for part in tool_body -%}
300
- {%- if part.get('type') == 'text' -%}
301
- {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}
302
- {%- endif -%}
303
- {%- endfor -%}
304
- {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
305
- {%- for part in tool_body -%}
306
- {%- if part.get('type') in ['image', 'image_url'] -%}
307
- {{- '<|image|>' -}}
308
- {%- elif part.get('type') in ['audio', 'input_audio'] -%}
309
- {{- '<|audio|>' -}}
310
- {%- elif part.get('type') == 'video' -%}
311
- {{- '<|video|>' -}}
312
- {%- endif -%}
313
- {%- endfor -%}
314
- {%- else -%}
315
- {{- format_tool_response_block(ns_tname.name, tool_body) -}}
316
- {%- endif -%}
317
  {%- set ns_tr_out.flag = true -%}
318
  {%- set ns.prev_message_type = 'tool_response' -%}
319
  {%- endif -%}
 
174
  {%- if not loop.last %},{% endif -%}
175
  {%- endfor -%}
176
  {{- '}' -}}
177
+ {%- elif response is string -%}
178
+ {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
179
+ {%- elif response is sequence -%}
180
+ {%- set ns_tr = namespace(txt='', mm='') -%}
181
+ {%- for part in response -%}
182
+ {%- if part.get('type') == 'text' -%}
183
+ {%- set ns_tr.txt = ns_tr.txt + (part.get('text') | default('')) -%}
184
+ {%- elif part.get('type') in ['image', 'image_url'] -%}
185
+ {%- set ns_tr.mm = ns_tr.mm + '<|image|>' -%}
186
+ {%- elif part.get('type') in ['audio', 'input_audio'] -%}
187
+ {%- set ns_tr.mm = ns_tr.mm + '<|audio|>' -%}
188
+ {%- elif part.get('type') == 'video' -%}
189
+ {%- set ns_tr.mm = ns_tr.mm + '<|video|>' -%}
190
+ {%- endif -%}
191
+ {%- endfor -%}
192
+ {{- 'response:' + tool_name + '{value:' + format_argument(ns_tr.txt, escape_keys=False) + '}' -}}
193
+ {{- ns_tr.mm -}}
194
  {%- else -%}
195
  {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
196
  {%- endif -%}
 
309
  {%- endfor -%}
310
  {#- Handle content as string or content-parts array -#}
311
  {%- set tool_body = follow.get('content') -%}
312
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
  {%- set ns_tr_out.flag = true -%}
314
  {%- set ns.prev_message_type = 'tool_response' -%}
315
  {%- endif -%}