Fix assistant content order before tool calls

#60
by youmuorz - opened

Render an assistant's pre-tool content before its tool_calls and the corresponding tool results in google/gemma-4-E4B-it.

The current template reorders an OpenAI-style message containing both fields as tool_calls → tool_responses → content. It also uses that earlier explanation to close the model turn after the final tool result, preventing continuation in the same turn.

Move the existing content-rendering block before tool calls, remove has_content, and use the existing next-message scan to decide when to close the turn. This preserves this repository's thinking-channel and multimodal handling.

Example model turn, with content="I'll check." and a weather tool returning sunny:

Before: <|tool_call>...<tool_call|><|tool_response>...<tool_response|>I'll check.<turn|>
After:  I'll check.<|tool_call>...<tool_call|><|tool_response>...<tool_response|>

Validation: on B300 with Transformers 5.5.4, rendered this exact repository template using AutoTokenizer.apply_chat_template(..., chat_template=template, tokenize=False, add_generation_prompt=True). The tokenizer was the installed Gemma 4 26B-A4B tokenizer; this is a template-rendering test. With the same message history, the original template fails content ordering and continuation; the patched file passes both, plus the next-user turn boundary. The same checks passed for all 23 standalone official Gemma 4 templates audited in this rollout.

Minimal history used:

messages = [
    {"role": "user", "content": "Weather?"},
    {"role": "assistant", "content": "I'll check.", "tool_calls": [{
        "id": "call_1", "type": "function", "function": {
            "name": "get_weather", "arguments": {"city": "London"}
        }
    }]},
    {"role": "tool", "tool_call_id": "call_1", "content": "sunny"},
]

Related work:

AI assistance (OpenAI Codex) was used for implementation and validation.

Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment