{"record":{"id":"cdd19505349033bc","repo":"Lightning-AI/pytorch-lightning","slug":"the-combinedloader-has-len-stateful-loaders-sta","errorCode":null,"errorMessage":"The CombinedLoader has {len(stateful_loaders)} stateful loaders, but found {len(states)} states in the checkpoint. Please make sure you define the same dataloaders that were used when saving the checkpoint.","messagePattern":"The CombinedLoader has (.+?) stateful loaders, but found (.+?) states in the checkpoint\\. Please make sure you define the same dataloaders that were used when saving the checkpoint\\.","errorType":"validation","errorClass":"RuntimeError","httpStatus":null,"severity":"critical","filePath":"src/lightning/pytorch/utilities/combined_loader.py","lineNumber":388,"sourceCode":"        \"\"\"Compute the total length of the datasets according to the current mode.\"\"\"\n        datasets = [getattr(dl, \"dataset\", None) for dl in self.flattened]\n        lengths = [length for ds in datasets if (length := sized_len(ds)) is not None]\n        if not lengths:\n            raise NotImplementedError(\"All datasets are iterable-style datasets.\")\n        fn = _SUPPORTED_MODES[self._mode][\"fn\"]\n        return fn(lengths)\n\n    def _state_dicts(self) -> list[dict[str, Any]]:\n        \"\"\"Returns the list of state dicts for iterables in `self.flattened` that are stateful.\"\"\"\n        return [loader.state_dict() for loader in self.flattened if isinstance(loader, _Stateful)]\n\n    def _load_state_dicts(self, states: list[dict[str, Any]]) -> None:\n        \"\"\"Loads the state dicts for iterables in `self.flattened` that are stateful.\"\"\"\n        if not states:\n            return\n        stateful_loaders = [loader for loader in self.flattened if isinstance(loader, _Stateful)]\n        if len(stateful_loaders) != len(states):\n            raise RuntimeError(\n                f\"The CombinedLoader has {len(stateful_loaders)} stateful loaders, but found {len(states)} states\"\n                \" in the checkpoint. Please make sure you define the same dataloaders that were used when saving\"\n                \" the checkpoint.\"\n            )\n        for loader, state_dict in zip(stateful_loaders, states):\n            loader.load_state_dict(state_dict)\n\n\ndef _shutdown_workers_and_reset_iterator(dataloader: object) -> None:\n    if hasattr(dataloader, \"_iterator\"):\n        if isinstance(dataloader._iterator, _MultiProcessingDataLoaderIter):\n            del dataloader._iterator\n        dataloader._iterator = None\n\n\ndef _get_iterables_lengths(iterables: list[Iterable]) -> list[Union[int, float]]:\n    return [(float(\"inf\") if (length := sized_len(iterable)) is None else length) for iterable in iterables]\n","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/utilities/combined_loader.py#L370-L406","documentation":"When restoring, CombinedLoader._load_state_dicts matches checkpointed states to stateful loaders (those implementing _Stateful/load_state_dict). If the count of stateful loaders differs from the number of states saved in the checkpoint, it raises RuntimeError because loader-to-state pairing is ambiguous.","triggerScenarios":"Resuming from a checkpoint saved with a different number of stateful dataloaders (e.g. 2 IterableDatasets with state before, 1 now), via _load_combined_loader_states during trainer.fit(ckpt_path=...).","commonSituations":"Changing dataloader topology between runs; resuming an old checkpoint after refactoring combined loaders; mixed import paths causing a loader to not register as stateful.","solutions":["Define exactly the same stateful dataloaders (same count) as when the checkpoint was saved","If the run config changed intentionally, start fresh without ckpt_path or save a new checkpoint","Verify you import LightningModule/dataloaders from the same lightning.pytorch namespace as the checkpoint run"],"exampleFix":"# before\n# checkpoint saved with 2 stateful loaders\ncl = CombinedLoader([stateful_dl])  # now only 1\ntrainer.fit(model, ckpt_path=\"old.ckpt\")\n# after\ncl = CombinedLoader([stateful_dl1, stateful_dl2])  # match saved topology\ntrainer.fit(model, ckpt_path=\"old.ckpt\")","handlingStrategy":"validation","validationCode":"from lightning.pytorch.utilitiescombined_loader import _Stateful  # note actual import path\nn_stateful = sum(isinstance(l, _Stateful) for l in cl.flattened)\n# compare n_stateful against len(states) from the checkpoint before loading","typeGuard":null,"tryCatchPattern":"try:\n    cl._load_state_dicts(states)\nexcept RuntimeError as e:\n    if \"stateful loaders\" in str(e):\n        raise SystemExit(\"Dataloader topology changed; cannot resume this checkpoint\") from e\n    raise","preventionTips":["Pin dataloader topology in config alongside checkpoints","Version checkpoints with the loader config that produced them"],"tags":["combined-loader","checkpoint","state-mismatch","resume"],"backgroundTag":"checkpoint-state-mismatch","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}