keras-team/keras · error · ValueError

Layer " " expects input(s), but it received input tensors…

Error message

Layer "{layer_name}" expects {len(input_spec)} input(s), but it received {len(inputs)} input tensors. Inputs received: {inputs}

What it means

Generic count guard in assert_input_compatibility: after flattening, the number of input tensors does not equal the number of InputSpecs, and the specs are not all named (so the friendlier dict-specific message does not apply). The received inputs are echoed for diagnosis.

Solutions

  1. Pass exactly as many tensors as the layer (or functional model) has inputs, in order
  2. Nest tensors in lists/tuples matching the input structure so tree.flatten yields the right count
  3. Pass a dict keyed by input names when names are available
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/layers/input_spec.py:172 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at keras/src/layers/input_spec.py:172

                        f"Expected the following keys: {names}"
                    )
                list_inputs.append(inputs[name])
            inputs = list_inputs

    inputs = tree.flatten(inputs)
    if len(inputs) != len(input_spec):
        # Provide appropriate error message for dict inputs.
        spec_names = [spec.name for spec in input_spec if spec is not None]
        if len(spec_names) == len(input_spec) and all(spec_names):
            raise ValueError(
                f'Layer "{layer_name}" expects {len(input_spec)} named '
                f"input(s) with keys {spec_names}, but it received "
                f"{len(inputs)} input tensors. Pass inputs as a dict, e.g. "
                "`layer({"
                + ", ".join(f"'{n}': ..." for n in spec_names)
                + "})`."
            )
        raise ValueError(
            f'Layer "{layer_name}" expects {len(input_spec)} input(s),'
            f" but it received {len(inputs)} input tensors. "
            f"Inputs received: {inputs}"
        )
    for input_index, (x, spec) in enumerate(zip(inputs, input_spec)):
        if spec is None:
            continue
        if x is None and spec.optional:
            continue

        # Having a shape/dtype is the only commonality of the various
        # tensor-like objects that may be passed. The most common kind of
        # invalid type we are guarding for is a Layer instance (Functional API),
        # which does not have a `shape` attribute.
        if not hasattr(x, "shape"):
            raise ValueError(
                f"Inputs to a layer should be tensors. Got '{x}' "
                f"(of type {type(x)}) as input for layer '{layer_name}'."

View on GitHub (pinned to 7a34a03db6)