I stumbled on a Gemma 4 chat template bug for tools and fixed it

Posted by EntertainmentBroad43@reddit | LocalLLaMA | View on Reddit | 17 comments

TLDR: tool parameters using the common JSON Schema pattern `anyOf: [$ref, null]` are rendered into the prompt as empty `type` fields. This strips the useful schema information before the model sees it.

--

Long, rambling version:

Gemma 4 was having issues with calling my custom MCP tool on >3 inference engines, while Qwen3.5 and gpt-oss-20b were doing fine.

I guessed it was either a chat template issue or inference library issue on an edge case, and thought time would sort it out, since many people were happy with Gemma 4 as an agent.

It didn't for at least 2 weeks now and I had no choice but to investigate myself.

What I did:

  1. I made a verbose log file via llama-server, running the same prompt/tool on Qwen3.5-27B-Q4_K_M and gemma-4-31B-it-Q4_K_S on a macbook pro.
  2. I asked GPT-5.5-high on codex CLI to read the logs and diagnose the issue.
  3. Found it in couple of minutes; the default Gemma chat template assumes tool parameters have a direct type field. Which means it will not work with JSON schema shapes like nullable refs:

{"anyOf": [{"$ref": "#/$defs/SomeObject"}, {"type": "null"}]}

where there is no top-level type. The useful structure is inside anyOf and $defs. The template drops anyOf, $ref, and $defs, then renders it as type: "".

  1. It was fixed by a small changes in the chat template jinja, and now Gemma is calling my tool perfectly!

Anyway I made a PR on HF, google/gemma-4-31B-it.

Meanwhile, you can use this jinja:
https://pastebin.com/p9z3BAC0