{"record":{"id":"1fe4453242a2e4ac","repo":"keras-team/keras","slug":"function-was-called-with-an-invalid-input-structur","errorCode":null,"errorMessage":"Function was called with an invalid input structure. Expected input structure: {self._inputs_struct}\nReceived input structure: {inputs}","messagePattern":"Function was called with an invalid input structure\\. Expected input structure: (.+?)\nReceived input structure: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/function.py","lineNumber":232,"sourceCode":"                    tensor_dict[id(x)] = y\n\n        output_tensors = []\n        for i, x in enumerate(self.outputs):\n            if id(x) not in tensor_dict:\n                path = tree.flatten_with_path(self._outputs_struct)[i][0]\n                path = \".\".join(str(p) for p in path)\n                raise ValueError(\n                    f\"Output with path `{path}` is not connected to `inputs`\"\n                )\n            output_tensors.append(tensor_dict[id(x)])\n\n        return tree.pack_sequence_as(self._outputs_struct, output_tensors)\n\n    def _assert_input_compatibility(self, inputs):\n        try:\n            tree.assert_same_structure(inputs, self._inputs_struct)\n        except ValueError:\n            raise ValueError(\n                \"Function was called with an invalid input structure. \"\n                f\"Expected input structure: {self._inputs_struct}\\n\"\n                f\"Received input structure: {inputs}\"\n            )\n        for x, x_ref in zip(tree.flatten(inputs), self._inputs):\n            if len(x.shape) != len(x_ref.shape):\n                raise ValueError(\n                    f\"{self.__class__.__name__} was passed \"\n                    f\"incompatible inputs. For input '{x_ref.name}', \"\n                    f\"expected shape {x_ref.shape}, but received \"\n                    f\"instead a tensor with shape {x.shape}.\"\n                )\n            for dim, ref_dim in zip(x.shape, x_ref.shape):\n                if ref_dim is not None and dim is not None:\n                    if dim != ref_dim:\n                        raise ValueError(\n                            f\"{self.__class__.__name__} was passed \"\n                            f\"incompatible inputs. For input '{x_ref.name}', \"","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/function.py#L214-L250","documentation":"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.","triggerScenarios":"fn built with inputs=(a, b) but called with fn([a, b]); or dict keys renamed","commonSituations":"Calling a functional model with a list vs tuple, dict with renamed keys, or unbatched tensor where a batch dim was traced","solutions":["Call with the identical nesting used at construction (dict keys, tuple vs list)","Repack your data with tree.pack_sequence_as(fn._inputs_struct, flat_values)","Rebuild the Function with the structure you will actually call it with"],"exampleFix":"# before\nfn([x1, x2])  # fn was built from {'a': x}\n\n# after\nfn({'a': x})","handlingStrategy":"validation","validationCode":"tree.assert_same_structure(x, fn._inputs_struct)","typeGuard":null,"tryCatchPattern":"try:\n    fn(x)\nexcept ValueError as e:\n    if 'invalid input structure' in str(e):\n        x = tree.pack_sequence_as(fn._inputs_struct, tree.flatten(x))\n        fn(x)","preventionTips":["Call the function with the exact nested structure it was built with","Wrap calls with a structure-normalizing helper"],"tags":["keras","ops","function","pytree"],"backgroundTag":"input-structure-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}