Lightning-AI/pytorch-lightning · error · ValueError

Mismatch in flattened length ({len(flattened)}) and existing

Error message

Mismatch in flattened length ({len(flattened)}) and existing length ({len(self._flattened)})

What it means

The CombinedLoader.flattened setter requires the replacement list to have the same length as the existing flattened iterable list; otherwise the tree_unflatten structure would not match and ValueError is raised.

Source

Thrown at src/lightning/pytorch/utilities/combined_loader.py:317

    def sampler(self) -> Any:
        """Return a collections of samplers extracted from iterables."""
        return _map_and_unflatten(lambda x: getattr(x, "sampler", None), self.flattened, self._spec)

    @property
    def batch_sampler(self) -> Any:
        """Return a collections of batch samplers extracted from iterables."""
        return _map_and_unflatten(lambda x: getattr(x, "batch_sampler", None), self.flattened, self._spec)

    @property
    def flattened(self) -> list[Any]:
        """Return the flat list of iterables."""
        return self._flattened

    @flattened.setter
    def flattened(self, flattened: list[Any]) -> None:
        """Setter to conveniently update the list of iterables."""
        if len(flattened) != len(self._flattened):
            raise ValueError(
                f"Mismatch in flattened length ({len(flattened)}) and existing length ({len(self._flattened)})"
            )
        # update the iterable collection
        self._iterables = tree_unflatten(flattened, self._spec)
        self._flattened = flattened

    @property
    def limits(self) -> Optional[list[Union[int, float]]]:
        """Optional limits per iterator."""
        return self._limits

    @limits.setter
    def limits(self, limits: Optional[Union[int, float, list[Union[int, float]]]]) -> None:
        if isinstance(limits, (int, float)):
            limits = [limits] * len(self.flattened)
        elif isinstance(limits, list) and len(limits) != len(self.flattened):
            raise ValueError(
                f"Mismatch in number of limits ({len(limits)}) and number of iterables ({len(self.flattened)})"

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Keep the same number of iterables; replace loaders positionally
  2. If you need a different count, construct a new CombinedLoader instead of mutating

Example fix

# before
cl.flattened = [dl1, dl2, dl3]  # cl was built from 2 loaders
# after
cl.flattened = [dl1_new, dl2_new]  # same count
# or: cl = CombinedLoader([dl1, dl2, dl3])
Defensive patterns

Strategy: validation

Validate before calling

assert len(new_flattened) == len(cl.flattened)

Prevention

When it happens

Trigger: Assigning combined_loader.flattened = new_list where new_list has a different count of loaders than at construction.

Common situations: Swapping in a different number of (wrapped) dataloaders between epochs or tests.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/5f81403f7db9fafd. Report an issue: GitHub.