{"record":{"id":"178fd47a9fc7e08f","repo":"Lightning-AI/pytorch-lightning","slug":"f-an-invalid-dataloader-was-passed-to-trainer-tr-178fd4","errorCode":null,"errorMessage":"f\"An invalid dataloader was passed to `Trainer.{trainer_fn.value}({prefix}dataloaders=...)`.\" f\" Found {dataloader}.\" f\" Either pass the dataloader to the `.{trainer_fn.value}()` method OR implement\" f\" `def {source.name}(self):` in your LightningModule/LightningDataModule.\"","messagePattern":"f\"An invalid dataloader was passed to `Trainer\\.(.+?)\\((.+?)dataloaders=\\.\\.\\.\\)`\\.\" f\" Found (.+?)\\.\" f\" Either pass the dataloader to the `\\.(.+?)\\(\\)` method OR implement\" f\" `def (.+?)\\(self\\):` in your LightningModule/LightningDataModule\\.\"","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/trainer/connectors/data_connector.py","lineNumber":405,"sourceCode":"    source: _DataLoaderSource,\n    trainer_fn: TrainerFn,\n) -> None:\n    if isinstance(dataloader, DataLoader):\n        # Fast path: `torch.utils.data.DataLoader` is always iterable, calling iter() would be expensive\n        return\n\n    try:\n        iter(dataloader)  # type: ignore[call-overload]\n    except TypeError:\n        # A prefix in the message to disambiguate between the train- and (optional) val dataloader that .fit() accepts\n        prefix = \"train_\" if trainer_fn == TrainerFn.FITTING else \"\"\n        if not source.is_module():\n            raise TypeError(\n                f\"An invalid dataloader was passed to `Trainer.{trainer_fn.value}({prefix}dataloaders=...)`.\"\n                f\" Found {dataloader}.\"\n            )\n        if not is_overridden(source.name, source.instance):\n            raise TypeError(\n                f\"An invalid dataloader was passed to `Trainer.{trainer_fn.value}({prefix}dataloaders=...)`.\"\n                f\" Found {dataloader}.\"\n                f\" Either pass the dataloader to the `.{trainer_fn.value}()` method OR implement\"\n                f\" `def {source.name}(self):` in your LightningModule/LightningDataModule.\"\n            )\n        raise TypeError(\n            f\"An invalid dataloader was returned from `{type(source.instance).__name__}.{source.name}()`.\"\n            f\" Found {dataloader}.\"\n        )\n\n\ndef _worker_check(trainer: \"pl.Trainer\", dataloader: object, name: str) -> None:\n    if not isinstance(dataloader, DataLoader):\n        return\n\n    upper_bound = suggested_max_num_workers(trainer.num_devices)\n    start_method = (\n        dataloader.multiprocessing_context.get_start_method()","sourceCodeStart":387,"sourceCodeEnd":423,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/trainer/connectors/data_connector.py#L387-L423","documentation":"Same iterable check as 558 but for the case where the non-iterable dataloader comes from a hook that IS overridden on the LightningModule or LightningDataModule: the object returned by (train_/val_/test_/predict_)_dataloader is not iterable (e.g., a bare HF dataset or numpy array), and Lightning tells you to fix the method's return value.","triggerScenarios":"def train_dataloader(self): return self.hf_dataset (a datasets.Dataset) or return np.array(...) / return SomeDatasetClass; the method is detected as overridden but its return value fails iter().","commonSituations":"Porting sklearn/HF pipelines into LightningModule hooks; returning a Dataset object where a DataLoader or IterableDataset is required; forgetting to instantiate/wrap.","solutions":["Return a torch DataLoader from the hook: return DataLoader(self.dataset, batch_size=self.bs)","For HF datasets: return ds.to_iterable_dataset() (optionally with .with_format(\"torch\"))","Return an iterable-style object implementing __iter__ (and __len__ where possible)"],"exampleFix":"# before\nclass M(LightningModule):\n    def train_dataloader(self):\n        return self.hf_dataset  # datasets.Dataset, not iterable\n# after\nclass M(LightningModule):\n    def train_dataloader(self):\n        return DataLoader(self.hf_dataset, batch_size=32)\n# or: return self.hf_dataset.to_iterable_dataset()","handlingStrategy":"type-guard","validationCode":"dl = model.train_dataloader()\ntry:\n    iter(dl)\nexcept TypeError:\n    raise TypeError(\"train_dataloader() must return a DataLoader/iterable, wrap the dataset\")","typeGuard":"def hook_returns_iterable(obj) -> bool:\n    try:\n        iter(obj)\n        return True\n    except TypeError:\n        return False","tryCatchPattern":null,"preventionTips":["Return DataLoader(...) from *_dataloader hooks, never raw datasets/arrays","Unit-test each LightningModule's dataloader hooks with iter()"],"tags":["lightning","dataloader","iterable","lightning-module","hook-return"],"backgroundTag":"invalid-dataloader-type","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}