{"record":{"id":"a8f0c86f2c85ffd7","repo":"Lightning-AI/pytorch-lightning","slug":"mismatch-in-number-of-limits-len-limits-and-n","errorCode":null,"errorMessage":"Mismatch in number of limits ({len(limits)}) and number of iterables ({len(iterables)})","messagePattern":"Mismatch in number of limits \\((.+?)\\) and number of iterables \\((.+?)\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/utilities/combined_loader.py","lineNumber":31,"sourceCode":"# limitations under the License.\nimport contextlib\nfrom collections.abc import Iterable, Iterator\nfrom typing import Any, Callable, Literal, Optional, Union\n\nfrom torch.utils.data.dataloader import _BaseDataLoaderIter, _MultiProcessingDataLoaderIter\nfrom typing_extensions import Self, TypedDict, override\n\nfrom lightning.fabric.utilities.data import sized_len\nfrom lightning.fabric.utilities.types import _Stateful\nfrom lightning.pytorch.utilities._pytree import _map_and_unflatten, _tree_flatten, tree_unflatten\n\n_ITERATOR_RETURN = tuple[Any, int, int]  # batch, batch_idx, dataloader_idx\n\n\nclass _ModeIterator(Iterator[_ITERATOR_RETURN]):\n    def __init__(self, iterables: list[Iterable], limits: Optional[list[Union[int, float]]] = None) -> None:\n        if limits is not None and len(limits) != len(iterables):\n            raise ValueError(f\"Mismatch in number of limits ({len(limits)}) and number of iterables ({len(iterables)})\")\n        self.iterables = iterables\n        self.iterators: list[Iterator] = []\n        self._idx = 0  # what would be batch_idx\n        self.limits = limits\n\n    @override\n    def __next__(self) -> _ITERATOR_RETURN:\n        raise NotImplementedError\n\n    @override\n    def __iter__(self) -> Self:\n        self.iterators = [iter(iterable) for iterable in self.iterables]\n        self._idx = 0\n        return self\n\n    def __len__(self) -> int:\n        raise NotImplementedError\n","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/utilities/combined_loader.py#L13-L49","documentation":"_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.","triggerScenarios":"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.","commonSituations":"Adding/removing a dataloader after computing limits; hardcoding limits that go stale.","solutions":["Pass a single int/float to apply the same limit to all loaders","Recompute the limits list so len(limits) == number of flattened iterables"],"exampleFix":"# before\ncl = CombinedLoader([dl1, dl2, dl3])\ncl.limits = [10, 20]\n# after\ncl = CombinedLoader([dl1, dl2, dl3])\ncl.limits = 10  # or [10, 20, 30]","handlingStrategy":"validation","validationCode":"assert limits is None or not isinstance(limits, list) or len(limits) == len(cl.flattened)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer scalar limits; derive list lengths from len(cl.flattened)"],"tags":["combined-loader","limits","validation"],"backgroundTag":"length-mismatch","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}