Lightning-AI/pytorch-lightning · error · ValueError

The filter keys {filter.keys() - state} are not present in t

Error message

The filter keys {filter.keys() - state} are not present in the state keys {set(state)}.

What it means

When a filter dict is given to fabric.save(), every filter key must correspond to an existing key in `state`. The check `set(filter).issubset(state)` fails when a filter references e.g. 'optimizer' while state only contains 'model', and the error reports the missing keys via set difference.

Source

Thrown at src/lightning/fabric/fabric.py:860

            ValueError: If filter keys don't match state keys.

        Example::

            state = {"model": model, "optimizer": optimizer, "epoch": epoch}
            fabric.save("checkpoint.pth", state)

            # With filter
            def param_filter(name, param):
                return "bias" not in name  # Save only non-bias parameters

            fabric.save("checkpoint.pth", state, filter={"model": param_filter})

        """
        if filter is not None:
            if not isinstance(filter, dict):
                raise TypeError(f"Filter should be a dictionary, given {filter!r}")
            if not set(filter).issubset(state):
                raise ValueError(
                    f"The filter keys {filter.keys() - state} are not present in the state keys {set(state)}."
                )
            for k, v in filter.items():
                if not callable(v):
                    raise TypeError(f"Expected `fabric.save(filter=...)` for key {k!r} to be a callable, given {v!r}")
        self._strategy.save_checkpoint(path=path, state=_unwrap_objects(state), filter=filter)
        self.barrier()

    def load(
        self,
        path: Union[str, Path],
        state: Optional[dict[str, Union[nn.Module, Optimizer, Any]]] = None,
        strict: bool = True,
        *,
        weights_only: Optional[bool] = None,
    ) -> dict[str, Any]:
        """Load a checkpoint from a file and restore the state of objects (modules, optimizers, etc.).

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Align filter keys with the state dict you pass: only filter keys that are present
  2. Add the missing entry to state before saving, or drop the stale filter key

Example fix

# before
fabric.save('ckpt.pth', {'model': model}, filter={'optimizer': keep_fn})
# after
fabric.save('ckpt.pth', {'model': model}, filter={'model': keep_fn})
Defensive patterns

Strategy: validation

Validate before calling

missing = set(filter or {}) - set(state)
assert not missing, f'filter keys not in state: {missing}'
fabric.save(path, state, filter=filter)

Prevention

When it happens

Trigger: fabric.save(path, {'model': model}, filter={'optimizer': fn}) — filtering on a state key that wasn't included in the state dict passed to save.

Common situations: Saving a minimal state (model only) while reusing a filter written for the full training state (model + optimizer); renaming state keys without updating the filter.

Related errors


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