I've been trying for some months to make something using some pretrained Gemma models, but I keep jumping from error to error, and I haven't even started the actual programming (haven't even gotten to play around with the models). My current issue is that when I try to run the example code from Kaggle, I get the following error:
My Code: (which is really just the example code from Kaggle)
import os
import keras
import keras_hub
import tensorflow as tf
# import kagglehub < this comment here was a band-aid fix for a previous error.
os.environ["KERAS_BACKEND"] = "torch" # Or "tensorflow" or "jax".
os.environ["XLA_PYTHON_CLIENT_MEM_FRACTION"] = "1.00"
model = keras_hub.models.Gemma4CausalLM.from_preset(
"gemma4_instruct_2b",
dtype="bfloat16",
)
The Error (it's on jupyter, so the error is on line 1 of its cell):
AttributeError Traceback (most recent call last)
Cell In[3], line 1
----> 1 model = keras_hub.models.Gemma4CausalLM.from_preset(
2 "gemma4_instruct_2b",
3 dtype="bfloat16",
4 )
AttributeError: module 'keras_hub.api.models' has no attribute 'Gemma4CausalLM'
For this specific snippet and result, I'm on Python 3.14.4 -- keras_hub 0.19.0 -- keras 3.15.1
I also attempted the same snippet on Python3.10.0 -- keras_hub 0.25.6 with the same result
I also tried other solutions I found online, such as switching Gemma4CausalLM to GemmaCausalLM
and replacing "gemma4_instruct_2b" to "kaggle://keras/gemma4/keras/gemma4_instruct_2b"
When I do this, the output is as follows:
Downloading to /home/fern/.cache/kagglehub/models/keras/gemma4/keras/gemma4_instruct_2b/2/config.json...
100%|██████████| 4.50k/4.50k [00:00<00:00, 1.61MB/s]
ValueError Traceback (most recent call last)
Cell In[6], line 1
----> 1 model = keras_hub.models.GemmaCausalLM.from_preset(
2 "kaggle://keras/gemma4/keras/gemma4_instruct_2b",
3 dtype="bfloat16",
4 )
File ~/projects/fernbot2.0/.venv/lib/python3.14/site-packages/keras_hub/src/models/task.py:186, in Task.from_preset(cls, preset, load_weights, **kwargs)
179 raise ValueError(
180 "Do not call `Task.from_preset()` directly. Instead call a "
181 "particular task class, e.g. "
182 "`keras_hub.models.TextClassifier.from_preset()`."
183 )
185 loader = get_preset_loader(preset)
--> 186 backbone_cls = loader.check_backbone_class()
187 # Detect the correct subclass if we need to.
188 if (
189 issubclass(backbone_cls, Backbone)
190 and cls.backbone_cls != backbone_cls
191 ):
File ~/projects/fernbot2.0/.venv/lib/python3.14/site-packages/keras_hub/src/utils/preset_utils.py:642, in KerasPresetLoader.check_backbone_class(self)
641 def check_backbone_class(self):
--> 642 return check_config_class(self.config)
...
489 "classes with `register_keras_serializable()`."
490 )
491 return cls
ValueError: Attempting to load class keras_hub>Gemma4Backbone with `from_preset()`, but there is no class registered with Keras for keras_hub>Gemma4Backbone. Make sure to register any custom classes with `register_keras_serializable()`
I'm guessing there's an issue with my Keras_hub version, and that's why it doesn't recognize Gemma4? But when I try to force upgrade to something like keras_hub 0.28.0 using pip install keras_hub==0.28.0 keras==3.13.0, the terminal says
"ERROR: Could not find a version that satisfies the requirement tensorflow-text; platform_system != "Windows" (from keras-hub) (from versions: none)
ERROR: No matching distribution found for tensorflow-text; platform_system != "Windows"
for context, I am on WSL Ubuntu using virtual environments. I have both versions of python installed on WSL (3.10 and 3.14) and I just use different commands to make the virtual environments (python3.10 -m venv .venv for python 3.10 and python3 -m venv .venv for python 3.14)
so perhaps there is an issue with my tensorflow install? When I ask pip to show me tensorflow, it says this:
Name: tensorflow
Version: 2.22.0rc0
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: packages@tensorflow.org
License: Apache 2.0
Location: /home/fern/projects/fernbot2.0/.venv/lib/python3.14/site-packages
Requires: absl-py, astunparse, flatbuffers, gast, google_pasta, grpcio, h5py, keras-nightly, libclang, ml_dtypes, numpy, opt_einsum, packaging, protobuf, requests, six, termcolor, typing_extensions, wrapt
Required-by: