dimondev commited on
Commit
c080de4
·
verified ·
1 Parent(s): f7f5b7f

fix: chat_template, close model turn across tool results + keep tool-call reasoning

Browse files

### Summary

Two defects in `chat_template.jinja`, both verified with [ChatLint](https://github.com/dimondevceo/ChatLint) against 49 conversation shapes. The patched file is attached / inlined as this PR.

| | Hub `main` | this PR |
| --- | --- | --- |
| score | 260/294 (88%) | **283/294 (96%)** |
| errors | **25** | **2** |

The remaining 2 are the preamble-hoist issue also present on E4B/31B (assistant text emitted after `<tool_response|>`); not addressed here.

### Fix 1: misplaced `endif` (22 `marker_balance` errors)

`ns.prev_non_tool_role` was assigned **outside** the `role != 'tool'` block, so a tool message overwrote it to `'tool'`. The next assistant turn then failed `continue_same_model_turn` and opened a second `<|turn>model` without closing the first:

```
broken: ...<tool_response|><|turn>model\nIt is 18C...
fixed: ...<tool_response|>It is 18C...<turn|>\n<|turn>user\n
```

This is the same class of turn-tag imbalance the Jul 15 fix cleared on `gemma-4-31B-it` / `gemma-4-E4B-it`. Those repos already have the `endif` in the right place; Diffusion Gemma did not get that change.

```diff
{%- endif -%}
- {%- endif -%}
-
{#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#}
{%- set ns.prev_non_tool_role = message['role'] -%}
+ {%- endif -%}
{%- endfor -%}
```

### Fix 2: tool-call reasoning dropped after the next user turn

```diff
- {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or (preserve_thinking and message.get('tool_calls')) -%}
+ {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or message.get('tool_calls') or preserve_thinking -%}
```

With the default `preserve_thinking=false`, an assistant turn that called a tool lost its `reasoning_content` as soon as a later user message existed, blowing ~27% of the cached prefix on every agent step after the first tool call.

### Reproduce

```bash
pip install chatlint
# before
curl -sL https://huggingface.co/google/diffusiongemma-26B-A4B-it/raw/main/chat_template.jinja \
| chatlint check /dev/stdin
# after (this PR's file)
chatlint check chat_template.jinja
```

### Ask

If this lands, consider adding [ChatLint's CI workflow](https://github.com/dimondevceo/ChatLint/blob/main/.github/workflows/chatlint.yml) so a sibling repo cannot drift from the Jul 15 fix again. The original Gemma 4 turn-tag regression survived 96 days; this file shows the same class of bug can reappear on a fork that nobody re-gated.

Files changed (1) hide show
  1. chat_template.jinja +2 -2
chat_template.jinja CHANGED
@@ -237,7 +237,7 @@
237
 
238
  {#- Render reasoning/reasoning_content as thinking channel -#}
239
  {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
240
- {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or (preserve_thinking and message.get('tool_calls')) -%}
241
  {%- if thinking_text and thinking_gate -%}
242
  {{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}}
243
  {%- endif -%}
@@ -372,10 +372,10 @@
372
  {%- elif not (ns_tr_out.flag and not has_content and not next_nt.found) -%}
373
  {{- '<turn|>\n' -}}
374
  {%- endif -%}
375
- {%- endif -%}
376
 
377
  {#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#}
378
  {%- set ns.prev_non_tool_role = message['role'] -%}
 
379
  {%- endfor -%}
380
 
381
  {%- if add_generation_prompt -%}
 
237
 
238
  {#- Render reasoning/reasoning_content as thinking channel -#}
239
  {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
240
+ {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or message.get('tool_calls') or preserve_thinking -%}
241
  {%- if thinking_text and thinking_gate -%}
242
  {{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}}
243
  {%- endif -%}
 
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) -#}
377
  {%- set ns.prev_non_tool_role = message['role'] -%}
378
+ {%- endif -%}
379
  {%- endfor -%}
380
 
381
  {%- if add_generation_prompt -%}