keras-team/keras · critical · ImportError

Layer IntegerLookup requires TensorFlow. Install it via `pip

Error message

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

What it means

IntegerLookup is implemented on TensorFlow ops, so its __init__ checks tf.available and refuses construction when TensorFlow is not installed. This is a hard dependency, not an optional accelerator.

Source

Thrown at keras/src/layers/preprocessing/integer_lookup.py:354

        self,
        max_tokens=None,
        num_oov_indices=1,
        mask_token=None,
        oov_token=-1,
        vocabulary=None,
        vocabulary_dtype="int64",
        idf_weights=None,
        invert=False,
        output_mode="int",
        sparse=False,
        pad_to_max_tokens=False,
        oov_method="floormod",
        salt=None,
        name=None,
        **kwargs,
    ):
        if not tf.available:
            raise ImportError(
                "Layer IntegerLookup requires TensorFlow. "
                "Install it via `pip install tensorflow`."
            )
        if max_tokens is not None and max_tokens <= 1:
            raise ValueError(
                "If `max_tokens` is set for `IntegerLookup`, it must be "
                f"greater than 1. Received: max_tokens={max_tokens}"
            )
        if num_oov_indices < 0:
            raise ValueError(
                "The value of `num_oov_indices` argument for `IntegerLookup` "
                "must >= 0. Received: num_oov_indices="
                f"{num_oov_indices}"
            )
        if sparse and backend.backend() != "tensorflow":
            raise ValueError(
                "`sparse=True` can only be used with the TensorFlow backend."
            )

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. pip install tensorflow (or tensorflow-cpu for a smaller install)
  2. Use a backend-neutral alternative such as a preprocessing step with keras.ops
  3. Verify with a construction smoke test in CI

Example fix

// before: ImportError with jax backend, no tf
layer = keras.layers.IntegerLookup()
// after
// pip install tensorflow-cpu
layer = keras.layers.IntegerLookup()
Defensive patterns

Strategy: validation

Validate before calling

try:
    import tensorflow  # noqa
    tf_ok = True
except ImportError:
    tf_ok = False
if not tf_ok:
    raise ImportError('IntegerLookup needs tensorflow: pip install tensorflow-cpu')

Prevention

When it happens

Trigger: Constructing keras.layers.IntegerLookup() in a Keras 3 environment (jax, torch, or numpy backend) where the tensorflow package is absent.

Common situations: Running multi-backend Keras without tensorflow installed; slim CI images that install only keras+jax; migrating a TF script to the torch backend.

Related errors


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