{"record":{"id":"0b838e7c7aca91bd","repo":"Lightning-AI/pytorch-lightning","slug":"train-dataloader-must-be-implemented-to-be-used","errorCode":null,"errorMessage":"`train_dataloader` must be implemented to be used with the Lightning Trainer","messagePattern":"`train_dataloader` must be implemented to be used with the Lightning Trainer","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/core/hooks.py","lineNumber":483,"sourceCode":"        For data processing use the following pattern:\n\n            - download in :meth:`prepare_data`\n            - process and split in :meth:`setup`\n\n        However, the above are only necessary for distributed processing.\n\n        .. warning:: do not assign state in prepare_data\n\n        - :meth:`~lightning.pytorch.trainer.trainer.Trainer.fit`\n        - :meth:`prepare_data`\n        - :meth:`setup`\n\n        Note:\n            Lightning tries to add the correct sampler for distributed and arbitrary hardware.\n            There is no need to set it yourself.\n\n        \"\"\"\n        raise MisconfigurationException(\"`train_dataloader` must be implemented to be used with the Lightning Trainer\")\n\n    def test_dataloader(self) -> EVAL_DATALOADERS:\n        r\"\"\"An iterable or collection of iterables specifying test samples.\n\n        For more information about multiple dataloaders, see this :ref:`section <multiple-dataloaders>`.\n\n        For data processing use the following pattern:\n\n            - download in :meth:`prepare_data`\n            - process and split in :meth:`setup`\n\n        However, the above are only necessary for distributed processing.\n\n        .. warning:: do not assign state in prepare_data\n\n\n        - :meth:`~lightning.pytorch.trainer.trainer.Trainer.test`\n        - :meth:`prepare_data`","sourceCodeStart":465,"sourceCodeEnd":501,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/hooks.py#L465-L501","documentation":"The LightningModule/LightningDataModule base implementation of train_dataloader is a stub that raises MisconfigurationException. It exists so that calling fit() without the user overriding train_dataloader fails loudly instead of silently returning None.","triggerScenarios":"trainer.fit(model) where the LightningDataModule/Module never defines train_dataloader and no train_dataloaders= argument was passed to fit.","commonSituations":"Forgetting to implement train_dataloader when switching from a module that only does predict/test; typos in the method name (train_dataloaders) so the override isn't picked up; passing datamodule=None accidentally.","solutions":["Implement def train_dataloader(self) in your LightningModule or DataModule returning a DataLoader/iterable","Or pass the loader directly: trainer.fit(model, train_dataloaders=train_dl)","Check spelling/signature — the override must be exactly train_dataloader"],"exampleFix":"# before\nclass MyModule(LightningModule):\n    def training_step(self, batch, batch_idx): ...\n    # no train_dataloader -> MisconfigurationException\n# after\nclass MyModule(LightningModule):\n    def training_step(self, batch, batch_idx): ...\n    def train_dataloader(self):\n        return DataLoader(self.dataset, batch_size=32)","handlingStrategy":"validation","validationCode":"def has_train_dataloader(obj) -> bool:\n    from lightning.pytorch.cli import LightningModule  # or core\n    m = type(obj).train_dataloader\n    return getattr(m, '__owner__', None) is not type(obj) or 'train_dataloader' in type(obj).__dict__\n# simplest robust check:\nassert 'train_dataloader' in MyModule.__dict__ or train_dl is not None","typeGuard":"def implements_train_dataloader(cls) -> bool:\n    \"\"\"True if cls itself (not the base stub) defines train_dataloader.\"\"\"\n    return 'train_dataloader' in cls.__dict__ or any('train_dataloader' in c.__dict__ for c in cls.__mro__[1:-1] if c.__name__ != 'Hooks')","tryCatchPattern":null,"preventionTips":["Pass train_dataloaders explicitly when the module has no loader method","Smoke-test trainer.fit with limit_train_batches=1 before long runs"],"tags":["train-dataloader","not-implemented","lightning-module","required-method"],"backgroundTag":"abstract-method-not-implemented","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}