{"record":{"id":"222bf544a50716e7","repo":"Lightning-AI/pytorch-lightning","slug":"val-dataloader-must-be-implemented-to-be-used-wi","errorCode":null,"errorMessage":"`val_dataloader` must be implemented to be used with the Lightning Trainer","messagePattern":"`val_dataloader` must be implemented to be used with the Lightning Trainer","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"critical","filePath":"src/lightning/pytorch/core/hooks.py","lineNumber":540,"sourceCode":"        a positive integer.\n\n        It's recommended that all data downloads and preparation happen in :meth:`prepare_data`.\n\n        - :meth:`~lightning.pytorch.trainer.trainer.Trainer.fit`\n        - :meth:`~lightning.pytorch.trainer.trainer.Trainer.validate`\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        Note:\n            If you don't need a validation dataset and a :meth:`validation_step`, you don't need to\n            implement this method.\n\n        \"\"\"\n        raise MisconfigurationException(\"`val_dataloader` must be implemented to be used with the Lightning Trainer\")\n\n    def predict_dataloader(self) -> EVAL_DATALOADERS:\n        r\"\"\"An iterable or collection of iterables specifying prediction samples.\n\n        For more information about multiple dataloaders, see this :ref:`section <multiple-dataloaders>`.\n\n        It's recommended that all data downloads and preparation happen in :meth:`prepare_data`.\n\n        - :meth:`~lightning.pytorch.trainer.trainer.Trainer.predict`\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        Return:\n            A :class:`torch.utils.data.DataLoader` or a sequence of them specifying prediction samples.","sourceCodeStart":522,"sourceCodeEnd":558,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/hooks.py#L522-L558","documentation":"LightningModule.val_dataloader is a stub hook that raises MisconfigurationException unless the user overrides it. The Trainer requires a validation dataloader whenever a validation loop runs (trainer.fit with validation, trainer.validate). If the method is not implemented on your subclass, calling it produces this error.","triggerScenarios":"Calling trainer.validate(model) or trainer.fit(model) with a validation loop enabled on a LightningModule that does not override val_dataloader; or calling model.val_dataloader() directly.","commonSituations":"User wrote training_step but forgot the validation dataloader; copied a template module that only implements train_dataloader; assumed the Trainer would fall back to train_dataloader for validation.","solutions":["Implement `def val_dataloader(self)` in your LightningModule returning a DataLoader or sequence of DataLoaders","If you don't need validation, call trainer.fit(model) without a val_dataloader or use trainer.validate only when implemented","For prediction-only workflows use trainer.predict with predict_dataloader instead"],"exampleFix":"// before\nclass MyModel(L.LightningModule):\n    def training_step(self, batch, batch_idx): ...\n    # no val_dataloader\n\n# after\nclass MyModel(L.LightningModule):\n    def training_step(self, batch, batch_idx): ...\n    def val_dataloader(self):\n        return DataLoader(val_dataset, batch_size=32)","handlingStrategy":"validation","validationCode":"hook = getattr(model, 'val_dataloader', None)\nimplemented = hook is not None and type(model).val_dataloader is not L.LightningModule.val_dataloader\nif not implemented and need_validation:\n    raise ValueError('implement val_dataloader before trainer.validate/fit')","typeGuard":"def has_val_dataloader(model) -> bool:\n    return type(model).val_dataloader is not L.LightningModule.val_dataloader","tryCatchPattern":"from lightning.pytorch.utilities.exceptions import MisconfigurationException\ntry:\n    trainer.validate(model)\nexcept MisconfigurationException as e:\n    if 'val_dataloader' in str(e):\n        model.val_dataloader = lambda: DataLoader(val_ds)","preventionTips":["Always pair validation_step with val_dataloader in module templates","Run a smoke test trainer.fit(..., max_steps=1, limit_val_batches=1) in CI to catch missing hooks"],"tags":["pytorch-lightning","lightning-module","dataloader","validation","hook-not-implemented"],"backgroundTag":"required-method-not-implemented","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}