{"record":{"id":"6a6a34ac98b2d20d","repo":"Lightning-AI/pytorch-lightning","slug":"trying-to-inject-parameters-into-the-dataloader-6a6a34","errorCode":null,"errorMessage":"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`","messagePattern":"Trying to inject parameters into the `(.+?)` instance\\. This would fail as it doesn't expose all its attributes in the `__init__` signature\\. The missing arguments are (.+?)\\. HINT: If you wrote the `(.+?)` class, add the `__init__` arguments or allow passing `\\*\\*kwargs`","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/utilities/data.py","lineNumber":223,"sourceCode":"    if required_args:\n        sorted_required_args = sorted(required_args)\n        dataloader_cls_name = dataloader.__class__.__name__\n        missing_args_message = \", \".join(f\"`self.{arg_name}`\" for arg_name in sorted_required_args)\n        raise MisconfigurationException(\n            f\"Trying to inject custom `Sampler` into the `{dataloader_cls_name}` instance. \"\n            \"This would fail as some of the `__init__` arguments are not available as instance attributes. \"\n            f\"The missing attributes are {sorted_required_args}. If you instantiate your `{dataloader_cls_name}` \"\n            \"inside a `*_dataloader` hook of your module, we will do this for you.\"\n            f\" Otherwise, define {missing_args_message} inside your `__init__`.\"\n        )\n\n    if not has_variadic_kwargs:\n        # the dataloader signature does not allow keyword arguments that need to be passed\n        missing_kwargs = (set(dl_kwargs) | set(arg_names)) - params.keys()\n        if missing_kwargs:\n            sorted_missing_kwargs = sorted(missing_kwargs)\n            dataloader_cls_name = dataloader.__class__.__name__\n            raise MisconfigurationException(\n                f\"Trying to inject parameters into the `{dataloader_cls_name}` instance. \"\n                \"This would fail as it doesn't expose all its attributes in the `__init__` signature. \"\n                f\"The missing arguments are {sorted_missing_kwargs}. HINT: If you wrote the `{dataloader_cls_name}` \"\n                \"class, add the `__init__` arguments or allow passing `**kwargs`\"\n            )\n\n    return dl_args, dl_kwargs\n\n\ndef _dataloader_init_kwargs_resolve_sampler(\n    dataloader: DataLoader,\n    sampler: Union[Sampler, Iterable],\n    mode: Optional[RunningStage] = None,\n) -> dict[str, Any]:\n    \"\"\"This function is used to handle the sampler, batch_sampler arguments associated within a DataLoader for its re-\n    instantiation.\n\n    If the dataloader is being used for prediction, the sampler will be wrapped into an `_IndexBatchSamplerWrapper`, so","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/utilities/data.py#L205-L241","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add **kwargs to your subclass __init__ and forward them to super().__init__","Or explicitly add the reported missing arguments to the __init__ signature","Or avoid triggering reinstantiation (Trainer(use_distributed_sampler=False))"],"exampleFix":"# before\nclass MyDL(DataLoader):\n    def __init__(self, dataset, batch_size):\n        super().__init__(dataset, batch_size=batch_size)\n\n# after\nclass MyDL(DataLoader):\n    def __init__(self, dataset, batch_size, **kwargs):\n        super().__init__(dataset, batch_size=batch_size, **kwargs)","handlingStrategy":"validation","validationCode":"import inspect\n\ndef accepts_variadic_kwargs(dl) -> bool:\n    for p in inspect.signature(type(dl).__init__).parameters.values():\n        if p.kind is inspect.Parameter.VAR_KEYWORD:\n            return True\n    return False\n\nassert accepts_variadic_kwargs(my_loader), 'add **kwargs to your DataLoader subclass __init__'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always add **kwargs to custom DataLoader __init__ signatures and forward to super()","Mirror standard DataLoader parameters you override in the signature","Test dataloader reinstantiation in a 2-device smoke run before long training"],"tags":["pytorch-lightning","dataloader","kwargs","signature-mismatch"],"backgroundTag":"dataloader-init-signature-mismatch","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}