Fix missing thinking channel in Gemma 4 chat template when using continue_final_message

#26

When enable_thinking: false is set, Gemma 4's chat template signals "no thinking" by inserting an empty thinking channel (<|channel>thought\n<channel|>) at the start of the model turn. However, this only happens in the add_generation_prompt code path. When using continue_final_message: true for assistant prefilling, the template skips this injection entirely, producing:

<|turn>model
[assistant prefill content]

Instead of the expected:

<|turn>model
<|channel>thought
<channel|>[assistant prefill content]

Without the empty thinking channel, the model is in a bad state and may produce thinking tokens in its response.

Solution
Added a conditional in the message loop to inject the empty thinking channel on the last model turn when thinking is disabled:

{%- if role == 'model' and not enable_thinking | default(false) and loop.last -%}
    {{- '<|channel>thought\n<channel|>' -}}
{%- endif -%}
Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment