{"record":{"id":"f434f41a1a9fccc7","repo":"Lightning-AI/pytorch-lightning","slug":"f-an-invalid-dataloader-was-passed-to-trainer-tr","errorCode":null,"errorMessage":"f\"An invalid dataloader was passed to `Trainer.{trainer_fn.value}({prefix}dataloaders=...)`.\" f\" Found {dataloader}.\"","messagePattern":"f\"An invalid dataloader was passed to `Trainer\\.(.+?)\\((.+?)dataloaders=\\.\\.\\.\\)`\\.\" f\" Found (.+?)\\.\"","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/trainer/connectors/data_connector.py","lineNumber":400,"sourceCode":"        return self.model\n\n\ndef _check_dataloader_iterable(\n    dataloader: object,\n    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):","sourceCodeStart":382,"sourceCodeEnd":418,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/trainer/connectors/data_connector.py#L382-L418","documentation":"TypeError from _check_dataloaders_every_n_epochs-adjacent _check_dataloader_iterable during setup_data: iter(dataloader) raised TypeError (object has no __iter__) and the dataloader does not come from an overridden method on the module/datamodule, so Lightning cannot give method-specific advice. The passed object is simply not iterable — not a DataLoader-like or dataset.","triggerScenarios":"Passing a HuggingFace datasets.Dataset (not IterableDataset), a numpy array, a torch Tensor, or a plain object to trainer.fit(model, train_dataloaders=ds); also datasets without __iter__/__len__ protocols.","commonSituations":"Assuming HF datasets or tensors are accepted directly; passing a Dataset class instead of an instantiated DataLoader; wrapping data objects that only support indexing.","solutions":["Wrap in a DataLoader: from torch.utils.data import DataLoader; DataLoader(ds, batch_size=32)","For HF datasets: trainer.fit(model, train_dataloaders=ds.to_iterable_dataset()) or construct a DataLoader over it","Return the dataloader from train_dataloader()/val_dataloader() on the LightningModule/DataModule instead of passing a non-iterable object"],"exampleFix":"# before\nfrom datasets import load_dataset\nds = load_dataset(\"mnist\", split=\"train\")\ntrainer.fit(model, train_dataloaders=ds)  # not iterable\n# after\ntrainer.fit(model, train_dataloaders=DataLoader(ds, batch_size=32))\n# or\ntrainer.fit(model, train_dataloaders=ds.to_iterable_dataset())","handlingStrategy":"type-guard","validationCode":"from torch.utils.data import DataLoader\nif not hasattr(dataloaders, \"__iter__\"):\n    dataloaders = DataLoader(dataloaders, batch_size=32)\ntrainer.fit(model, train_dataloaders=dataloaders)","typeGuard":"def is_iterable_dataloader(dl) -> bool:\n    try:\n        iter(dl)\n        return True\n    except TypeError:\n        return False","tryCatchPattern":"try:\n    trainer.fit(model, train_dataloaders=ds)\nexcept TypeError as e:\n    if \"invalid dataloader\" in str(e):\n        trainer.fit(model, train_dataloaders=DataLoader(ds, batch_size=32))\n    else:\n        raise","preventionTips":["Always hand Trainer a DataLoader or IterableDataset","For HF datasets use .to_iterable_dataset() or wrap in DataLoader","Add a smoke check that iter(dataloader) works before fit"],"tags":["lightning","dataloader","iterable","type-error","fit"],"backgroundTag":"invalid-dataloader-type","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}