{"record":{"id":"30dfa63592a6fda8","repo":"keras-team/keras","slug":"inputs-have-incompatible-shapes-received-shapes","errorCode":null,"errorMessage":"Inputs have incompatible shapes. Received shapes {shape1} and {shape2}","messagePattern":"Inputs have incompatible shapes\\. Received shapes (.+?) and (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/merging/base_merge.py","lineNumber":93,"sourceCode":"        \"\"\"\n\n        if None in [shape1, shape2]:\n            return None\n        elif len(shape1) < len(shape2):\n            return self._compute_elemwise_op_output_shape(shape2, shape1)\n        elif not shape2:\n            return shape1\n        output_shape = list(shape1[: -len(shape2)])\n        for i, j in zip(shape1[-len(shape2) :], shape2):\n            if i is None or j is None:\n                output_shape.append(None)\n            elif i == 1:\n                output_shape.append(j)\n            elif j == 1:\n                output_shape.append(i)\n            else:\n                if i != j:\n                    raise ValueError(\n                        \"Inputs have incompatible shapes. \"\n                        f\"Received shapes {shape1} and {shape2}\"\n                    )\n                output_shape.append(i)\n        return tuple(output_shape)\n\n    def build(self, input_shape):\n        # Used purely for shape validation.\n        if not isinstance(input_shape[0], (tuple, list)):\n            raise ValueError(\n                \"A merge layer should be called on a list of inputs. \"\n                f\"Received: input_shape={input_shape} (not a list of shapes)\"\n            )\n        if len(input_shape) < 1:\n            raise ValueError(\n                \"A merge layer should be called \"\n                \"on a list of at least 1 input. \"\n                f\"Received {len(input_shape)} inputs. \"","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/merging/base_merge.py#L75-L111","documentation":"Merge layers (Add, Multiply, Average, Maximum, etc.) broadcast their inputs and require non-batch dimensions to be either equal or 1. This error fires during build/compute_output_shape when two inputs disagree on a dimension and neither is 1, so broadcasting cannot resolve them.","triggerScenarios":"Calling Add()([x, y]) where x has shape (None, 32) and y has shape (None, 16); Multiply on feature maps with different channel counts; shape inference on inputs with conflicting dims.","commonSituations":"Feeding embeddings of different dimensions into an Add; forgetting a projection/reshape layer before merging encoder and decoder branches; off-by-one pooling that changes a spatial dim on one branch only.","solutions":["Fix the upstream layers so both branches produce the same non-batch shape","Insert a Dense/Conv projection or Reshape on one branch to align dimensions before the merge","Use 1 for a dimension to exploit broadcasting intentionally","If shapes are dynamic (None), confirm the runtime shapes actually match"],"exampleFix":"# before\nout = layers.Add()([enc, dec])  # (None,64) + (None,128) -> ValueError\n\n# after\ndec = layers.Dense(64)(dec)\nout = layers.Add()([enc, dec])","handlingStrategy":"validation","validationCode":"def broadcastable(s1, s2):\n    return len(s1) == len(s2) and all(a == b or a == 1 or b == 1 or a is None or b is None for a, b in zip(s1, s2))\nassert broadcastable(tuple(x.shape), tuple(y.shape))","typeGuard":"def shapes_broadcastable(s1, s2) -> bool:\n    return len(s1) == len(s2) and all(a == b or a == 1 or b == 1 or a is None or b is None for a, b in zip(s1, s2))","tryCatchPattern":null,"preventionTips":["Assert all branch shapes before merge layers","Add projections or reshapes when fusing branches","Use model.summary() to verify shapes during development"],"tags":["keras","merge","shape-mismatch","broadcasting"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}