Lightning-AI/pytorch-lightning · error · MisconfigurationException

`predict_dataloader` must be implemented to be used with the

Error message

`predict_dataloader` must be implemented to be used with the Lightning Trainer

What it means

LightningModule.predict_dataloader is a stub hook that raises MisconfigurationException unless overridden. The Trainer's predict loop requires it to know what data to run prediction over. Invoking prediction without this method (and without passing dataloaders to trainer.predict) triggers the error.

Source

Thrown at src/lightning/pytorch/core/hooks.py:561

        r"""An iterable or collection of iterables specifying prediction samples.

        For more information about multiple dataloaders, see this :ref:`section <multiple-dataloaders>`.

        It's recommended that all data downloads and preparation happen in :meth:`prepare_data`.

        - :meth:`~lightning.pytorch.trainer.trainer.Trainer.predict`
        - :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.

        Return:
            A :class:`torch.utils.data.DataLoader` or a sequence of them specifying prediction samples.

        """
        raise MisconfigurationException(
            "`predict_dataloader` must be implemented to be used with the Lightning Trainer"
        )

    def transfer_batch_to_device(self, batch: Any, device: torch.device, dataloader_idx: int) -> Any:
        """Override this hook if your :class:`~torch.utils.data.DataLoader` returns tensors wrapped in a custom data
        structure.

        The data types listed below (and any arbitrary nesting of them) are supported out of the box:

        - :class:`torch.Tensor` or anything that implements `.to(...)`
        - :class:`list`
        - :class:`dict`
        - :class:`tuple`

        For anything else, you need to define how the data is moved to the target device (CPU, GPU, TPU, ...).

        Note:
            This hook should only transfer the data and not modify it, nor should it move the data to

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Implement `def predict_dataloader(self)` returning a DataLoader or list of DataLoaders
  2. Alternatively pass the dataloader directly: trainer.predict(model, dataloaders=pred_dl)
  3. If you meant validation instead of prediction, use trainer.validate with val_dataloader

Example fix

// before
trainer.predict(model)

// after
trainer.predict(model, dataloaders=DataLoader(pred_ds, batch_size=64))
# or implement:
# def predict_dataloader(self): return DataLoader(pred_ds)
Defensive patterns

Strategy: validation

Validate before calling

if trainer.state.fn == trainer.stateFn.PREDICT and type(model).predict_dataloader is L.LightningModule.predict_dataloader and not dataloaders:
    raise ValueError('pass dataloaders= or implement predict_dataloader')

Type guard

def has_predict_dataloader(model) -> bool:
    return type(model).predict_dataloader is not L.LightningModule.predict_dataloader

Try / catch

try:
    trainer.predict(model)
except MisconfigurationException as e:
    if 'predict_dataloader' in str(e):
        preds = trainer.predict(model, dataloaders=DataLoader(ds))

Prevention

When it happens

Trigger: Calling trainer.predict(model) on a module that doesn't implement predict_dataloader, or calling model.predict_dataloader() directly.

Common situations: User only implemented train_dataloader/val_dataloader and assumed predict would reuse them; migrated a training script to run inference without adding a predict dataloader.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/759ddbf690720969. Report an issue: GitHub.