Bug report + proposed fix for chat_template.jinja

#142
Files changed (1) hide show
  1. chat_template.jinja +14 -7
chat_template.jinja CHANGED
@@ -181,7 +181,7 @@
181
  {%- endmacro -%}
182
 
183
  {#- ===== SETUP ===== -#}
184
- {%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None) -%}
185
  {%- set loop_messages = messages -%}
186
  {%- set enable_thinking = enable_thinking | default(false) -%}
187
  {%- set preserve_thinking = preserve_thinking | default(false) -%}
@@ -368,9 +368,14 @@
368
 
369
  {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
370
  {{- '<|tool_response>' -}}
 
371
  {%- elif continues_into_next -%}
 
372
  {%- elif not (ns_tr_out.flag and not has_content and not next_nt.found) -%}
373
  {{- '<turn|>\n' -}}
 
 
 
374
  {%- endif -%}
375
 
376
  {#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#}
@@ -379,12 +384,14 @@
379
  {%- endfor -%}
380
 
381
  {%- if add_generation_prompt -%}
382
- {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
383
- {{- '<|turn>model\n' -}}
384
- {%- if not enable_thinking -%}
 
 
 
 
385
  {{- '<|channel>thought\n<channel|>' -}}
386
  {%- endif -%}
387
- {%- elif ns.prev_message_type == 'tool_response' and enable_thinking -%}
388
- {{- '<|channel>thought\n' -}}
389
  {%- endif -%}
390
- {%- endif -%}
 
181
  {%- endmacro -%}
182
 
183
  {#- ===== SETUP ===== -#}
184
+ {%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None, model_turn_open=false) -%}
185
  {%- set loop_messages = messages -%}
186
  {%- set enable_thinking = enable_thinking | default(false) -%}
187
  {%- set preserve_thinking = preserve_thinking | default(false) -%}
 
368
 
369
  {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
370
  {{- '<|tool_response>' -}}
371
+ {%- set ns.model_turn_open = true -%}
372
  {%- elif continues_into_next -%}
373
+ {%- set ns.model_turn_open = true -%}
374
  {%- elif not (ns_tr_out.flag and not has_content and not next_nt.found) -%}
375
  {{- '<turn|>\n' -}}
376
+ {%- set ns.model_turn_open = false -%}
377
+ {%- else -%}
378
+ {%- set ns.model_turn_open = true -%}
379
  {%- endif -%}
380
 
381
  {#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#}
 
384
  {%- endfor -%}
385
 
386
  {%- if add_generation_prompt -%}
387
+ {%- if ns.prev_message_type != 'tool_call' -%}
388
+ {%- if not ns.model_turn_open -%}
389
+ {{- '<|turn>model\n' -}}
390
+ {%- endif -%}
391
+ {%- if enable_thinking -%}
392
+ {{- '<|channel>thought\n' -}}
393
+ {%- else -%}
394
  {{- '<|channel>thought\n<channel|>' -}}
395
  {%- endif -%}
 
 
396
  {%- endif -%}
397
+ {%- endif -%}