Skip to content

fix(gemma4): current Gemma 4 chat template for E2B/E4B; 400 for non-object tool arguments (#975) - #1039

Draft
zaneni6 wants to merge 3 commits into
mainfrom
fix/gemma4-template-975
Draft

zaneni6 wants to merge 3 commits into
mainfrom
fix/gemma4-template-975

Conversation

@zaneni6

@zaneni6 zaneni6 commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Draft: waiting on ModelScope. The new files are on a new branch, flm_1.0.15, of FastFlowLM/Gemma4-E2B-IT-NPU2 and FastFlowLM/Gemma4-E4B-IT-NPU2 on Hugging Face (uploaded and verified byte for byte). main is unchanged, so released FLM versions are not affected. amd/Gemma4-E2B-IT-NPU2 and amd/Gemma4-E4B-IT-NPU2 on ModelScope need the same flm_1.0.15 branch before this merges; until then --modelscope pulls of these tags fail.

File on flm_1.0.15 E2B E4B
chat_template.jinja oid 9d494178…, 18,454 bytes same file
config.json (only flm_version 0.9.43 → 1.0.15) oid a2222087…, 7,528 bytes oid e504aebc…, 7,229 bytes
tokenizer.json (Google's glitch-token fix) sha256 ab26b2ad…, 32,155,733 bytes sha256 91d9cf86…, 32,156,428 bytes

Fixes #975. Also fixes #1057 for the NPU packages (gemma4-it:e2b/e4b, gemma4e-flash:e2b/e4b); #1108 covers gemma4-it-core.

Problem

gemma4-it:e2b/e4b (and gemma4e-flash:e2b/e4b, which use the same HF packages) ship an older Gemma 4 chat template than Google's current one. Rendered through FLM's minja, the old template turns a null tool argument into note:, / nested:{a:}. It also lacks Google's later fixes:

  • turn closing around tool calls;
  • reasoning kept on assistant messages after the last user message;
  • the image_url / input_audio part names.

Changes

Template (src/model_info.json, 4 records). Pins Google's current Gemma 4 template minus the line that opens <|channel>thought\n after a tool response. 12B ships without that line for the same reason (65240918). The server strips reasoning from history, so a prompt ending in that open channel is never a prefix of the next request. Every agent-loop step would re-prefill the whole conversation, and continue_final_message after a tool result would land inside a thought channel. The 12B template is unchanged.

Tool-call arguments that are not a JSON object (e.g. "oops", "[1]"):

  • Before: Google's template raises on them, so 12B and Nemotron already returned HTTP 500 and wiped the context and prompt cache. With the new template, E2B/E4B would do the same.
  • After: a new shared helper, gemma4_template_messages (used by Gemma4e and Gemma4_12B), returns HTTP 400 naming the call. Empty strings still become {}.

Branch, not main. url, file_url and ms_url of the four tags point at flm_1.0.15 (the same pattern as flm_q4k_high_precision for Qwen3.5). Released FLM versions keep pulling main, whose files still match their pinned hashes, so updating Hugging Face does not break them. src/test/model_extension_gemma4e/model-list.json reads gemma4-it:e2b's records, so it moves to the branch too.

Tokenizer (#1057). The NPU packages shipped Google's pre-fix tokenizer.json (sha256 cc8d3a0c…). The branch carries Google's fixed file for each model (google/gemma-4-E2B-it 4adf1574, google/gemma-4-E4B-it 4ecc2ba6): glitch-token merges are pruned, while the vocabulary and token ids are unchanged, so the weights are unaffected. The model_info.json records are copied from the branch's tree API; every record of the four tags equals the branch.

Existing installs (config.json flm_version 1.0.15, flm_min_version 1.0.15). Without this, an installed package stays Ready and keeps the old template. With it, an older download is Outdated. flm run, serve, bench and pull re-check every file, remove the files that no longer match, and download only those again (template, config.json, tokenizer); the weights are kept. Same pattern as 5c07ada0 (Gemma3 4B family).

gemma4_open_frontend.cpp (extension test frontend): empty-string arguments become {}, as in Gemma4e.

Testing

Upgrade path, from the real Hugging Face branch: copies of existing E2B and E4B installs (old template, old tokenizer, config.json at 0.9.43). flm list shows all four tags outdated (0.9.43 < 1.0.15). flm pull verifies every file, removes and re-downloads only chat_template.jinja, config.json and tokenizer.json from flm_1.0.15, all three hash to the pinned values, and flm list shows all four tags ready.

Tokenizer (#1057), NPU: with the fixed tokenizer, E2B and E4B repeat drawingCodehint (Google's repro), CYCLONEDB and Hello world exactly. The API suite on the full new file set (template, config, tokenizer): e4b 178 / 0 / 44, e2b 179 / 0 / 43, the same as below.

Rebased on main (3077edfd). The 12B empty-thought fix this PR used to carry landed separately as #1053, so it is no longer here.

Host:

  • test_chat_tools passes, with new cases for argument handling. They fail when the helper is broken.
  • test_chat_gemma4_args, test_chat_continue and test_gemma4_12b_thought pass.

NPU (Linux, HRX, fastflow-sdk-v0.1.0.dev202611100004), src/test/openai_api_test/run_all.py. The baseline is the shipped template on the same binary:

Model Shipped template This PR
gemma4-it:e4b 178 pass / 0 fail / 44 skip 178 / 0 / 44 (same outcome per case)
gemma4-it:e2b 179 / 0 / 43 179 / 0 / 43 (same outcome per case)
gemma4-it:12b — 178 / 1 / 43

The one 12B failure, test_tool_calls.function_nested_long_bounded_pattern ("unexpected key 'data'"), also fails on plain origin/main with the same package. It is not from this PR.

NPU tool and reasoning scenarios, on all three models:

  • tool round trip with a null argument, thinking on/off, stream and non-stream;
  • first call;
  • follow-up after the tool loop;
  • tool_choice=required and response_format after a tool result;
  • non-object arguments return 400 with session_cleared:false;
  • no markup leaks into content or reasoning.

On e4b, /v1/messages (Anthropic) and /v1/responses both complete a tool round trip with a null argument, and /v1/responses returns 400 for non-object function_call.arguments.

Known: e2b, thinking off, greedy, tool call with a null argument. The first token after the tool result is a near tie between <|tool_response> (−0.63, a stop token) and The (−0.76), so the reply comes back empty. With the old template's malformed unit: the gap was wider (−0.13 vs −2.07); without the null it answers. This is the model's own close call on the correct prompt, not something this PR changes.

Follow-ups (not in this PR)

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Security scan found no critical or high findings on lines changed by this pull request

View the security scan run for complete reports.

“New” means the finding is located in a changed file on an added or modified line; it is not a base-versus-head findings delta.

…bject tool arguments (#975)

Template: gemma4-it:e2b/e4b and gemma4e-flash:e2b/e4b now pin
chat_template.jinja oid 9d494178d6f050c786d4b6fa70a0e1a24fac4ae6 (18454 bytes):
Google's current Gemma 4 template (null arguments render as null, turn
closing around tool calls, reasoning kept after the last user message,
image_url/input_audio parts) without the line that opens a thought channel
after a tool response. The server drops reasoning from history, so a prompt
ending in "<|channel>thought\n" is never a prefix of the next request and
every agent-loop step would prefill the whole conversation again; 12B
already ships without that line for the same reason. The file has to be
uploaded to FastFlowLM/Gemma4-E2B-IT-NPU2 and Gemma4-E4B-IT-NPU2 before this
merges.

Tool arguments: the template refuses arguments that are not an object or
null. gemma4_template_messages (shared by Gemma4e and Gemma4_12B) now turns
those into HTTP 400 naming the call, not a 500 from minja.

The gemma4-open extension test frontend maps empty-string arguments to {},
as Gemma4e does.

Co-Authored-By: Claude <noreply@anthropic.com>
@zaneni6
zaneni6 force-pushed the fix/gemma4-template-975 branch from 002ec50 to 08876e6 Compare December 6, 2026 04:50
zaneni6 and others added 2 commits December 5, 2026 22:04
…lls refresh the template (#975)

The new chat template only reached fresh pulls: an installed E2B/E4B
package stays Ready while its config.json is current, and keeps the old
template. Their Hugging Face config.json now says flm_version 1.0.15
(E2B git oid a2222087, E4B git oid e504aebc; sizes unchanged), so:
- src/model_info.json: the new config.json oid for gemma4-it:e2b/e4b and
  gemma4e-flash:e2b/e4b, which share the two packages;
- src/model_list.json: flm_min_version 1.0.15 for the same four tags.
An older download is then Outdated: run/serve/pull re-check every file,
remove the template and config.json that no longer match, and download
those two again. The weights are kept.

Until the files are on Hugging Face, fresh pulls of these four tags fail
the hash check.

Co-Authored-By: Claude <noreply@anthropic.com>
…zer fix (#975, #1057)

The new chat template and the 1.0.15 config.json are on a new branch,
flm_1.0.15, of FastFlowLM/Gemma4-E2B-IT-NPU2 and Gemma4-E4B-IT-NPU2, not on
main. Released FLM versions keep pulling main and its hashes keep matching;
this version pulls the branch.

The branch also carries Google's fixed tokenizer.json for each model
(google/gemma-4-E2B-it 4adf1574, google/gemma-4-E4B-it 4ecc2ba6: glitch-token
merges pruned, vocabulary and ids unchanged; #1057). The NPU packages shipped
Google's pre-fix tokenizer (sha256 cc8d3a0c).

- src/model_list.json: url, file_url and ms_url of gemma4-it:e2b/e4b and
  gemma4e-flash:e2b/e4b point at flm_1.0.15;
- src/model_info.json: the tokenizer.json records of those four tags, as
  the branch's tree API lists them. Every record of the four tags now
  equals the branch;
- src/test/model_extension_gemma4e/model-list.json: gemma4-open-it:e2b reads
  gemma4-it:e2b's records, so it points at the branch with
  flm_min_version 1.0.15 too.

Until amd/Gemma4-E2B-IT-NPU2 and amd/Gemma4-E4B-IT-NPU2 on ModelScope have the
same branch, --modelscope pulls of these tags fail.

Co-Authored-By: Claude <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update Gemma tokenizer.json after Google's recent fix Gemma 4 chat template is older than Google's current one

1 participant