Lightning-AI/pytorch-lightning · error · TypeError
f"An invalid dataloader was passed to `Trainer.{trainer_fn.v
Error message
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." What it means
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.
Source
Thrown at src/lightning/pytorch/trainer/connectors/data_connector.py:405
source: _DataLoaderSource,
trainer_fn: TrainerFn,
) -> None:
if isinstance(dataloader, DataLoader):
# Fast path: `torch.utils.data.DataLoader` is always iterable, calling iter() would be expensive
return
try:
iter(dataloader) # type: ignore[call-overload]
except TypeError:
# A prefix in the message to disambiguate between the train- and (optional) val dataloader that .fit() accepts
prefix = "train_" if trainer_fn == TrainerFn.FITTING else ""
if not source.is_module():
raise TypeError(
f"An invalid dataloader was passed to `Trainer.{trainer_fn.value}({prefix}dataloaders=...)`."
f" Found {dataloader}."
)
if not is_overridden(source.name, source.instance):
raise TypeError(
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."
)
raise TypeError(
f"An invalid dataloader was returned from `{type(source.instance).__name__}.{source.name}()`."
f" Found {dataloader}."
)
def _worker_check(trainer: "pl.Trainer", dataloader: object, name: str) -> None:
if not isinstance(dataloader, DataLoader):
return
upper_bound = suggested_max_num_workers(trainer.num_devices)
start_method = (
dataloader.multiprocessing_context.get_start_method()View on GitHub (pinned to 9fed5c27d2)
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)
Example fix
# before
class M(LightningModule):
def train_dataloader(self):
return self.hf_dataset # datasets.Dataset, not iterable
# after
class M(LightningModule):
def train_dataloader(self):
return DataLoader(self.hf_dataset, batch_size=32)
# or: return self.hf_dataset.to_iterable_dataset() Defensive patterns
Strategy: type-guard
Validate before calling
dl = model.train_dataloader()
try:
iter(dl)
except TypeError:
raise TypeError("train_dataloader() must return a DataLoader/iterable, wrap the dataset") Type guard
def hook_returns_iterable(obj) -> bool:
try:
iter(obj)
return True
except TypeError:
return False Prevention
- Return DataLoader(...) from *_dataloader hooks, never raw datasets/arrays
- Unit-test each LightningModule's dataloader hooks with iter()
When it happens
Trigger: 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().
Common situations: Porting sklearn/HF pipelines into LightningModule hooks; returning a Dataset object where a DataLoader or IterableDataset is required; forgetting to instantiate/wrap.
Related errors
- f"An invalid dataloader was passed to `Trainer.{trainer_fn.v
- You seem to have configured a sampler in your DataLoader whi
- `val_dataloader` must be implemented to be used with the Lig
- `predict_dataloader` must be implemented to be used with the
- Couldn't infer the batch indices fetched from your dataloade
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/178fd47a9fc7e08f.
Report an issue: GitHub.