keras-team/keras · error · ValueError

When `output_mode="int"`, `dtype` should be an integer type,

Error message

When `output_mode="int"`, `dtype` should be an integer type, 'int32' or 'in64'. Received: dtype={kwargs['dtype']}

What it means

When output_mode='int', Hashing emits bucket indices, so its dtype policy must be int32 or int64. A float dtype (e.g. the backend default floatx) makes index output meaningless and is rejected.

Source

Thrown at keras/src/layers/preprocessing/hashing.py:169

        # By default, output int32 when output_mode='int' and floats otherwise.
        if "dtype" not in kwargs or kwargs["dtype"] is None:
            kwargs["dtype"] = (
                "int64" if output_mode == "int" else backend.floatx()
            )

        super().__init__(**kwargs)

        if num_bins is None or num_bins <= 0:
            raise ValueError(
                "The `num_bins` for `Hashing` cannot be `None` or "
                f"non-positive values. Received: num_bins={num_bins}."
            )

        if output_mode == "int" and (
            self.dtype_policy.name not in ("int32", "int64")
        ):
            raise ValueError(
                'When `output_mode="int"`, `dtype` should be an integer '
                f"type, 'int32' or 'in64'. Received: dtype={kwargs['dtype']}"
            )

        # 'output_mode' must be one of (INT, ONE_HOT, MULTI_HOT, COUNT)
        accepted_output_modes = ("int", "one_hot", "multi_hot", "count")
        if output_mode not in accepted_output_modes:
            raise ValueError(
                "Invalid value for argument `output_mode`. "
                f"Expected one of {accepted_output_modes}. "
                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"`, or `"count"`. '
                f"Received: sparse={sparse} and "

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass dtype='int64' (or 'int32') when output_mode='int'
  2. Simplest: omit dtype — the layer defaults to int64 for output_mode='int'
  3. If you wanted float output, switch output_mode to 'one_hot'/'multi_hot'/'count' instead

Example fix

// before
layer = Hashing(num_bins=1000, output_mode="int", dtype="float32")
// after
layer = Hashing(num_bins=1000, output_mode="int", dtype="int64")
# or simply omit dtype
Defensive patterns

Strategy: validation

Validate before calling

assert kwargs.get("dtype", None) in (None, "int32", "int64") or output_mode != "int"

Type guard

def valid_int_dtype(d):
    return d in (None, "int32", "int64")

Try / catch

catch ValueError from Hashing.__init__ and pass dtype='int64' (or drop dtype) when constructing again

Prevention

When it happens

Trigger: Hashing(..., output_mode='int', dtype='float32') (or any dtype whose policy name is not int32/int64) at construction.

Common situations: Explicitly passing dtype='float32' (or inheriting a global float dtype policy) while leaving output_mode='int'; refactoring code from one_hot/count back to int without updating dtype.

Related errors


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