{"record":{"id":"5dbdaaec105a1a17","repo":"keras-team/keras","slug":"all-of-the-arrays-in-x-should-have-the-same-leng","errorCode":null,"errorMessage":"All of the arrays in `x` should have the same length. Found a pair with: len(x[0]) = {len(x)}, len(x[?]) = {len(xx)}","messagePattern":"All of the arrays in `x` should have the same length\\. Found a pair with: len\\(x\\[0\\]\\) = (.+?), len\\(x\\[\\?\\]\\) = (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/preprocessing/image.py","lineNumber":556,"sourceCode":"        save_format=\"png\",\n        subset=None,\n        ignore_class_split=False,\n        dtype=None,\n    ):\n        if data_format is None:\n            data_format = backend.image_data_format()\n        if dtype is None:\n            dtype = backend.floatx()\n        self.dtype = dtype\n        if isinstance(x, tuple) or isinstance(x, list):\n            if not isinstance(x[1], list):\n                x_misc = [np.asarray(x[1])]\n            else:\n                x_misc = [np.asarray(xx) for xx in x[1]]\n            x = x[0]\n            for xx in x_misc:\n                if len(x) != len(xx):\n                    raise ValueError(\n                        \"All of the arrays in `x` \"\n                        \"should have the same length. \"\n                        \"Found a pair with: \"\n                        f\"len(x[0]) = {len(x)}, len(x[?]) = {len(xx)}\"\n                    )\n        else:\n            x_misc = []\n\n        if y is not None and len(x) != len(y):\n            raise ValueError(\n                \"`x` (images tensor) and `y` (labels) \"\n                \"should have the same length. \"\n                f\"Found: x.shape = {np.asarray(x).shape}, \"\n                f\"y.shape = {np.asarray(y).shape}\"\n            )\n        if sample_weight is not None and len(x) != len(sample_weight):\n            raise ValueError(\n                \"`x` (images tensor) and `sample_weight` \"","sourceCodeStart":538,"sourceCodeEnd":574,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/preprocessing/image.py#L538-L574","documentation":"NumpyArrayIterator.__init__ accepts x as a tuple of (images, [aux arrays...]); every auxiliary array must have the same first-dimension length as the images. A mismatch raises this ValueError because batching would produce misaligned multi-input batches.","triggerScenarios":"flow(x=(x_imgs, meta_array), y=y) where meta_array has fewer or more rows than x_imgs; slicing or filtering one input but not the other after a train/test split.","commonSituations":"Multi-input models (image + tabular metadata) where one array was shuffled, subsampled, or filtered independently; off-by-one errors after dropping NaN rows from only part of the inputs.","solutions":["Assert lengths match before calling: all(len(a) == len(x_imgs) for a in aux)","Re-derive all inputs from the same index/mask so they stay aligned","Shuffle with a shared permutation applied to every array"],"exampleFix":"# before\nit = gen.flow((x_imgs, meta), y)  # len(meta) != len(x_imgs)\n\n# after\nassert len(x_imgs) == len(meta) == len(y)\nit = gen.flow((x_imgs, meta), y)","handlingStrategy":"validation","validationCode":"n = len(x[0])\nassert all(len(a) == n for a in x[1]), [len(a) for a in x[1]]","typeGuard":"def aligned_inputs(x):\n    n = len(x[0])\n    return all(len(a) == n for a in x[1])","tryCatchPattern":null,"preventionTips":["Apply the same mask/permutation to every input array and y","Add a startup assert on first-dimension lengths for multi-input pipelines"],"tags":["keras","numpy-array-iterator","shape-mismatch","multi-input"],"backgroundTag":"data-length-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}