keras-team/keras · error · ValueError

The `salt` argument for `Hashing` can only be a tuple of siz

Error message

The `salt` argument for `Hashing` can only be a tuple of size 2 integers, or a single integer. Received: salt={salt}.

What it means

The salt parameter of the Hashing layer seeds the hash function so results are stable across runs. It accepts either a single integer (used for both parts of the SipHash salt) or a tuple/list of exactly two integers. Anything else - strings, floats, wrong-length lists - raises this ValueError in __init__.

Source

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

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

        self.num_bins = num_bins
        self.mask_value = mask_value
        self.strong_hash = True if salt is not None else False
        self.output_mode = output_mode
        self.sparse = sparse
        self.salt = None
        if salt is not None:
            if isinstance(salt, (tuple, list)) and len(salt) == 2:
                self.salt = list(salt)
            elif isinstance(salt, int):
                self.salt = [salt, salt]
            else:
                raise ValueError(
                    "The `salt` argument for `Hashing` can only be a tuple of "
                    "size 2 integers, or a single integer. "
                    f"Received: salt={salt}."
                )
        self._convert_input_args = False
        self._allow_non_tensor_positional_args = True
        self.supports_jit = False

    def compute_output_shape(self, input_shape):
        if self.output_mode == "int":
            return tuple(input_shape)
        # `one_hot`, `multi_hot` and `count` encode the last (sample) axis
        # into a `num_bins`-sized axis.
        if len(input_shape) == 0:
            return (self.num_bins,)
        return tuple(input_shape[:-1]) + (self.num_bins,)

    def call(self, inputs):

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass a single int, e.g. salt=1337, which becomes [1337, 1337].
  2. Or pass a tuple/list of exactly two ints, e.g. salt=(1337, 7331).
  3. If you need a string-derived salt, convert it to an int first (e.g. an integer hash of the string).

Example fix

# before
layer = keras.layers.Hashing(num_bins=64, salt="my_seed")
# after
layer = keras.layers.Hashing(num_bins=64, salt=1337)
Defensive patterns

Strategy: type-guard

Validate before calling

assert salt is None or isinstance(salt, int) or (isinstance(salt, (tuple, list)) and len(salt) == 2 and all(isinstance(s, int) for s in salt)), f"bad salt: {salt!r}"

Type guard

def is_valid_salt(s):
    return s is None or isinstance(s, int) or (isinstance(s, (tuple, list)) and len(s) == 2 and all(isinstance(x, int) for x in s))

Prevention

When it happens

Trigger: Passing salt="foo", salt=0.5, salt=(1,2,3), or salt=[1.0, 2.0] to keras.layers.Hashing().

Common situations: Copying a salt value from another library (e.g. a string seed from TensorFlow hashing or a random seed float); assuming salt works like the seed argument of initializers.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


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