keras-team/keras · error · ValueError

`x` (images tensor) and `y` (labels) should have the same le

Error message

`x` (images tensor) and `y` (labels) should have the same length. Found: x.shape = {np.asarray(x).shape}, y.shape = {np.asarray(y).shape}

What it means

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.

Source

Thrown at keras/src/legacy/preprocessing/image.py:566

        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` "
                "should have the same length. "
                f"Found: x.shape = {np.asarray(x).shape}, "
                f"sample_weight.shape = {np.asarray(sample_weight).shape}"
            )
        if subset is not None:
            if subset not in {"training", "validation"}:
                raise ValueError(
                    f"Invalid subset name: {subset}"
                    '; expected "training" or "validation".'
                )

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Assert len(x) == len(y) before calling flow
  2. Filter x and y with the same mask/indices: x, y = x[mask], y[mask]
  3. Shuffle both with one shared permutation before iterating

Example fix

# before
it = gen.flow(x_clean, y_original)  # rows dropped from x only

# after
mask = valid_indices
it = gen.flow(x_clean, y_original[mask])
Defensive patterns

Strategy: validation

Validate before calling

assert len(x) == len(y), (len(x), len(y))

Type guard

def xy_aligned(x, y): return len(x) == len(y)

Prevention

When it happens

Trigger: 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.

Common situations: Applying train_test_split to x but not y, dropping corrupt images from x without removing matching labels, dataset resampling done on images only.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/363f3d52f05859c9. Report an issue: GitHub.