keras-team/keras · error · ValueError

`output_mode` must be `'int'` when `invert` is true. Receive

Error message

`output_mode` must be `'int'` when `invert` is true. Received: output_mode={output_mode}

What it means

With `invert=True` the layer maps indices back to tokens, which only makes sense for dense integer output. The constructor therefore requires `output_mode='int'` when inverting.

Source

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

        if output_mode == "binary":
            output_mode = "multi_hot"
        if output_mode == "tf-idf":
            output_mode = "tf_idf"
        argument_validation.validate_string_arg(
            output_mode,
            allowable_strings=(
                "int",
                "one_hot",
                "multi_hot",
                "count",
                "tf_idf",
            ),
            caller_name=self.__class__.__name__,
            arg_name="output_mode",
        )

        if invert and output_mode != "int":
            raise ValueError(
                "`output_mode` must be `'int'` when `invert` is true. "
                f"Received: output_mode={output_mode}"
            )

        if sparse and output_mode == "int":
            raise ValueError(
                "`sparse` may only be true if `output_mode` is "
                "`'one_hot'`, `'multi_hot'`, `'count'` or `'tf_idf'`. "
                f"Received: sparse={sparse} and "
                f"output_mode={output_mode}"
            )

        if idf_weights is not None and output_mode != "tf_idf":
            raise ValueError(
                "`idf_weights` should only be set if `output_mode` is "
                f"`'tf_idf'`. Received: idf_weights={idf_weights} and "
                f"output_mode={output_mode}"
            )

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Set `output_mode='int'` when `invert=True`.
  2. Or build the inverse mapping from the vocabulary with a separate non-inverted lookup layer.

Example fix

# before
inv = IndexLookup(vocabulary=vocab, invert=True, output_mode='multi_hot')

# after
inv = IndexLookup(vocabulary=vocab, invert=True, output_mode='int')
Defensive patterns

Strategy: validation

Validate before calling

if invert and output_mode != 'int':
    output_mode = 'int'
layer = IndexLookup(invert=invert, output_mode=output_mode)

Prevention

When it happens

Trigger: `IndexLookup(invert=True, output_mode='multi_hot')` (or 'one_hot', 'count', 'tf_idf').

Common situations: Building a decoder layer by copying an encoder's config and flipping `invert`, forgetting the encoder used multi_hot or tf_idf mode.

Related errors


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