{"record":{"id":"11c51f6a714f4810","repo":"Lightning-AI/pytorch-lightning","slug":"the-filter-keys-filter-keys-state-are-not-pr","errorCode":null,"errorMessage":"The filter keys {filter.keys() - state} are not present in the state keys {set(state)}.","messagePattern":"The filter keys (.+?) are not present in the state keys (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/fabric.py","lineNumber":860,"sourceCode":"            ValueError: If filter keys don't match state keys.\n\n        Example::\n\n            state = {\"model\": model, \"optimizer\": optimizer, \"epoch\": epoch}\n            fabric.save(\"checkpoint.pth\", state)\n\n            # With filter\n            def param_filter(name, param):\n                return \"bias\" not in name  # Save only non-bias parameters\n\n            fabric.save(\"checkpoint.pth\", state, filter={\"model\": param_filter})\n\n        \"\"\"\n        if filter is not None:\n            if not isinstance(filter, dict):\n                raise TypeError(f\"Filter should be a dictionary, given {filter!r}\")\n            if not set(filter).issubset(state):\n                raise ValueError(\n                    f\"The filter keys {filter.keys() - state} are not present in the state keys {set(state)}.\"\n                )\n            for k, v in filter.items():\n                if not callable(v):\n                    raise TypeError(f\"Expected `fabric.save(filter=...)` for key {k!r} to be a callable, given {v!r}\")\n        self._strategy.save_checkpoint(path=path, state=_unwrap_objects(state), filter=filter)\n        self.barrier()\n\n    def load(\n        self,\n        path: Union[str, Path],\n        state: Optional[dict[str, Union[nn.Module, Optimizer, Any]]] = None,\n        strict: bool = True,\n        *,\n        weights_only: Optional[bool] = None,\n    ) -> dict[str, Any]:\n        \"\"\"Load a checkpoint from a file and restore the state of objects (modules, optimizers, etc.).\n","sourceCodeStart":842,"sourceCodeEnd":878,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/fabric.py#L842-L878","documentation":"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.","triggerScenarios":"fabric.save(path, {'model': model}, filter={'optimizer': fn}) — filtering on a state key that wasn't included in the state dict passed to save.","commonSituations":"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.","solutions":["Align filter keys with the state dict you pass: only filter keys that are present","Add the missing entry to state before saving, or drop the stale filter key"],"exampleFix":"# before\nfabric.save('ckpt.pth', {'model': model}, filter={'optimizer': keep_fn})\n# after\nfabric.save('ckpt.pth', {'model': model}, filter={'model': keep_fn})","handlingStrategy":"validation","validationCode":"missing = set(filter or {}) - set(state)\nassert not missing, f'filter keys not in state: {missing}'\nfabric.save(path, state, filter=filter)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Generate filter keys from state.keys() at build time instead of hardcoding"],"tags":["lightning","fabric","checkpointing","key-mismatch"],"backgroundTag":"dict-key-mismatch","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}