keras-team/keras · error · ValueError

Expected as input a list/tuple of 2 tensors. Received input_

Error message

Expected as input a list/tuple of 2 tensors. Received input_shape={input_shape}

What it means

compute_output_shape expects input_shape to be a list/tuple of exactly two shape-tuples, because HashedCrossing crosses exactly two inputs. Anything else (one shape, three shapes, non-tuple entries) fails validation.

Source

Thrown at keras/src/layers/preprocessing/hashed_crossing.py:116

            allowable_strings=("int", "one_hot"),
            caller_name=self.__class__.__name__,
            arg_name="output_mode",
        )

        self.num_bins = num_bins
        self.output_mode = output_mode
        self.sparse = sparse
        self._allow_non_tensor_positional_args = True
        self._convert_input_args = False
        self.supports_jit = False

    def compute_output_shape(self, input_shape):
        if (
            not len(input_shape) == 2
            or not isinstance(input_shape[0], tuple)
            or not isinstance(input_shape[1], tuple)
        ):
            raise ValueError(
                "Expected as input a list/tuple of 2 tensors. "
                f"Received input_shape={input_shape}"
            )
        if input_shape[0][-1] != input_shape[1][-1]:
            raise ValueError(
                "Expected the two input tensors to have identical shapes. "
                f"Received input_shape={input_shape}"
            )

        if not input_shape:
            if self.output_mode == "int":
                return ()
            return (self.num_bins,)
        if self.output_mode == "int":
            return tuple(input_shape[0])

        if self.output_mode == "one_hot" and input_shape[0][-1] != 1:
            return tuple(input_shape[0]) + (self.num_bins,)

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Feed the layer a list/tuple of exactly two tensors: layer([a, b])
  2. When building a Functional model, connect two Input layers to the HashedCrossing layer
  3. To cross more than two features, nest HashedCrossing layers

Example fix

// before
layer = HashedCrossing(num_bins=100)
out = layer(x)  # single tensor
// after
out = layer([a, b])  # exactly two tensors
Defensive patterns

Strategy: validation

Validate before calling

assert isinstance(input_shape, (list, tuple)) and len(input_shape) == 2 and all(isinstance(s, tuple) for s in input_shape)

Type guard

def is_pair_of_shapes(s):
    return isinstance(s, (list, tuple)) and len(s) == 2 and all(isinstance(i, tuple) for i in s)

Try / catch

catch ValueError from layer.compute_output_shape()/build and normalize inputs to [x1, x2] of equal shape

Prevention

When it happens

Trigger: Passing a single tensor instead of a list of two; passing nested lists; Keras Functional model shape inference delivering an unexpected nested structure.

Common situations: Passing a single tensor instead of a list of two; passing nested lists; Keras Functional model shape inference delivering an unexpected nested structure.

Related errors


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