gemma icon indicating copy to clipboard operation
gemma copied to clipboard

Gemma 4 chat template: with thinking off, no empty thought channel after a tool response

Open fysoul17 opened this issue 3 weeks ago • 1 comments

Summary

With thinking off, the Gemma 4 chat template closes an empty thought channel for the model at the start of a turn, but not when generation continues after a tool response. There the model has to write <|channel>thought\n<channel|> itself. When it does not write it cleanly, reasoning text (often a bare thought) ends up at the start of the visible answer.

Template: chat_template.jinja in google/gemma-4-26B-A4B-it (main as of 2026-11-09, SHA-256 ae53464bf3be25802b3a5b37def7fd89667067d7577049b3b2d74c4d8de4c6d4).

Where

The generation prompt block:

{%- if add_generation_prompt -%}
    {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
        {{- '<|turn>model\n' -}}
        {%- if not enable_thinking -%}
            {{- '<|channel>thought\n<channel|>' -}}
        {%- endif -%}
    {%- elif ns.prev_message_type == 'tool_response' and enable_thinking -%}
        {{- '<|channel>thought\n' -}}
    {%- endif -%}
{%- endif -%}

After a tool response with enable_thinking=False, no branch matches, so nothing is added.

Reproduce

Render with add_generation_prompt=True, enable_thinking=False, one tool (get_weather):

Messages End of the rendered prompt
user ...<turn|>\n<|turn>model\n<|channel>thought\n<channel|>
user, assistant tool call, tool response ...<|"|>}<tool_response|> (no empty thought channel)

Suggested fix

Add one branch, so the prompt after a tool response matches the prompt at the start of a turn:

    {%- elif ns.prev_message_type == 'tool_response' -%}
        {{- '<|channel>thought\n<channel|>' -}}

Every other prompt (turn start, thinking on, earlier turns) renders exactly as before.

What we measured

All with synthetic conversations: multi-turn tool calls, including tool responses of about 35k characters. Model: the FP8 build of this checkpoint (RedHatAI/gemma-4-26B-A4B-it-FP8-dynamic, same template) on vLLM 0.20 with --reasoning-parser gemma4 --tool-call-parser gemma4 --enable-auto-tool-choice.

  • Upstream template: in 110 of 110 generations after a tool response, the model wrote the empty thought channel itself.
  • With the branch added: 0 of 110. The model still made the next tool call in 20 of 20 cases where one was needed.

vLLM's examples/tool_chat_template_gemma4.jinja has the same gap: https://github.com/vllm-project/vllm/issues/66430

fysoul17 avatar Nov 08 '26 23:11 fysoul17

Hi, I'd like to work on this issue. Could you please assign it to me? I'm interested in contributing and will work on implementing a solution. Thanks!

Johanl001 avatar Nov 13 '26 11:11 Johanl001