Lightning-AI/pytorch-lightning · error · ValueError

Mismatch in number of limits ({len(limits)}) and number of i

Error message

Mismatch in number of limits ({len(limits)}) and number of iterables ({len(iterables)})

What it means

_ModeIterator (the internal iterator of CombinedLoader) validates that when an explicit list of limits is given, its length must equal the number of iterables. A mismatch raises ValueError.

Source

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

# limitations under the License.
import contextlib
from collections.abc import Iterable, Iterator
from typing import Any, Callable, Literal, Optional, Union

from torch.utils.data.dataloader import _BaseDataLoaderIter, _MultiProcessingDataLoaderIter
from typing_extensions import Self, TypedDict, override

from lightning.fabric.utilities.data import sized_len
from lightning.fabric.utilities.types import _Stateful
from lightning.pytorch.utilities._pytree import _map_and_unflatten, _tree_flatten, tree_unflatten

_ITERATOR_RETURN = tuple[Any, int, int]  # batch, batch_idx, dataloader_idx


class _ModeIterator(Iterator[_ITERATOR_RETURN]):
    def __init__(self, iterables: list[Iterable], limits: Optional[list[Union[int, float]]] = None) -> None:
        if limits is not None and len(limits) != len(iterables):
            raise ValueError(f"Mismatch in number of limits ({len(limits)}) and number of iterables ({len(iterables)})")
        self.iterables = iterables
        self.iterators: list[Iterator] = []
        self._idx = 0  # what would be batch_idx
        self.limits = limits

    @override
    def __next__(self) -> _ITERATOR_RETURN:
        raise NotImplementedError

    @override
    def __iter__(self) -> Self:
        self.iterators = [iter(iterable) for iterable in self.iterables]
        self._idx = 0
        return self

    def __len__(self) -> int:
        raise NotImplementedError

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Pass a single int/float to apply the same limit to all loaders
  2. Recompute the limits list so len(limits) == number of flattened iterables

Example fix

# before
cl = CombinedLoader([dl1, dl2, dl3])
cl.limits = [10, 20]
# after
cl = CombinedLoader([dl1, dl2, dl3])
cl.limits = 10  # or [10, 20, 30]
Defensive patterns

Strategy: validation

Validate before calling

assert limits is None or not isinstance(limits, list) or len(limits) == len(cl.flattened)

Prevention

When it happens

Trigger: Constructing a CombinedLoader, calling iter() and setting limits with a list whose length differs from the number of dataloaders, e.g. combined_loader.limits = [10, 20] with 3 loaders.

Common situations: Adding/removing a dataloader after computing limits; hardcoding limits that go stale.

Related errors


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