Lightning-AI/pytorch-lightning · error · MisconfigurationException
`train_dataloader` must be implemented to be used with the L
Error message
`train_dataloader` must be implemented to be used with the Lightning Trainer
What it means
The LightningModule/LightningDataModule base implementation of train_dataloader is a stub that raises MisconfigurationException. It exists so that calling fit() without the user overriding train_dataloader fails loudly instead of silently returning None.
Source
Thrown at src/lightning/pytorch/core/hooks.py:483
For data processing use the following pattern:
- download in :meth:`prepare_data`
- process and split in :meth:`setup`
However, the above are only necessary for distributed processing.
.. warning:: do not assign state in prepare_data
- :meth:`~lightning.pytorch.trainer.trainer.Trainer.fit`
- :meth:`prepare_data`
- :meth:`setup`
Note:
Lightning tries to add the correct sampler for distributed and arbitrary hardware.
There is no need to set it yourself.
"""
raise MisconfigurationException("`train_dataloader` must be implemented to be used with the Lightning Trainer")
def test_dataloader(self) -> EVAL_DATALOADERS:
r"""An iterable or collection of iterables specifying test samples.
For more information about multiple dataloaders, see this :ref:`section <multiple-dataloaders>`.
For data processing use the following pattern:
- download in :meth:`prepare_data`
- process and split in :meth:`setup`
However, the above are only necessary for distributed processing.
.. warning:: do not assign state in prepare_data
- :meth:`~lightning.pytorch.trainer.trainer.Trainer.test`
- :meth:`prepare_data`View on GitHub (pinned to 9fed5c27d2)
Solutions
- Implement def train_dataloader(self) in your LightningModule or DataModule returning a DataLoader/iterable
- Or pass the loader directly: trainer.fit(model, train_dataloaders=train_dl)
- Check spelling/signature — the override must be exactly train_dataloader
Example fix
# before
class MyModule(LightningModule):
def training_step(self, batch, batch_idx): ...
# no train_dataloader -> MisconfigurationException
# after
class MyModule(LightningModule):
def training_step(self, batch, batch_idx): ...
def train_dataloader(self):
return DataLoader(self.dataset, batch_size=32) Defensive patterns
Strategy: validation
Validate before calling
def has_train_dataloader(obj) -> bool:
from lightning.pytorch.cli import LightningModule # or core
m = type(obj).train_dataloader
return getattr(m, '__owner__', None) is not type(obj) or 'train_dataloader' in type(obj).__dict__
# simplest robust check:
assert 'train_dataloader' in MyModule.__dict__ or train_dl is not None Type guard
def implements_train_dataloader(cls) -> bool:
"""True if cls itself (not the base stub) defines train_dataloader."""
return 'train_dataloader' in cls.__dict__ or any('train_dataloader' in c.__dict__ for c in cls.__mro__[1:-1] if c.__name__ != 'Hooks') Prevention
- Pass train_dataloaders explicitly when the module has no loader method
- Smoke-test trainer.fit with limit_train_batches=1 before long runs
When it happens
Trigger: trainer.fit(model) where the LightningDataModule/Module never defines train_dataloader and no train_dataloaders= argument was passed to fit.
Common situations: Forgetting to implement train_dataloader when switching from a module that only does predict/test; typos in the method name (train_dataloaders) so the override isn't picked up; passing datamodule=None accidentally.
Related errors
- `test_dataloader` must be implemented to be used with the Li
- The `CSVLogger` does not yet support logging hyperparameters
- DeepSpeed handles gradient clipping automatically within the
- The `{type(self).__name__}` does not use the `CheckpointIO`
- The `{type(self).__name__}` does not support setting a `Chec
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/0b838e7c7aca91bd.
Report an issue: GitHub.