keras-team/keras · error · ImportError
Layer Hashing requires TensorFlow. Install it via `pip…
Error message
Layer Hashing requires TensorFlow. Install it via `pip install tensorflow`.
What it means
The Hashing preprocessing layer is implemented on top of TensorFlow ops (SipHash). This ImportError is raised at layer construction when TensorFlow is not importable in the current environment, regardless of which Keras backend is active.
Solutions
- pip install tensorflow and run with KERAS_BACKEND=tensorflow
- On other backends, hash features in the input pipeline (precompute) or use a different feature spec
- Guard feature specs so hashed features are only created when TF is present
Example fix
// before KERAS_BACKEND=jax python train.py # uses layers.Hashing // after pip install tensorflow KERAS_BACKEND=tensorflow python train.py
Defensive patterns
Strategy: fallback
Validate before calling
from keras.src.backend import tensorflow as tf
if not tf.available:
raise RuntimeError("Hashing layer needs TensorFlow installed") Type guard
def can_use_hashing():
from keras.src.backend import tensorflow as tf
return tf.available Try / catch
catch ImportError and either install TensorFlow or precompute hashes in the input pipeline
Prevention
- pip install tensorflow before using the Hashing layer
- On JAX/torch backends, hash features upstream instead of using this layer
When it happens
Trigger: Constructing layers.Hashing(...) in a Keras 3 process without tensorflow installed, or with KERAS_BACKEND=jax/torch.
Common situations: Keras 3 with jax or torch backend using FeatureSpace integer_hashed/string_hashed features or layers.Hashing; slim environments where tensorflow was never installed.
Understand the failure class
Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.
Related errors
- Layer HashedCrossing requires TensorFlow. Install it via…
- Layer IntegerLookup requires TensorFlow. Install it via…
- Cannot do batch_dot on inputs with tf.shapes
- Cannot perform batch_dot over axis 0. If your inputs are…
- Expected the 2 dimensions of the `dilation_rate` argument…
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/9ff6c146cad21254.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/layers/preprocessing/hashing.py:147
Output shape:
An `int32` tensor of shape `(batch_size, ...)`.
Reference:
- [SipHash with salt](https://www.131002.net/siphash/siphash.pdf)
"""
def __init__(
self,
num_bins,
mask_value=None,
salt=None,
output_mode="int",
sparse=False,
**kwargs,
):
if not tf.available:
raise ImportError(
"Layer Hashing requires TensorFlow. "
"Install it via `pip install tensorflow`."
)
# By default, output int32 when output_mode='int' and floats otherwise.
if "dtype" not in kwargs or kwargs["dtype"] is None:
kwargs["dtype"] = (
"int64" if output_mode == "int" else backend.floatx()
)
super().__init__(**kwargs)
if num_bins is None or num_bins <= 0:
raise ValueError(
"The `num_bins` for `Hashing` cannot be `None` or "
f"non-positive values. Received: num_bins={num_bins}."
)
View on GitHub (pinned to 7a34a03db6)