{"record":{"id":"a6ad2d4102dac681","repo":"Lightning-AI/pytorch-lightning","slug":"filter-should-be-a-dictionary-given-filter-r","errorCode":null,"errorMessage":"Filter should be a dictionary, given {filter!r}","messagePattern":"Filter should be a dictionary, given (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/fabric.py","lineNumber":858,"sourceCode":"        Raises:\n            TypeError: If filter is not a dictionary or contains non-callable values.\n            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]:","sourceCodeStart":840,"sourceCodeEnd":876,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/fabric.py#L840-L876","documentation":"fabric.save(path, state, filter=...) expects `filter` to be a dictionary mapping state keys to callables (predicate functions applied per entry). Any other type (a bare lambda, a list, a string) fails the isinstance(filter, dict) check with TypeError.","triggerScenarios":"Calling fabric.save('ckpt.pth', state, filter=lambda k, v: ...) — passing a bare callable instead of {'key': callable}; or passing a list of key names thinking it selects keys.","commonSituations":"Selective checkpointing of model weights but not optimizer state; misunderstanding the filter API shape from the docstring example.","solutions":["Wrap the callable(s) in a dict keyed by state keys: filter={'model': lambda k, v: 'optimizer' not in k}","Ensure every filter key exists in `state` (the next check enforces subset)"],"exampleFix":"# before\nfabric.save('ckpt.pth', state, filter=lambda k, v: 'optimizer' not in k)\n# after\nfabric.save('ckpt.pth', state, filter={'model': lambda k, v: 'optimizer' not in k})","handlingStrategy":"type-guard","validationCode":"assert filter is None or isinstance(filter, dict), 'filter must be a dict of {state_key: callable}'","typeGuard":"def is_valid_filter(f):\n    return f is None or isinstance(f, dict)","tryCatchPattern":null,"preventionTips":["Remember filter is a dict of {state_key: predicate}, not a bare callable or list"],"tags":["lightning","fabric","checkpointing","type-validation"],"backgroundTag":"invalid-argument-type","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}