keras-team/keras · error · ValueError

`input_dim` must be a positive integer. Received: input_dim=

Error message

`input_dim` must be a positive integer. Received: input_dim={input_dim} (of type {type(input_dim).__name__}).

What it means

keras.layers.Embedding requires input_dim (vocabulary size) to be a Python int strictly greater than 0; bools are explicitly rejected even though bool subclasses int. Passing a float, numpy integer, 0, a negative number, or True raises this ValueError in __init__.

Source

Thrown at keras/src/layers/core/embedding.py:106

        self,
        input_dim,
        output_dim,
        embeddings_initializer="uniform",
        embeddings_regularizer=None,
        embeddings_constraint=None,
        mask_zero=False,
        weights=None,
        lora_rank=None,
        lora_alpha=None,
        quantization_config=None,
        **kwargs,
    ):
        if (
            not isinstance(input_dim, int)
            or isinstance(input_dim, bool)
            or input_dim <= 0
        ):
            raise ValueError(
                "`input_dim` must be a positive integer. "
                f"Received: input_dim={input_dim} "
                f"(of type {type(input_dim).__name__})."
            )
        if (
            not isinstance(output_dim, int)
            or isinstance(output_dim, bool)
            or output_dim <= 0
        ):
            raise ValueError(
                "`output_dim` must be a positive integer. "
                f"Received: output_dim={output_dim} "
                f"(of type {type(output_dim).__name__})."
            )
        input_length = kwargs.pop("input_length", None)
        if input_length is not None:
            warnings.warn(
                "Argument `input_length` is deprecated. Just remove it."

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass a plain Python int greater than 0, e.g. Embedding(input_dim=10000, output_dim=128)
  2. Convert numpy scalars explicitly with int(...)
  3. Check for an empty vocabulary / off-by-one before constructing (len(vocab), not len(vocab)-1)

Example fix

# before
layer = keras.layers.Embedding(input_dim=np.int64(vocab_size), output_dim=128)
# after
layer = keras.layers.Embedding(input_dim=int(vocab_size), output_dim=128)
Defensive patterns

Strategy: type-guard

Validate before calling

assert isinstance(input_dim, int) and not isinstance(input_dim, bool) and input_dim > 0

Type guard

def is_positive_int(v):
    return isinstance(v, int) and not isinstance(v, bool) and v > 0

Prevention

When it happens

Trigger: Constructing Embedding with input_dim=0, input_dim=-1, input_dim=1000.0, input_dim=np.int64(5000), input_dim=True, or a value taken unconverted from a config/serialization dict.

Common situations: Loading configs from JSON/YAML where numbers deserialize as floats; using numpy scalars from tokenizers; off-by-one vocabulary counts yielding 0; passing True from a flag variable by mistake.

Related errors


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