Keras 3 vs tf.keras import breakage in CI
TensorFlow 2.16 switched its default tf.keras to Keras 3, which changed imports and behavior. Libraries built for Keras 2 (or that import from removed paths) fail at import time when CI installs the newer stack.
What this error means
An import fails with "Your currently installed version of Keras is Keras 3, but this is not yet supported" or "cannot import name ... from keras", after a TensorFlow upgrade pulled in Keras 3.
RuntimeError: Your currently installed version of Keras is Keras 3, but this is not yet
supported. Please install the backwards-compatible tf-keras package with
`pip install tf-keras`.Common causes
TensorFlow 2.16+ defaults to Keras 3
Newer TensorFlow bundles Keras 3, which moves APIs and is not a drop-in replacement for the Keras 2 surface many libraries still expect.
A dependency requires the legacy tf.keras
Transformers, TF-Hub, or other libraries detect Keras 3 and demand the backwards-compatible tf-keras package.
How to fix it
Install the backwards-compatible tf-keras
Add tf-keras and point TensorFlow at it so the legacy API is available.
pip install tf-keras
# in the environment for code that needs the legacy API:
export TF_USE_LEGACY_KERAS=1Pin the stack to a compatible version set
Pin TensorFlow and Keras together to a combination your code supports rather than letting CI pull Keras 3.
pip install "tensorflow==2.15.*" "keras<3"How to prevent it
- Pin TensorFlow and Keras as a matched pair in CI.
- Set
TF_USE_LEGACY_KERAS=1when a dependency needs Keras 2. - Test against the Keras major version you actually ship with.