{"record":{"id":"155bb0da54db9081","repo":"keras-team/keras","slug":"expected-padding-to-be-a-tuple-of-3-tuples-of-2","errorCode":null,"errorMessage":"Expected `padding` to be a tuple of 3 tuples of 2 integers. Received: padding={padding}","messagePattern":"Expected `padding` to be a tuple of 3 tuples of 2 integers\\. Received: padding=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/backend.py","lineNumber":2041,"sourceCode":"        raise ValueError(f\"Unknown data_format: {data_format}\")\n\n    if data_format == \"channels_first\":\n        pattern = [[0, 0], [0, 0], list(padding[0]), list(padding[1])]\n    else:\n        pattern = [[0, 0], list(padding[0]), list(padding[1]), [0, 0]]\n    return tf.compat.v1.pad(x, pattern)\n\n\n@keras_export(\"keras._legacy.backend.spatial_3d_padding\")\ndef spatial_3d_padding(x, padding=((1, 1), (1, 1), (1, 1)), data_format=None):\n    \"\"\"DEPRECATED.\"\"\"\n    if (\n        len(padding) != 3\n        or len(padding[0]) != 2\n        or len(padding[1]) != 2\n        or len(padding[2]) != 2\n    ):\n        raise ValueError(\n            \"Expected `padding` to be a tuple of 3 tuples of 2 integers. \"\n            f\"Received: padding={padding}\"\n        )\n    if data_format is None:\n        data_format = backend.image_data_format()\n    if data_format not in {\"channels_first\", \"channels_last\"}:\n        raise ValueError(f\"Unknown data_format: {data_format}\")\n\n    if data_format == \"channels_first\":\n        pattern = [\n            [0, 0],\n            [0, 0],\n            [padding[0][0], padding[0][1]],\n            [padding[1][0], padding[1][1]],\n            [padding[2][0], padding[2][1]],\n        ]\n    else:\n        pattern = [","sourceCodeStart":2023,"sourceCodeEnd":2059,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/backend.py#L2023-L2059","documentation":"Keras's legacy 3D spatial padding helper validates that `padding` is a nested structure of exactly 3 pairs, one (before, after) pair per spatial dimension of a 5D tensor (e.g. a 3D conv or 3D pooling op). Passing a flat tuple, a single int, the 2D-style ((1,1),(1,1)), or a ragged structure raises this ValueError before any op runs. The check exists because tf.pad needs an explicit per-dimension pad pattern that this helper builds from the argument.","triggerScenarios":"Calling keras._legacy.backend.spatial_3d_padding(x, padding=...) with a flat tuple like (1,1,1), a 2-pair structure like ((1,1),(1,1)), a single integer, or any nested structure whose outer length is not 3 or whose elements are not length-2.","commonSituations":"Porting old Keras 2 code that mixed up 2D and 3D padding helpers; copying a Conv2D ZeroPadding2D config into a 3D pipeline; passing config-loaded padding flattened by YAML/JSON round-tripping.","solutions":["Pass padding as 3 pairs of ints, e.g. ((1,1),(2,2),(3,3)) — one pair per spatial dim of the 5D tensor","If you meant 2D padding, call the 2D helper (spatial_2d_padding) or use ZeroPadding2D instead","Validate config-file-derived padding shape with a guard before calling the legacy backend"],"exampleFix":"# before\nspatial_3d_padding(x, padding=(1, 1))\n# after\nspatial_3d_padding(x, padding=((1, 1), (1, 1), (1, 1)))","handlingStrategy":"validation","validationCode":"def valid_3d_padding(p):\n    return (isinstance(p, (tuple, list)) and len(p) == 3\n            and all(isinstance(d, (tuple, list)) and len(d) == 2\n                    and all(isinstance(v, int) for v in d) for d in p))\n\npadding = ((1, 1), (2, 2), (2, 2))\nassert valid_3d_padding(padding), f'bad padding: {padding}'","typeGuard":"def is_3d_padding(p) -> bool:\n    return (isinstance(p, tuple) and len(p) == 3\n            and all(isinstance(d, tuple) and len(d) == 2 for d in p))","tryCatchPattern":null,"preventionTips":["Keep per-spatial-dimension padding as explicit pairs in configs; never flatten them","Add schema validation for padding fields when loading YAML/JSON model configs"],"tags":["keras","padding","argument-validation","legacy","conv3d"],"backgroundTag":"invalid-argument-shape","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}