transformers - 💡(How to fix) Fix Gemma4: chat_template missing from tokenizer_config.json, requires manual loading from separate file [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#45205Fetched 2026-04-08 02:33:12
View on GitHub
Comments
2
Participants
2
Timeline
2
Reactions
0
Timeline (top)
commented ×2

The Gemma4 models (e.g. google/gemma-4-E2B-it) don't include chat_template in tokenizer_config.json. The chat template is shipped as a separate chat_template.jinja file instead.

This means the standard tokenizer.apply_chat_template() workflow fails out of the box:

from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("google/gemma-4-E2B-it")
tokenizer.apply_chat_template([{"role": "user", "content": "Hello"}], tokenize=False, add_generation_prompt=True)
ValueError: Cannot use chat template functions because tokenizer.chat_template is not set
and no template argument was passed!

Error Message

ValueError: Cannot use chat template functions because tokenizer.chat_template is not set and no template argument was passed!

Root Cause

ValueError: Cannot use chat template functions because tokenizer.chat_template is not set
and no template argument was passed!

Fix Action

Workaround

You have to manually load the Jinja file and set it on the tokenizer:

import os
from huggingface_hub import hf_hub_download

chat_template_path = hf_hub_download("google/gemma-4-E2B-it", "chat_template.jinja")
with open(chat_template_path) as f:
    tokenizer.chat_template = f.read()

Code Example

from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("google/gemma-4-E2B-it")
tokenizer.apply_chat_template([{"role": "user", "content": "Hello"}], tokenize=False, add_generation_prompt=True)

---

ValueError: Cannot use chat template functions because tokenizer.chat_template is not set
and no template argument was passed!

---

import os
from huggingface_hub import hf_hub_download

chat_template_path = hf_hub_download("google/gemma-4-E2B-it", "chat_template.jinja")
with open(chat_template_path) as f:
    tokenizer.chat_template = f.read()
RAW_BUFFERClick to expand / collapse

Description

The Gemma4 models (e.g. google/gemma-4-E2B-it) don't include chat_template in tokenizer_config.json. The chat template is shipped as a separate chat_template.jinja file instead.

This means the standard tokenizer.apply_chat_template() workflow fails out of the box:

from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("google/gemma-4-E2B-it")
tokenizer.apply_chat_template([{"role": "user", "content": "Hello"}], tokenize=False, add_generation_prompt=True)
ValueError: Cannot use chat template functions because tokenizer.chat_template is not set
and no template argument was passed!

Workaround

You have to manually load the Jinja file and set it on the tokenizer:

import os
from huggingface_hub import hf_hub_download

chat_template_path = hf_hub_download("google/gemma-4-E2B-it", "chat_template.jinja")
with open(chat_template_path) as f:
    tokenizer.chat_template = f.read()

Expected behavior

tokenizer.chat_template should be set automatically when loading the tokenizer, either by embedding the template in tokenizer_config.json (like other models do) or by having AutoTokenizer detect and load the .jinja file automatically.

This affects all Gemma4 variants I tested: E2B-it, E4B-it.

Environment

  • transformers 5.5.0
  • Python 3.14
  • macOS

extent analysis

TL;DR

To fix the issue with Gemma4 models, manually load the chat_template.jinja file and set it on the tokenizer before applying the chat template.

Guidance

  • Load the chat_template.jinja file from the Hugging Face hub using hf_hub_download and set it as the chat_template attribute of the tokenizer.
  • Verify that tokenizer.chat_template is set correctly before calling apply_chat_template.
  • Consider updating the tokenizer_config.json to include the chat_template for future models, if possible.
  • Test the workaround with different Gemma4 variants to ensure it works consistently.

Example

import os
from huggingface_hub import hf_hub_download
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("google/gemma-4-E2B-it")
chat_template_path = hf_hub_download("google/gemma-4-E2B-it", "chat_template.jinja")
with open(chat_template_path) as f:
    tokenizer.chat_template = f.read()
tokenizer.apply_chat_template([{"role": "user", "content": "Hello"}], tokenize=False, add_generation_prompt=True)

Notes

This workaround assumes that the chat_template.jinja file is available on the Hugging Face hub for the specific model being used. If the file is not available, an alternative solution will be needed.

Recommendation

Apply the workaround by manually loading the chat_template.jinja file and setting it on the tokenizer, as this is the most straightforward solution given the current implementation of the Gemma4 models.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

tokenizer.chat_template should be set automatically when loading the tokenizer, either by embedding the template in tokenizer_config.json (like other models do) or by having AutoTokenizer detect and load the .jinja file automatically.

This affects all Gemma4 variants I tested: E2B-it, E4B-it.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

transformers - 💡(How to fix) Fix Gemma4: chat_template missing from tokenizer_config.json, requires manual loading from separate file [2 comments, 2 participants]