Lightning-AI/pytorch-lightning · error · MisconfigurationException

Trying to inject parameters into the `{dataloader_cls_name}`

Error message

Trying to inject parameters into the `{dataloader_cls_name}` instance. This would fail as it doesn't expose all its attributes in the `__init__` signature. The missing arguments are {sorted_missing_kwargs}. HINT: If you wrote the `{dataloader_cls_name}` class, add the `__init__` arguments or allow passing `**kwargs`

What it means

Raised as a MisconfigurationException when reconstructing a dataloader would require passing kwargs that the DataLoader subclass's __init__ signature does not accept and it doesn't take **kwargs. Lightning computed the saved/derived kwargs (batch_size, sampler, etc.) but the class cannot accept them, so re-instantiation would fail with a TypeError.

Source

Thrown at src/lightning/pytorch/utilities/data.py:223

    if required_args:
        sorted_required_args = sorted(required_args)
        dataloader_cls_name = dataloader.__class__.__name__
        missing_args_message = ", ".join(f"`self.{arg_name}`" for arg_name in sorted_required_args)
        raise MisconfigurationException(
            f"Trying to inject custom `Sampler` into the `{dataloader_cls_name}` instance. "
            "This would fail as some of the `__init__` arguments are not available as instance attributes. "
            f"The missing attributes are {sorted_required_args}. If you instantiate your `{dataloader_cls_name}` "
            "inside a `*_dataloader` hook of your module, we will do this for you."
            f" Otherwise, define {missing_args_message} inside your `__init__`."
        )

    if not has_variadic_kwargs:
        # the dataloader signature does not allow keyword arguments that need to be passed
        missing_kwargs = (set(dl_kwargs) | set(arg_names)) - params.keys()
        if missing_kwargs:
            sorted_missing_kwargs = sorted(missing_kwargs)
            dataloader_cls_name = dataloader.__class__.__name__
            raise MisconfigurationException(
                f"Trying to inject parameters into the `{dataloader_cls_name}` instance. "
                "This would fail as it doesn't expose all its attributes in the `__init__` signature. "
                f"The missing arguments are {sorted_missing_kwargs}. HINT: If you wrote the `{dataloader_cls_name}` "
                "class, add the `__init__` arguments or allow passing `**kwargs`"
            )

    return dl_args, dl_kwargs


def _dataloader_init_kwargs_resolve_sampler(
    dataloader: DataLoader,
    sampler: Union[Sampler, Iterable],
    mode: Optional[RunningStage] = None,
) -> dict[str, Any]:
    """This function is used to handle the sampler, batch_sampler arguments associated within a DataLoader for its re-
    instantiation.

    If the dataloader is being used for prediction, the sampler will be wrapped into an `_IndexBatchSamplerWrapper`, so

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Add **kwargs to your subclass __init__ and forward them to super().__init__
  2. Or explicitly add the reported missing arguments to the __init__ signature
  3. Or avoid triggering reinstantiation (Trainer(use_distributed_sampler=False))

Example fix

# before
class MyDL(DataLoader):
    def __init__(self, dataset, batch_size):
        super().__init__(dataset, batch_size=batch_size)

# after
class MyDL(DataLoader):
    def __init__(self, dataset, batch_size, **kwargs):
        super().__init__(dataset, batch_size=batch_size, **kwargs)
Defensive patterns

Strategy: validation

Validate before calling

import inspect

def accepts_variadic_kwargs(dl) -> bool:
    for p in inspect.signature(type(dl).__init__).parameters.values():
        if p.kind is inspect.Parameter.VAR_KEYWORD:
            return True
    return False

assert accepts_variadic_kwargs(my_loader), 'add **kwargs to your DataLoader subclass __init__'

Prevention

When it happens

Trigger: Using a DataLoader subclass whose __init__ has a fixed signature without **kwargs while Lightning needs to pass extra parameters (e.g. a distributed sampler or changed batch_sampler); combined with _update_dataloader during distributed training or batch-size recalculation.

Common situations: Strict custom __init__ signatures like def __init__(self, dataset, batch_size) that omit num_workers/sampler/etc.; third-party dataloaders with narrow signatures; version upgrades that inject additional kwargs.

Related errors


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