{"record":{"id":"f0b31847a3a0b224","repo":"keras-team/keras","slug":"patches-has-unexpected-rank-for-3d-if-self-is","errorCode":null,"errorMessage":"`patches` has unexpected rank for {'3D' if self.is_3d else '2D'} reconstruction. Expected {expected_ndim_unbatched} (unbatched) or {expected_ndim_batched} (batched). Received shape: {patches.shape}","messagePattern":"`patches` has unexpected rank for (.+?) reconstruction\\. Expected (.+?) \\(unbatched\\) or (.+?) \\(batched\\)\\. Received shape: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/image.py","lineNumber":995,"sourceCode":"        for s in self.size:\n            patch_volume *= s\n        channels_out = None if flat is None else flat // patch_volume\n\n        if self.is_3d:\n            expected_ndim_batched = 5\n            expected_ndim_unbatched = 4\n            spatial = self.output_size  # (D, H, W)\n        else:\n            expected_ndim_batched = 4\n            expected_ndim_unbatched = 3\n            spatial = self.output_size  # (H, W)\n\n        if original_ndim == expected_ndim_batched:\n            batch = patches_shape[0]\n        elif original_ndim == expected_ndim_unbatched:\n            batch = None\n        else:\n            raise ValueError(\n                f\"`patches` has unexpected rank for \"\n                f\"{'3D' if self.is_3d else '2D'} reconstruction. \"\n                f\"Expected {expected_ndim_unbatched} (unbatched) or \"\n                f\"{expected_ndim_batched} (batched). \"\n                f\"Received shape: {patches.shape}\"\n            )\n\n        if flat is not None and flat % patch_volume != 0:\n            raise ValueError(\n                f\"`patches` last dim ({flat}) is not divisible by \"\n                f\"prod(size) ({patch_volume}).\"\n            )\n\n        if self.data_format == \"channels_last\":\n            grid_offset = 1 if original_ndim == expected_ndim_batched else 0\n            grid = patches_shape[grid_offset : grid_offset + len(self.size)]\n        else:\n            # channels_first: the grid dims are the trailing dims,","sourceCodeStart":977,"sourceCodeEnd":1013,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/image.py#L977-L1013","documentation":"Raised during reconstruct_patches when the patches tensor rank doesn't match either the batched or unbatched layout expected for 2D (3/4-D) or 3D (4/5-D) reconstruction, indicating a malformed or transposed patches array.","triggerScenarios":"Thrown at keras/src/ops/image.py:995 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Provide patches from extract_patches in (batch, grid..., patch_volume) layout","Add/remove the batch dimension to match unbatched or batched calling convention","Verify you're using the correct 2D vs 3D variant for your data"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}