keras-team/keras · error · ValueError
All of the arrays in `x` should have the same length. Found
Error message
All of the arrays in `x` should have the same length. Found a pair with: len(x[0]) = {len(x)}, len(x[?]) = {len(xx)} What it means
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.
Source
Thrown at keras/src/legacy/preprocessing/image.py:556
save_format="png",
subset=None,
ignore_class_split=False,
dtype=None,
):
if data_format is None:
data_format = backend.image_data_format()
if dtype is None:
dtype = backend.floatx()
self.dtype = dtype
if isinstance(x, tuple) or isinstance(x, list):
if not isinstance(x[1], list):
x_misc = [np.asarray(x[1])]
else:
x_misc = [np.asarray(xx) for xx in x[1]]
x = x[0]
for xx in x_misc:
if len(x) != len(xx):
raise ValueError(
"All of the arrays in `x` "
"should have the same length. "
"Found a pair with: "
f"len(x[0]) = {len(x)}, len(x[?]) = {len(xx)}"
)
else:
x_misc = []
if y is not None and len(x) != len(y):
raise ValueError(
"`x` (images tensor) and `y` (labels) "
"should have the same length. "
f"Found: x.shape = {np.asarray(x).shape}, "
f"y.shape = {np.asarray(y).shape}"
)
if sample_weight is not None and len(x) != len(sample_weight):
raise ValueError(
"`x` (images tensor) and `sample_weight` "View on GitHub (pinned to 7a34a03db6)
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
Example fix
# before it = gen.flow((x_imgs, meta), y) # len(meta) != len(x_imgs) # after assert len(x_imgs) == len(meta) == len(y) it = gen.flow((x_imgs, meta), y)
Defensive patterns
Strategy: validation
Validate before calling
n = len(x[0]) assert all(len(a) == n for a in x[1]), [len(a) for a in x[1]]
Type guard
def aligned_inputs(x):
n = len(x[0])
return all(len(a) == n for a in x[1]) Prevention
- Apply the same mask/permutation to every input array and y
- Add a startup assert on first-dimension lengths for multi-input pipelines
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- `x` (images tensor) and `y` (labels) should have the same le
- Architecture configuration does not match {weights_name} var
- Model name "{name}" does not match weights variant "{weights
- DenseNet does not support the `channels_first` image data fo
- The last dimension of `query_shape` and `value_shape` must b
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/5dbdaaec105a1a17.
Report an issue: GitHub.