Lightning-AI/pytorch-lightning · error · ValueError

`.{fn}(ckpt_path="best")` is set but `ModelCheckpoint` is no

Error message

`.{fn}(ckpt_path="best")` is set but `ModelCheckpoint` is not configured.

What it means

Raised by CheckpointConnector._parse_ckpt_path when ckpt_path="best" is passed to .validate()/.test()/.predict() but no ModelCheckpoint callback is configured. "best" requires the checkpoint callback to have recorded a best_model_path, which only exists if checkpointing was enabled during fit.

Source

Thrown at src/lightning/pytorch/trainer/connectors/checkpoint_connector.py:166

            )
            rank_zero_warn(
                f"`.{fn}(ckpt_path=None)` was called without a model."
                " The best model of the previous `fit` call will be used."
                + ft_tip
                + f" You can pass `.{fn}(ckpt_path='best')` to use the best model or"
                f" `.{fn}(ckpt_path='last')` to use the last model."
                " If you pass a value, this warning will be silenced."
            )

        if ckpt_path == "best":
            if len(self.trainer.checkpoint_callbacks) > 1:
                rank_zero_warn(
                    f'`.{fn}(ckpt_path="best")` is called with Trainer configured with multiple `ModelCheckpoint`'
                    " callbacks. It will use the best checkpoint path from first checkpoint callback."
                )

            if not self.trainer.checkpoint_callback:
                raise ValueError(f'`.{fn}(ckpt_path="best")` is set but `ModelCheckpoint` is not configured.')

            has_best_model_path = self.trainer.checkpoint_callback.best_model_path
            if hasattr(self.trainer.checkpoint_callback, "best_model_path") and not has_best_model_path:
                if self.trainer.fast_dev_run:
                    raise ValueError(
                        f'You cannot execute `.{fn}(ckpt_path="best")` with `fast_dev_run=True`.'
                        f" Please pass an exact checkpoint path to `.{fn}(ckpt_path=...)`"
                    )
                raise ValueError(
                    f'`.{fn}(ckpt_path="best")` is set but `ModelCheckpoint` is not configured to save the best model.'
                )
            # load best weights
            ckpt_path = getattr(self.trainer.checkpoint_callback, "best_model_path", None)

        elif ckpt_path == "last":
            candidates = {getattr(ft, "ckpt_path", None) for ft in ft_checkpoints}
            for callback in self.trainer.checkpoint_callbacks:
                if isinstance(callback, ModelCheckpoint):

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Pass the explicit checkpoint path: trainer.test(ckpt_path="/path/to/best.ckpt")
  2. Add a ModelCheckpoint callback and keep enable_checkpointing=True during fit, then use ckpt_path="best"
  3. Load weights into the model manually (model = MyModel.load_from_checkpoint(path)) and call trainer.test(model) with ckpt_path=None

Example fix

# before
trainer = Trainer(enable_checkpointing=False)
trainer.fit(model)
trainer.test(model, ckpt_path="best")
# after
trainer = Trainer(callbacks=[ModelCheckpoint(monitor="val_loss", save_top_k=1)])
trainer.fit(model)
trainer.test(model, ckpt_path="best")
Defensive patterns

Strategy: validation

Validate before calling

mode = "test"
if ckpt_path == "best":
    assert trainer.checkpoint_callback is not None, "configure ModelCheckpoint before using ckpt_path='best'"

Try / catch

try:
    trainer.test(model, ckpt_path="best")
except ValueError as e:
    if "not configured" in str(e):
        trainer.test(model, ckpt_path=explicit_path)
    else:
        raise

Prevention

When it happens

Trigger: trainer.validate(ckpt_path="best") or trainer.test(ckpt_path="best") / .predict(ckpt_path="best") on a Trainer built with enable_checkpointing=False and no ModelCheckpoint in callbacks.

Common situations: Running evaluation-only workflows where the user assumed the best checkpoint is tracked automatically; disabling checkpointing for the fit run and then asking for the best weights; separating fit and eval into different Trainer instances without passing a path.

Related errors


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