{"record":{"id":"34c2cd33e7d31fd7","repo":"Lightning-AI/pytorch-lightning","slug":"trainer-validate-requires-a-lightningmodule","errorCode":null,"errorMessage":"\"`Trainer.validate()` requires a `LightningModule` when it hasn't been passed in a previous run\"","messagePattern":"\"`Trainer\\.validate\\(\\)` requires a `LightningModule` when it hasn't been passed in a previous run\"","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/trainer/trainer.py","lineNumber":698,"sourceCode":"            like :meth:`~lightning.pytorch.LightningModule.validation_step` etc.\n            The length of the list corresponds to the number of validation dataloaders used.\n\n        Raises:\n            TypeError:\n                If no ``model`` is passed and there was no ``LightningModule`` passed in the previous run.\n                If ``model`` passed is not `LightningModule` or `torch._dynamo.OptimizedModule`.\n\n            MisconfigurationException:\n                If both ``dataloaders`` and ``datamodule`` are passed. Pass only one of these.\n\n            RuntimeError:\n                If a compiled ``model`` is passed and the strategy is not supported.\n\n        \"\"\"\n        if model is None:\n            # do we still have a reference from a previous call?\n            if self.lightning_module is None:\n                raise TypeError(\n                    \"`Trainer.validate()` requires a `LightningModule` when it hasn't been passed in a previous run\"\n                )\n        else:\n            model = _maybe_unwrap_optimized(model)\n            self.strategy._lightning_module = model\n        _verify_strategy_supports_compile(self.lightning_module, self.strategy)\n        self.state.fn = TrainerFn.VALIDATING\n        self.state.status = TrainerStatus.RUNNING\n        self.validating = True\n        return call._call_and_handle_interrupt(\n            self, self._validate_impl, model, dataloaders, ckpt_path, verbose, datamodule, weights_only\n        )\n\n    def _validate_impl(\n        self,\n        model: Optional[\"pl.LightningModule\"] = None,\n        dataloaders: Optional[Union[EVAL_DATALOADERS, LightningDataModule]] = None,\n        ckpt_path: Optional[_PATH] = None,","sourceCodeStart":680,"sourceCodeEnd":716,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/trainer/trainer.py#L680-L716","documentation":"trainer.validate() was called with model=None but the Trainer has no reference to a LightningModule from a previous run. validate() can reuse the model only after fit/validate/test/predict has attached one; on a fresh Trainer you must pass the model explicitly.","triggerScenarios":"trainer = Trainer(); trainer.validate() without any prior fit/test/predict call; or using a new Trainer instance after a previous run finished and expecting it to remember the model.","commonSituations":"Script structure where validation is done in a separate process/session with a fresh Trainer, or calling validate() first thing expecting checkpoint auto-loading (it does not auto-load).","solutions":["Pass the model: trainer.validate(model, dataloaders=...)","Load a checkpoint into the model first via CustomModel.load_from_checkpoint(...) then pass it","Call trainer.fit(model) before trainer.validate() on the same Trainer"],"exampleFix":"# before\ntrainer = Trainer()\ntrainer.validate(dataloaders=val_loader)\n# after\nmodel = LitModel.load_from_checkpoint(\"ckpt.ckpt\")\ntrainer = Trainer()\ntrainer.validate(model, dataloaders=val_loader)","handlingStrategy":"type-guard","validationCode":"if trainer.lightning_module is None:\n    model = LitModel.load_from_checkpoint(\"ckpt.ckpt\")\nelse:\n    model = trainer.lightning_module\ntrainer.validate(model, dataloaders=loader)","typeGuard":"def has_model(t) -> bool:\n    return t.lightning_module is not None","tryCatchPattern":null,"preventionTips":["Always pass the model explicitly in evaluation scripts","Remember a new Trainer instance never carries over the model from a previous run"],"tags":["trainer","validate","missing-model","pytorch-lightning"],"backgroundTag":"missing-required-model-argument","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}