keras-team/keras · error · ValueError

The value of `num_oov_indices` argument for `IntegerLookup`

Error message

The value of `num_oov_indices` argument for `IntegerLookup` must >= 0. Received: num_oov_indices={num_oov_indices}

What it means

num_oov_indices for IntegerLookup must be a non-negative integer; negative values are rejected in __init__ because a negative count of out-of-vocabulary buckets is meaningless.

Source

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

        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."
            )
        if vocabulary_dtype != "int64":
            raise ValueError(
                "Only `vocabulary_dtype='int64'` is supported "
                "at this time. Received: "
                f"vocabulary_dtype={vocabulary_dtype}"
            )
        super().__init__(
            max_tokens=max_tokens,
            num_oov_indices=num_oov_indices,
            mask_token=mask_token,

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Set num_oov_indices to 0 or a positive integer (1 is the common default)
  2. Clamp computed values: max(0, n)
  3. Use 0 only if inputs are guaranteed to contain solely known integers

Example fix

// before
layer = IntegerLookup(num_oov_indices=-1)
// after
layer = IntegerLookup(num_oov_indices=1)
Defensive patterns

Strategy: validation

Validate before calling

assert isinstance(num_oov_indices, int) and num_oov_indices >= 0

Type guard

def valid_oov(n): return isinstance(n, int) and n >= 0

Prevention

When it happens

Trigger: Passing num_oov_indices=-1 (or any negative) to keras.layers.IntegerLookup(); configs generated from arithmetic that underflows, e.g. num_oov_indices = vocab_size - something.

Common situations: Copying StringLookup configs where a sentinel was used; programmatic generation of layer params with buggy math.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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