Lightning-AI/pytorch-lightning · error · ValueError
`setup_dataloaders` requires at least one dataloader as inpu
Error message
`setup_dataloaders` requires at least one dataloader as input.
What it means
Raised by `_validate_setup_dataloaders` when `fabric.setup_dataloaders()` is called with an empty sequence. Fabric needs at least one dataloader to wrap it in `_FabricDataLoader` and prepare it for the distributed strategy.
Source
Thrown at src/lightning/fabric/fabric.py:1247
)
if not optimizers:
raise ValueError("`setup_optimizers` requires at least one optimizer as input.")
if any(isinstance(opt, _FabricOptimizer) for opt in optimizers):
raise ValueError("An optimizer should be passed only once to the `setup_optimizers` method.")
if any(_has_meta_device_parameters_or_buffers(optimizer) for optimizer in optimizers):
raise RuntimeError(
"The optimizer has references to the model's meta-device parameters. Materializing them is"
" is currently not supported. Create the optimizer after setting up the model, then call"
" `fabric.setup_optimizers(optimizer)`."
)
def _validate_setup_dataloaders(self, dataloaders: Sequence[DataLoader]) -> None:
self._validate_launched()
if not dataloaders:
raise ValueError("`setup_dataloaders` requires at least one dataloader as input.")
if any(isinstance(dl, _FabricDataLoader) for dl in dataloaders):
raise ValueError("A dataloader should be passed only once to the `setup_dataloaders` method.")
if any(not isinstance(dl, DataLoader) for dl in dataloaders):
raise TypeError("Only PyTorch DataLoader are currently supported in `setup_dataloaders`.")
@staticmethod
def _configure_callbacks(callbacks: Optional[Union[list[Any], Any]]) -> list[Any]:
callbacks = callbacks if callbacks is not None else []
callbacks = callbacks if isinstance(callbacks, list) else [callbacks]
callbacks.extend(_load_external_callbacks("lightning.fabric.callbacks_factory"))
return callbacks
View on GitHub (pinned to 9fed5c27d2)
Solutions
- Pass at least one `torch.utils.data.DataLoader`, e.g. `train_dl = fabric.setup_dataloaders(DataLoader(train_ds, batch_size=32))`
- Guard the call: only invoke setup_dataloaders when the dataloader list is non-empty
- Check upstream logic that builds the dataloader list for bugs that empty it
Example fix
# before
loaders = [dl for dl in maybe_dataloaders if dl is not None]
fabric.setup_dataloaders(*loaders) # may be empty
# after
if loaders:
loaders = fabric.setup_dataloaders(*loaders) Defensive patterns
Strategy: validation
Validate before calling
if dataloaders:
dataloaders = fabric.setup_dataloaders(*dataloaders) Prevention
- Guard setup calls on non-empty collections
- Log the number of dataloaders built per split
When it happens
Trigger: Calling `fabric.setup_dataloaders()` with no args, or with an empty list, e.g. when building dataloaders conditionally and the list is empty at runtime.
Common situations: Evaluation-only or inference scripts adapted from training scripts where the val/test dataloader list may be empty; loops constructing dataloaders per split.
Related errors
- `setup_optimizers` requires at least one optimizer as input.
- A dataloader should be passed only once to the `setup_datalo
- Only PyTorch DataLoader are currently supported in `setup_da
- `val_dataloader` must be implemented to be used with the Lig
- `self.log_dict({dictionary})` was called, but nested diction
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/559be418b9cd682e.
Report an issue: GitHub.