keras-team/keras · error · ValueError

Function was called with an invalid input structure. Expecte

Error message

Function was called with an invalid input structure. Expected input structure: {self._inputs_struct}
Received input structure: {inputs}

What it means

Keras ops Functions validate that a call's input pytree matches the structure captured at construction. Same flat contents but different nesting (list vs tuple, dict key order/names) fail the assert_same_structure check.

Source

Thrown at keras/src/ops/function.py:232

                    tensor_dict[id(x)] = y

        output_tensors = []
        for i, x in enumerate(self.outputs):
            if id(x) not in tensor_dict:
                path = tree.flatten_with_path(self._outputs_struct)[i][0]
                path = ".".join(str(p) for p in path)
                raise ValueError(
                    f"Output with path `{path}` is not connected to `inputs`"
                )
            output_tensors.append(tensor_dict[id(x)])

        return tree.pack_sequence_as(self._outputs_struct, output_tensors)

    def _assert_input_compatibility(self, inputs):
        try:
            tree.assert_same_structure(inputs, self._inputs_struct)
        except ValueError:
            raise ValueError(
                "Function was called with an invalid input structure. "
                f"Expected input structure: {self._inputs_struct}\n"
                f"Received input structure: {inputs}"
            )
        for x, x_ref in zip(tree.flatten(inputs), self._inputs):
            if len(x.shape) != len(x_ref.shape):
                raise ValueError(
                    f"{self.__class__.__name__} was passed "
                    f"incompatible inputs. For input '{x_ref.name}', "
                    f"expected shape {x_ref.shape}, but received "
                    f"instead a tensor with shape {x.shape}."
                )
            for dim, ref_dim in zip(x.shape, x_ref.shape):
                if ref_dim is not None and dim is not None:
                    if dim != ref_dim:
                        raise ValueError(
                            f"{self.__class__.__name__} was passed "
                            f"incompatible inputs. For input '{x_ref.name}', "

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Call with the identical nesting used at construction (dict keys, tuple vs list)
  2. Repack your data with tree.pack_sequence_as(fn._inputs_struct, flat_values)
  3. Rebuild the Function with the structure you will actually call it with

Example fix

# before
fn([x1, x2])  # fn was built from {'a': x}

# after
fn({'a': x})
Defensive patterns

Strategy: validation

Validate before calling

tree.assert_same_structure(x, fn._inputs_struct)

Try / catch

try:
    fn(x)
except ValueError as e:
    if 'invalid input structure' in str(e):
        x = tree.pack_sequence_as(fn._inputs_struct, tree.flatten(x))
        fn(x)

Prevention

When it happens

Trigger: fn built with inputs=(a, b) but called with fn([a, b]); or dict keys renamed

Common situations: Calling a functional model with a list vs tuple, dict with renamed keys, or unbatched tensor where a batch dim was traced

Related errors


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