Lightning-AI/pytorch-lightning · error · ValueError

You set `.load_from_checkpoint(..., strict={strict!r})` whic

Error message

You set `.load_from_checkpoint(..., strict={strict!r})` which is in conflict with `{cls.__name__}.strict_loading={obj.strict_loading!r}. Please set the same value for both of them.

What it means

Raised when the strict argument passed to load_from_checkpoint conflicts with the strict_loading attribute set on the LightningModule (__init__ strict_loading=...). Lightning requires both values to agree (or one to be None) to avoid ambiguity about whether load_state_dict is strict.

Source

Thrown at src/lightning/pytorch/core/saving.py:187

                " add the path to `lightning.pytorch.core.saving._ALLOWED_INSTANTIATORS` before loading."
            )
        module_path, name = instantiator_path.rsplit(".", 1)
        instantiator = getattr(__import__(module_path, fromlist=[name]), name)

    if not cls_spec.varkw:
        # filter kwargs according to class init unless it allows any argument via kwargs
        _cls_kwargs = {k: v for k, v in _cls_kwargs.items() if k in cls_init_args_name}

    obj = instantiator(cls, _cls_kwargs) if instantiator else cls(**_cls_kwargs)

    if isinstance(obj, pl.LightningDataModule):
        if obj.__class__.__qualname__ in checkpoint:
            obj.load_state_dict(checkpoint[obj.__class__.__qualname__])
        return obj

    if isinstance(obj, pl.LightningModule):
        if obj._strict_loading is not None and strict is not None and strict != obj.strict_loading:
            raise ValueError(
                f"You set `.load_from_checkpoint(..., strict={strict!r})` which is in conflict with"
                f" `{cls.__name__}.strict_loading={obj.strict_loading!r}. Please set the same value for both of them."
            )
        strict = obj.strict_loading if strict is None else strict

        if is_overridden("configure_model", obj):
            obj.configure_model()

        # give model a chance to load something
        obj.on_load_checkpoint(checkpoint)

    # load the state_dict on the model automatically
    keys = obj.load_state_dict(checkpoint["state_dict"], strict=strict)  # type: ignore[arg-type]

    if not strict:
        if keys.missing_keys:
            rank_zero_warn(
                f"Found keys that are in the model state dict but not in the checkpoint: {keys.missing_keys}"

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Pass the same value: either omit strict in load_from_checkpoint (module default wins) or match it
  2. Or change the module's strict_loading constructor arg to match the desired call-site value

Example fix

// before
model = MyModel.load_from_checkpoint(ckpt, strict=True)  # module has strict_loading=False
// after
model = MyModel.load_from_checkpoint(ckpt)  # uses module's strict_loading
Defensive patterns

Strategy: validation

Validate before calling

strict = None  # simplest: let the module's strict_loading decide
# or explicitly reconcile:
# strict = model_kw_strict if model_kw_strict == cls_strict_loading else None

Prevention

When it happens

Trigger: Defining class MyModel(pl.LightningModule) with strict_loading=False and then calling MyModel.load_from_checkpoint(ckpt, strict=True), or any combination where both are non-None and differ.

Common situations: A module author pins strict_loading for all users; a user of the class passes the familiar strict= kwarg from older Lightning versions with a different value, causing the conflict.

Related errors


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