{"record":{"id":"5f81403f7db9fafd","repo":"Lightning-AI/pytorch-lightning","slug":"mismatch-in-flattened-length-len-flattened-an","errorCode":null,"errorMessage":"Mismatch in flattened length ({len(flattened)}) and existing length ({len(self._flattened)})","messagePattern":"Mismatch in flattened length \\((.+?)\\) and existing length \\((.+?)\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/utilities/combined_loader.py","lineNumber":317,"sourceCode":"    def sampler(self) -> Any:\n        \"\"\"Return a collections of samplers extracted from iterables.\"\"\"\n        return _map_and_unflatten(lambda x: getattr(x, \"sampler\", None), self.flattened, self._spec)\n\n    @property\n    def batch_sampler(self) -> Any:\n        \"\"\"Return a collections of batch samplers extracted from iterables.\"\"\"\n        return _map_and_unflatten(lambda x: getattr(x, \"batch_sampler\", None), self.flattened, self._spec)\n\n    @property\n    def flattened(self) -> list[Any]:\n        \"\"\"Return the flat list of iterables.\"\"\"\n        return self._flattened\n\n    @flattened.setter\n    def flattened(self, flattened: list[Any]) -> None:\n        \"\"\"Setter to conveniently update the list of iterables.\"\"\"\n        if len(flattened) != len(self._flattened):\n            raise ValueError(\n                f\"Mismatch in flattened length ({len(flattened)}) and existing length ({len(self._flattened)})\"\n            )\n        # update the iterable collection\n        self._iterables = tree_unflatten(flattened, self._spec)\n        self._flattened = flattened\n\n    @property\n    def limits(self) -> Optional[list[Union[int, float]]]:\n        \"\"\"Optional limits per iterator.\"\"\"\n        return self._limits\n\n    @limits.setter\n    def limits(self, limits: Optional[Union[int, float, list[Union[int, float]]]]) -> None:\n        if isinstance(limits, (int, float)):\n            limits = [limits] * len(self.flattened)\n        elif isinstance(limits, list) and len(limits) != len(self.flattened):\n            raise ValueError(\n                f\"Mismatch in number of limits ({len(limits)}) and number of iterables ({len(self.flattened)})\"","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/utilities/combined_loader.py#L299-L335","documentation":"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.","triggerScenarios":"Assigning combined_loader.flattened = new_list where new_list has a different count of loaders than at construction.","commonSituations":"Swapping in a different number of (wrapped) dataloaders between epochs or tests.","solutions":["Keep the same number of iterables; replace loaders positionally","If you need a different count, construct a new CombinedLoader instead of mutating"],"exampleFix":"# before\ncl.flattened = [dl1, dl2, dl3]  # cl was built from 2 loaders\n# after\ncl.flattened = [dl1_new, dl2_new]  # same count\n# or: cl = CombinedLoader([dl1, dl2, dl3])","handlingStrategy":"validation","validationCode":"assert len(new_flattened) == len(cl.flattened)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat CombinedLoader as immutable; rebuild it when loader count changes"],"tags":["combined-loader","mutation","length-mismatch"],"backgroundTag":"length-mismatch","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}