keras-team/keras · error · ValueError

If set, `max_tokens` must be greater than 1. Received: max_t

Error message

If set, `max_tokens` must be greater than 1. Received: max_tokens={max_tokens}

What it means

Raised by IndexLookup's constructor when `max_tokens` is set to a value <= 1. The vocabulary table reserves slots for padding/OOV tokens, so a cap of 1 or 0 would leave a zero-element vocabulary, which is meaningless.

Source

Thrown at keras/src/layers/preprocessing/index_lookup.py:133

        num_oov_indices,
        mask_token,
        oov_token,
        vocabulary_dtype,
        vocabulary=None,
        idf_weights=None,
        invert=False,
        output_mode="int",
        sparse=False,
        pad_to_max_tokens=False,
        oov_method="floormod",
        name=None,
        salt=None,
        **kwargs,
    ):
        # If max_tokens is set, the value must be greater than 1 - otherwise we
        # are creating a 0-element vocab, which doesn't make sense.
        if max_tokens is not None and max_tokens <= 1:
            raise ValueError(
                "If set, `max_tokens` must be greater than 1. "
                f"Received: max_tokens={max_tokens}"
            )

        if pad_to_max_tokens and max_tokens is None:
            raise ValueError(
                "If pad_to_max_tokens is True, must set `max_tokens`. "
                f"Received: max_tokens={max_tokens}"
            )

        if num_oov_indices < 0:
            raise ValueError(
                "`num_oov_indices` must be greater than or equal to 0. "
                f"Received: num_oov_indices={num_oov_indices}"
            )

        argument_validation.validate_string_arg(
            oov_method,

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Set max_tokens=None if you don't need a cap
  2. Use a value > 1 that accounts for OOV and padding indices, e.g. vocab_size + num_oov_indices + 1

Example fix

// before
layer = StringLookup(vocabulary=vocab, max_tokens=1)
// after
layer = StringLookup(vocabulary=vocab, max_tokens=None)
Defensive patterns

Strategy: validation

Validate before calling

assert max_tokens is None or max_tokens > 1, 'max_tokens must be greater than 1 when set'

Type guard

def valid_max_tokens(v):
    return v is None or (isinstance(v, int) and v > 1)

Try / catch

try:
    layer = StringLookup(vocabulary=vocab, max_tokens=mt)
except ValueError:
    mt = None
    layer = StringLookup(vocabulary=vocab, max_tokens=mt)

Prevention

When it happens

Trigger: Calling StringLookup(vocabulary=..., max_tokens=1) or IntegerLookup(max_tokens=0); any IndexLookup subclass (StringLookup, IntegerLookup, CategoryEncoding) with max_tokens <= 1.

Common situations: Computing max_tokens programmatically (e.g. len(set(labels)) minus reserved tokens) and getting 0 or 1; copying a config where max_tokens was meant to be unset (None).

Related errors


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