{"record":{"id":"00c692728b81f7c6","repo":"reflex-dev/reflex","slug":"expected-a-mapping-type-or-a-dataclass-got-var","errorCode":null,"errorMessage":"Expected a mapping type or a dataclass, got {_var_value!r} of type {type(_var_value).__name__}.","messagePattern":"Expected a mapping type or a dataclass, got (.+?) of type (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/reflex-base/src/reflex_base/vars/object.py","lineNumber":528,"sourceCode":"\n        Args:\n            _var_value: The value of the var.\n            _var_type: The type of the var.\n            _var_data: Additional hooks and imports associated with the Var.\n\n        Returns:\n            The literal object var.\n\n        Raises:\n            TypeError: If the value is not a mapping type or a dataclass.\n        \"\"\"\n        if not isinstance(_var_value, collections.abc.Mapping):\n            from reflex_base.utils.serializers import serialize\n\n            serialized = serialize(_var_value, get_type=False)\n            if not isinstance(serialized, collections.abc.Mapping):\n                msg = f\"Expected a mapping type or a dataclass, got {_var_value!r} of type {type(_var_value).__name__}.\"\n                raise TypeError(msg)\n\n            return LiteralObjectVar(\n                _js_expr=\"\",\n                _var_type=(type(_var_value) if _var_type is None else _var_type),\n                _var_data=_var_data,\n                _var_value=serialized,\n            )\n\n        return LiteralObjectVar(\n            _js_expr=\"\",\n            _var_type=(figure_out_type(_var_value) if _var_type is None else _var_type),\n            _var_data=_var_data,\n            _var_value=_var_value,\n        )\n\n\n@var_operation\ndef object_keys_operation(value: ObjectVar):","sourceCodeStart":510,"sourceCodeEnd":546,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/vars/object.py#L510-L546","documentation":"LiteralObjectVar.create accepts only Mapping types (or dataclasses, which are serialized to a mapping). If the passed value is neither a mapping nor serializable into one (the serializer returns a non-mapping), TypeError is raised with the actual type name.","triggerScenarios":"Passing a list, set, string, or arbitrary object where an object var is expected — e.g. rx.Var literal creation from a non-dict, merge()/__getitem__/__getattr__ operations landing on a non-mapping value.","commonSituations":"Annotating a state var as dict but assigning a list; computed vars returning the wrong shape; passing a pydantic model whose custom serializer returns a scalar; refactors that change a value's type without updating the annotation.","solutions":["Pass an actual dict/Mapping (or dataclass) to the object var API.","Fix the state annotation and stored value to agree (dict field holds dicts).","Register a serializer that converts your custom type to a dict."],"exampleFix":"# before\nclass State(rx.State):\n    config: dict = [\"a\", \"b\"]  # list assigned to dict var\n\n# after\nclass State(rx.State):\n    config: dict = {\"items\": [\"a\", \"b\"]}","handlingStrategy":"type-guard","validationCode":"import collections.abc as cabc\n\ndef is_mapping(v) -> bool:\n    return isinstance(v, cabc.Mapping) or hasattr(v, \"__dataclass_fields__\")","typeGuard":"import collections.abc as cabc\n\ndef is_mapping_or_dataclass(v) -> bool:\n    return isinstance(v, cabc.Mapping) or hasattr(v, \"__dataclass_fields__\")","tryCatchPattern":null,"preventionTips":["Match state var annotations with the actual values assigned.","Initialize dict vars with {} not [].","Add a startup assertion for the shape of default state values."],"tags":["reflex","objectvar","type-error","mapping"],"backgroundTag":"var-type-annotation-mismatch","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}