{"record":{"id":"363f3d52f05859c9","repo":"keras-team/keras","slug":"x-images-tensor-and-y-labels-should-have-t","errorCode":null,"errorMessage":"`x` (images tensor) and `y` (labels) should have the same length. Found: x.shape = {np.asarray(x).shape}, y.shape = {np.asarray(y).shape}","messagePattern":"`x` \\(images tensor\\) and `y` \\(labels\\) should have the same length\\. Found: x\\.shape = (.+?), y\\.shape = (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/preprocessing/image.py","lineNumber":566,"sourceCode":"        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` \"\n                \"should have the same length. \"\n                f\"Found: x.shape = {np.asarray(x).shape}, \"\n                f\"sample_weight.shape = {np.asarray(sample_weight).shape}\"\n            )\n        if subset is not None:\n            if subset not in {\"training\", \"validation\"}:\n                raise ValueError(\n                    f\"Invalid subset name: {subset}\"\n                    '; expected \"training\" or \"validation\".'\n                )","sourceCodeStart":548,"sourceCodeEnd":584,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/preprocessing/image.py#L548-L584","documentation":"NumpyArrayIterator.__init__ requires the images tensor x and labels y to have equal first-dimension length; otherwise batches would pair images with wrong labels, and the constructor raises this ValueError immediately.","triggerScenarios":"flow(x, y) with len(x) != len(y); y one-hot encoded from a differently-ordered array, or a split/filter applied to only one of x or y.","commonSituations":"Applying train_test_split to x but not y, dropping corrupt images from x without removing matching labels, dataset resampling done on images only.","solutions":["Assert len(x) == len(y) before calling flow","Filter x and y with the same mask/indices: x, y = x[mask], y[mask]","Shuffle both with one shared permutation before iterating"],"exampleFix":"# before\nit = gen.flow(x_clean, y_original)  # rows dropped from x only\n\n# after\nmask = valid_indices\nit = gen.flow(x_clean, y_original[mask])","handlingStrategy":"validation","validationCode":"assert len(x) == len(y), (len(x), len(y))","typeGuard":"def xy_aligned(x, y): return len(x) == len(y)","tryCatchPattern":null,"preventionTips":["Filter x and y together with one index set","After any resampling or cleaning step, re-assert len equality before training"],"tags":["keras","numpy-array-iterator","shape-mismatch","labels"],"backgroundTag":"data-length-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}