latent-variable commited on
Commit
6710fb6
·
verified ·
1 Parent(s): 60c2d3e

fix chat template to avoid empty historical `<think>` blocks

Browse files

This fixes a chat template issue where historical assistant turns can emit empty `<think>...</think>` blocks even when `reasoning_content` is empty.

That matters because these empty historical `<think>` blocks change the serialized prompt without adding any useful information.

Why this is important:
- it reduces unnecessary prompt drift
- it improves prefix-cache reuse
- it helps avoid avoidable cache misses
- it reduces extra token processing caused by equivalent histories rendering differently

In practice, this means less wasted compute and better cache stability, especially in longer multi-turn or tool-using conversations.

The change is intentionally minimal:
- keep the historical `<think>` wrapper when `reasoning_content` is actually present
- do not emit an empty `<think>` block when there is no reasoning content

Without this guard, the template can produce prior turns like:

assistant
<think>

</think>

<tool_call>...

instead of rendering just the assistant content or tool call directly.

So this change preserves real reasoning content while avoiding empty reasoning scaffolding that can hurt caching behavior.

Files changed (1) hide show
  1. chat_template.jinja +1 -1
chat_template.jinja CHANGED
@@ -97,7 +97,7 @@
97
  {%- endif %}
98
  {%- endif %}
99
  {%- set reasoning_content = reasoning_content|trim %}
100
- {%- if loop.index0 > ns.last_query_index %}
101
  {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
102
  {%- else %}
103
  {{- '<|im_start|>' + message.role + '\n' + content }}
 
97
  {%- endif %}
98
  {%- endif %}
99
  {%- set reasoning_content = reasoning_content|trim %}
100
+ {%- if loop.index0 > ns.last_query_index and reasoning_content %}
101
  {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
102
  {%- else %}
103
  {{- '<|im_start|>' + message.role + '\n' + content }}