Lightning-AI/pytorch-lightning · error · RuntimeError
You provided only a single `{stage.dataloader_prefix}_datalo
Error message
You provided only a single `{stage.dataloader_prefix}_dataloader`, but have included `dataloader_idx` in `{type(pl_module).__name__}.{hook}()`. Either remove the argument or give it a default value i.e. `dataloader_idx=0`. What it means
Raised by _verify_dataloader_idx_requirement when a single {train/val/test/predict}_dataloader was provided but the corresponding step hook (e.g. training_step) declares a required `dataloader_idx` parameter with no default. Lightning would have to pass an index that does not exist for a single loader, so the signature is rejected.
Source
Thrown at src/lightning/pytorch/loops/utilities.py:195
context_manager = torch.no_grad
with context_manager():
return loop_run(self, *args, **kwargs)
return _decorator
def _verify_dataloader_idx_requirement(
hooks: tuple[str, ...], is_expected: bool, stage: RunningStage, pl_module: "pl.LightningModule"
) -> None:
for hook in hooks:
fx = getattr(pl_module, hook)
# this validation only works if "dataloader_idx" is used, no other names such as "dl_idx"
param_present = is_param_in_hook_signature(fx, "dataloader_idx")
if not is_expected:
if param_present:
params = inspect.signature(fx).parameters
if "dataloader_idx" in params and params["dataloader_idx"].default is inspect.Parameter.empty:
raise RuntimeError(
f"You provided only a single `{stage.dataloader_prefix}_dataloader`, but have included "
f"`dataloader_idx` in `{type(pl_module).__name__}.{hook}()`. Either remove the"
" argument or give it a default value i.e. `dataloader_idx=0`."
)
elif not param_present:
raise RuntimeError(
f"You provided multiple `{stage.dataloader_prefix}_dataloader`, but no `dataloader_idx`"
f" argument in `{type(pl_module).__name__}.{hook}()`. Try adding `dataloader_idx=0` to its"
" signature."
)
View on GitHub (pinned to 9fed5c27d2)
Solutions
- Give the parameter a default: `def training_step(self, batch, batch_idx, dataloader_idx=0)`
- Or remove `dataloader_idx` from the signature entirely for single-loader setups
- Or pass a list of dataloaders so the index is meaningful
Example fix
# before
def training_step(self, batch, batch_idx, dataloader_idx):
...
trainer.fit(model, single_loader)
# after
def training_step(self, batch, batch_idx, dataloader_idx=0):
...
trainer.fit(model, single_loader) Defensive patterns
Strategy: validation
Validate before calling
import inspect
params = inspect.signature(model.training_step).parameters
n_loaders = 1 if not isinstance(train_dataloader, (list, tuple)) else len(train_dataloader)
if n_loaders == 1 and 'dataloader_idx' in params:
assert params['dataloader_idx'].default is not inspect.Parameter.empty, 'give dataloader_idx a default' Type guard
def signature_ok_single_loader(fn) -> bool:
p = inspect.signature(fn).parameters.get('dataloader_idx')
return p is None or p.default is not inspect.Parameter.empty Prevention
- Always declare `dataloader_idx=0` with a default so hooks work for both single and multiple loaders
- Re-run fast_dev_run after changing dataloader counts
When it happens
Trigger: `def training_step(self, batch, batch_idx, dataloader_idx):` while passing one dataloader (not a list) to fit; removing a second dataloader from a datamodule without simplifying the step signature.
Common situations: Downsizing an experiment from multi-dataloader to single dataloader; sharing a LightningModule between single- and multi-loader experiments.
Related errors
- You provided multiple `{stage.dataloader_prefix}_dataloader`
- Device should be CPU, got {device} instead.
- `devices` selected with `CPUAccelerator` should be an int >
- Device should be CUDA, got {device} instead.
- You requested to find {num_devices} devices but there are no
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/e07ec025927dd73e.
Report an issue: GitHub.