Lightning-AI/pytorch-lightning · error · TypeError

"`Trainer.validate()` requires a `LightningModule` when it h

Error message

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

What it means

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.

Source

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

            like :meth:`~lightning.pytorch.LightningModule.validation_step` etc.
            The length of the list corresponds to the number of validation dataloaders used.

        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.

        """
        if model is None:
            # do we still have a reference from a previous call?
            if self.lightning_module is None:
                raise TypeError(
                    "`Trainer.validate()` 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.VALIDATING
        self.state.status = TrainerStatus.RUNNING
        self.validating = True
        return call._call_and_handle_interrupt(
            self, self._validate_impl, model, dataloaders, ckpt_path, verbose, datamodule, weights_only
        )

    def _validate_impl(
        self,
        model: Optional["pl.LightningModule"] = None,
        dataloaders: Optional[Union[EVAL_DATALOADERS, LightningDataModule]] = None,
        ckpt_path: Optional[_PATH] = None,

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Pass the model: trainer.validate(model, dataloaders=...)
  2. Load a checkpoint into the model first via CustomModel.load_from_checkpoint(...) then pass it
  3. Call trainer.fit(model) before trainer.validate() on the same Trainer

Example fix

# before
trainer = Trainer()
trainer.validate(dataloaders=val_loader)
# after
model = LitModel.load_from_checkpoint("ckpt.ckpt")
trainer = Trainer()
trainer.validate(model, dataloaders=val_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.validate(model, dataloaders=loader)

Type guard

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

Prevention

When it happens

Trigger: 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.

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

Related errors


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