Lightning-AI/pytorch-lightning · error · TypeError

`Trainer.predict()` requires a `LightningModule` when it has

Error message

`Trainer.predict()` requires a `LightningModule` when it hasn't been passed in a previous run

What it means

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.

Source

Thrown at src/lightning/pytorch/trainer/trainer.py:937

        Raises:
            TypeError:
                If no ``model`` is passed and there was no ``LightningModule`` passed in the previous run.
                If ``model`` passed is not `LightningModule` or `torch._dynamo.OptimizedModule`.

            MisconfigurationException:
                If both ``dataloaders`` and ``datamodule`` are passed. Pass only one of these.

            RuntimeError:
                If a compiled ``model`` is passed and the strategy is not supported.

        See :ref:`Lightning inference section<deploy/production_basic:Predict step with your LightningModule>` for more.

        """
        if model is None:
            # do we still have a reference from a previous call?
            if self.lightning_module is None:
                raise TypeError(
                    "`Trainer.predict()` requires a `LightningModule` when it hasn't been passed in a previous run"
                )
        else:
            model = _maybe_unwrap_optimized(model)
            self.strategy._lightning_module = model
        _verify_strategy_supports_compile(self.lightning_module, self.strategy)
        self.state.fn = TrainerFn.PREDICTING
        self.state.status = TrainerStatus.RUNNING
        self.predicting = True
        return call._call_and_handle_interrupt(
            self,
            self._predict_impl,
            model,
            dataloaders,
            datamodule,
            return_predictions,
            ckpt_path,
            weights_only,

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Load the model and pass it: model = LitModel.load_from_checkpoint(...); trainer.predict(model)
  2. Call trainer.fit(model) first on the same Trainer
  3. Reuse the same Trainer object that already ran fit/predict

Example fix

# before
trainer = Trainer()
preds = trainer.predict(dataloaders=loader)
# after
model = LitModel.load_from_checkpoint("ckpt.ckpt")
preds = trainer.predict(model, dataloaders=loader)
Defensive patterns

Strategy: type-guard

Validate before calling

if trainer.lightning_module is None:
    model = LitModel.load_from_checkpoint("ckpt.ckpt")
else:
    model = trainer.lightning_module
trainer.predict(model, dataloaders=loader)

Type guard

def has_model(t) -> bool:
    return t.lightning_module is not None

Prevention

When it happens

Trigger: Fresh Trainer followed directly by trainer.predict(dataloaders=...); or a new Trainer instance created for an inference script without passing a model.

Common situations: 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.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/e649f03ec89e6513. Report an issue: GitHub.