{"record":{"id":"e649f03ec89e6513","repo":"Lightning-AI/pytorch-lightning","slug":"trainer-predict-requires-a-lightningmodule-w","errorCode":null,"errorMessage":"`Trainer.predict()` requires a `LightningModule` when it hasn't been passed in a previous run","messagePattern":"`Trainer\\.predict\\(\\)` 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":937,"sourceCode":"\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        See :ref:`Lightning inference section<deploy/production_basic:Predict step with your LightningModule>` for more.\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.predict()` 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.PREDICTING\n        self.state.status = TrainerStatus.RUNNING\n        self.predicting = True\n        return call._call_and_handle_interrupt(\n            self,\n            self._predict_impl,\n            model,\n            dataloaders,\n            datamodule,\n            return_predictions,\n            ckpt_path,\n            weights_only,","sourceCodeStart":919,"sourceCodeEnd":955,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/trainer/trainer.py#L919-L955","documentation":"trainer.predict() was called with model=None on a Trainer with no previously attached LightningModule. predict() reuses a model only after fit/validate/test/predict has run on the same Trainer; otherwise you must supply the model.","triggerScenarios":"Fresh Trainer followed directly by trainer.predict(dataloaders=...); or a new Trainer instance created for an inference script without passing a model.","commonSituations":"Standalone inference scripts that build a Trainer and call predict() expecting the model to be picked up from a checkpoint or from a prior session.","solutions":["Load the model and pass it: model = LitModel.load_from_checkpoint(...); trainer.predict(model)","Call trainer.fit(model) first on the same Trainer","Reuse the same Trainer object that already ran fit/predict"],"exampleFix":"# before\ntrainer = Trainer()\npreds = trainer.predict(dataloaders=loader)\n# after\nmodel = LitModel.load_from_checkpoint(\"ckpt.ckpt\")\npreds = trainer.predict(model, dataloaders=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.predict(model, dataloaders=loader)","typeGuard":"def has_model(t) -> bool:\n    return t.lightning_module is not None","tryCatchPattern":null,"preventionTips":["Inference scripts should always load and pass the model explicitly","Do not assume Trainer state persists across processes or instances"],"tags":["trainer","predict","inference","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"}