Gemma 3 1B - TypeError: 'NoneType' object is not callable
Author: amemov
Pull request: no
Labels: name: bug

### System Info

I'm trying to run Gemma3 using pipeline, and after updating Transformers to the latest version, making sure my token is set up to work with gated repositories, I still can't run Gemma 3.
Environment:
```
- `transformers` version: 4.50.0.dev0
- Platform: Linux-4.4.0-19041-Microsoft-x86_64-with-glibc2.39
- Python version: 3.12.3
- Huggingface_hub version: 0.29.3
- Safetensors version: 0.5.3
- Accelerate version: not installed
- Accelerate config: not found
- DeepSpeed version: not installed
- PyTorch version (GPU?): 2.6.0+cu124 (False)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using distributed or parallel set-up in script?: <fill in>
```


The error message:
```
Traceback (most recent call last):
  File "MyFolder/Test.py", line 80, in <module>
    output = generator(prompt, do_sample=False)
  File "/MyFolder/env/lib/python3.12/site-packages/transformers/pipelines/text_generation.py", line 287, in __call__
    return super().__call__(text_inputs, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/MyFolder/env/lib/python3.12/site-packages/transformers/pipelines/base.py", line 1371, in __call__
    return self.run_single(inputs, preprocess_params, forward_params, postprocess_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "MyFolder/env/lib/python3.12/site-packages/transformers/pipelines/base.py", line 1377, in run_single
    model_inputs = self.preprocess(inputs, **preprocess_params)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/MyFolder/env/lib/python3.12/site-packages/transformers/pipelines/text_generation.py", line 325, in preprocess
    inputs = self.tokenizer(prefix + prompt_text, return_tensors=self.framework, **tokenizer_kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'apply_chat_template'
```

### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

Code to replicate the error:
```
from transformers import pipeline
msg = """Blah blah = Ok and Foo Foo = Not ok. """
prompt = (
    "Extract the following information from the message below:\n"
    "1. Blah blah\n"
    "2. Foo Foo\n"
    "Message:\n"
    f"{msg}\n\n"
    "Provide your answer in JSON format with keys 'Blah blah', 'Foo Foo'."
)

# Tried providing various lengths - didn't help
output = generator(prompt, do_sample=False)

# Print the generated output
print(output[0]['generated_text'])
```

### Expected behavior

Expected output:
`$ { "Ok", "Not ok" }`

Comments:
which gemma 3 model are you using?
Is the given code the complete code?

`from transformers import pipeline,AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("google/gemma-3-1b-it")
model = AutoModelForCausalLM.from_pretrained("google/gemma-3-1b-it")

msg = """Blah blah = Ok and Foo Foo = Not ok. """
prompt = (
    "Extract the following information from the message below:\n"
    "1. Blah blah\n"
    "2. Foo Foo\n"
    "Message:\n"
    f"{msg}\n\n"
    "Provide your answer in JSON format with keys 'Blah blah', 'Foo Foo'."
)

generator = pipeline("text-generation", model=model,tokenizer=tokenizer)
output = generator(prompt,max_length=200)
generated_text = output[0]['generated_text']
print(generated_text)

import json
start_index = generated_text.find('{')
end_index = generated_text.rfind('}') + 1
if start_index != -1 and end_index != 0:
    json_string = generated_text[start_index:end_index]
    result = json.loads(json_string)
    print(result)
else:
    print("Couldn't find JSON in the generated text")
    print(generated_text)`

This should give output according to your need. Hope it is helpful

Thanks for the help - it works. I thought LLM's output followed the same logic as other Transorfmer models, but I guess it is a little bit different. 

In your original code you have forgotten to give the name of the model. The error you faced raises because the tokenizer wasn't mentioned.

I mean, before that I had this code for Transformer and it worked perfectly fine:
```
msg = """ some text """
classifier = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
questions = ["What is the name of the company?"]
company = self.classifier(question=questions[0], context=msg)
return company 
```
So I assumed it would work as well for Gemma - and example on the Hugging Face website seemed to suggest the same:
```
from transformers import pipeline

pipe = pipeline("text-generation", model="google/gemma-3-1b-it", device="cuda", torch_dtype=torch.bfloat16)

messages = [
    [
        {
            "role": "system",
            "content": [{"type": "text", "text": "You are a helpful assistant."},]
        },
        {
            "role": "user",
            "content": [{"type": "text", "text": "Write a poem on Hugging Face, the company"},]
        },
    ],
]

output = pipe(messages, max_new_tokens=50)

```

Gemma 3 doesn't have a question answering and text classification module. Only a text-generation module which basically just generates the asked text so the code is a bit different compared to older models on huggingface like distilbert or llama

Yes, but my point is that even example with the pipeline doesn't mention that you need a tokenizer explicitly. Just played with it a little more and it works just fine like this:
```
from transformers import AutoTokenizer, pipeline

tokenizer = AutoTokenizer.from_pretrained("google/gemma-3-1b-it")
generator = pipeline("text-generation", model="google/gemma-3-1b-it", tokenizer = tokenizer)
msg = """Blah blah = Ok and Foo Foo = Not ok. """
prompt = (
    "Extract the following information from the message below:\n"
    "1. Blah blah\n"
    "2. Foo Foo\n"
    "Message:\n"
    f"{msg}\n\n"
    "Provide your answer in JSON format with keys 'Blah blah', 'Foo Foo'."
)

# Tried providing various lengths - didn't help
output = generator(prompt, do_sample=False)

# Print the generated output
print(output[0]['generated_text'])
```
Without explicitly mentioning tokenizer, it doesn't work. Either there is a bug, or the documentation is incorrect, or I could be wrong

Hey! This was fixed in the release branch a few hours ago, it was indeed a bug in the way tokenizer is loaded. It should be automatically loaded without requiring you to pass it

Since the bug has been fixed, I think the issue can be closed