keras-team/keras · error · ImportError

Layer Hashing requires TensorFlow. Install it via `pip insta

Error message

Layer Hashing requires TensorFlow. Install it via `pip install tensorflow`.

What it means

The Hashing layer depends on TensorFlow ops, so constructing it raises ImportError when the tensorflow package is unavailable. It does not work with jax or torch backends.

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)

Solutions

  1. pip install tensorflow and run with KERAS_BACKEND=tensorflow
  2. On other backends, hash features in the input pipeline (precompute) or use a different feature spec
  3. 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

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.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/9ff6c146cad21254. Report an issue: GitHub.