{"record":{"id":"79afdc0d8819e6ce","repo":"keras-team/keras","slug":"method-compute-output-shape-of-layer-self-c","errorCode":null,"errorMessage":"Method `compute_output_shape()` of layer {self.__class__.__name__} is returning a type that cannot be interpreted as a shape. It should return a shape tuple. Received: {output_shape}","messagePattern":"Method `compute_output_shape\\(\\)` of layer (.+?) is returning a type that cannot be interpreted as a shape\\. It should return a shape tuple\\. Received: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/layer.py","lineNumber":1249,"sourceCode":"            shapes_dict = update_shapes_dict_for_target_fn(\n                self.compute_output_shape,\n                shapes_dict=shapes_dict,\n                call_spec=call_spec,\n                class_name=self.__class__.__name__,\n            )\n            output_shape = self.compute_output_shape(**shapes_dict)\n\n            if (\n                isinstance(output_shape, list)\n                and output_shape\n                and isinstance(output_shape[0], (int, type(None)))\n            ):\n                output_shape = tuple(output_shape)\n            if not isinstance(output_shape, (list, tuple, dict)):\n                try:\n                    output_shape = tuple(output_shape)\n                except:\n                    raise ValueError(\n                        \"Method `compute_output_shape()` of layer \"\n                        f\"{self.__class__.__name__} is returning \"\n                        \"a type that cannot be interpreted as a shape. \"\n                        \"It should return a shape tuple. \"\n                        f\"Received: {output_shape}\"\n                    )\n            if (\n                isinstance(output_shape, tuple)\n                and output_shape\n                and isinstance(output_shape[0], (int, type(None)))\n            ):\n                return KerasTensor(output_shape, dtype=self.compute_dtype)\n            # Case: nested. Could be a tuple/list of shapes, or a dict of\n            # shapes. Could be deeply nested.\n            return tree.map_shape_structure(\n                lambda s: KerasTensor(s, dtype=self.compute_dtype), output_shape\n            )\n","sourceCodeStart":1231,"sourceCodeEnd":1267,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/layer.py#L1231-L1267","documentation":"compute_output_spec() falls back to the layer's compute_output_shape(); whatever that returns must be convertible to a shape tuple/list/dict. If the returned object cannot be coerced with tuple(), Keras raises this error showing the offending value.","triggerScenarios":"A custom layer's compute_output_shape() returns a TensorShape in an odd backend, a tensor, a string, or None; returning a scalar/int instead of a tuple; returning a shape object from another library.","commonSituations":"Writing custom layers with dynamic output shapes; mixing TF TensorShape with Keras 3 multi-backend code; returning shape logic that degenerates to a non-iterable.","solutions":["Make compute_output_shape() return a plain tuple of ints (or list/dict for multi-output)","If returning dynamic shapes, ensure the object is tuple()-convertible (e.g. tuple(tensor_shape)","Test compute_output_spec directly with keras.ops.is_keras_tensor inputs"],"exampleFix":"# before\ndef compute_output_shape(self, input_shape):\n    return input_shape  # may be a single int\n# after\ndef compute_output_shape(self, input_shape):\n    return tuple(input_shape)[:-1] + (self.units,)","handlingStrategy":"validation","validationCode":"out = layer.compute_output_shape(input_shape)\nassert isinstance(out, (list, tuple, dict)) or tuple(out), out","typeGuard":"def is_valid_shape(s):\n    try:\n        tuple(s)\n        return True\n    except Exception:\n        return False","tryCatchPattern":null,"preventionTips":["Always return plain tuples of ints from compute_output_shape","Unit-test compute_output_shape alongside custom layers"],"tags":["keras","custom-layers","output-shape"],"backgroundTag":"invalid-return-shape","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}