{"record":{"id":"c30d3f69781f3c53","repo":"reflex-dev/reflex","slug":"cannot-access-a-primitive-map-with-a-var-consider","errorCode":null,"errorMessage":"Cannot access a primitive map with a Var. Consider calling rx.Var.create() on the map.","messagePattern":"Cannot access a primitive map with a Var\\. Consider calling rx\\.Var\\.create\\(\\) on the map\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"reflex/compiler/compiler.py","lineNumber":920,"sourceCode":"        TypeError: If the component is not a Component.\n\n    # noqa: DAR401\n    \"\"\"\n    if (converted := _into_component_once(component)) is not None:\n        return converted\n    if not callable(component):\n        msg = f\"Expected a Component or callable, got {component!r} of type {type(component)}\"\n        raise TypeError(msg)\n\n    try:\n        component_called = component()\n    except KeyError as e:\n        if isinstance(e, ReflexError):\n            _modify_exception(e)\n            raise\n        key = e.args[0] if e.args else None\n        if key is not None and isinstance(key, Var):\n            raise TypeError(\n                \"Cannot access a primitive map with a Var. Consider calling rx.Var.create() on the map.\"\n            ).with_traceback(e.__traceback__) from None\n        raise\n    except TypeError as e:\n        if isinstance(e, ReflexError):\n            _modify_exception(e)\n            raise\n        message = e.args[0] if e.args else None\n        if message and isinstance(message, str):\n            if message.endswith(\"has no len()\") and (\n                \"ArrayCastedVar\" in message\n                or \"ObjectCastedVar\" in message\n                or \"StringCastedVar\" in message\n            ):\n                raise TypeError(\n                    \"Cannot pass a Var to a built-in function. Consider using .length() for accessing the length of an iterable Var.\"\n                ).with_traceback(e.__traceback__) from None\n            if message.endswith((","sourceCodeStart":902,"sourceCodeEnd":938,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/reflex/compiler/compiler.py#L902-L938","documentation":"When a callable page/render function executes, a KeyError escaping from rendering usually means a plain dict was indexed with a Var key (d[Var('k')] instead of d['k']). Reflex intercepts the KeyError and rewrites it into this TypeError advising rx.Var.create() so the map becomes a reactive Var-backed mapping instead of a Python dict lookup that cannot work at render time.","triggerScenarios":"Inside a component function, indexing a plain dict with a state Var: data[State.key]. Python attempts hash(Var) which the render path turns into a missing key, raising KeyError with a Var key, which into_component converts. Also triggered by any user code that raises KeyError whose arg is a Var while the result is being converted to a component.","commonSituations":"Trying to make reactive lookups into static dicts/lists; migrating from f-string interpolation to Var-based data access; accessing translation maps or config dicts with a Var key.","solutions":["Wrap the mapping with rx.Var.create(...) so it becomes a Var supporting Var-keyed access","Use rx.cond() or computed Vars (in state) to perform the lookup reactively instead of dict indexing at render time","If the key is static, use a plain string key, not a Var"],"exampleFix":"# before\ndef index():\n    return rx.text(data[State.key])  # TypeError: Cannot access a primitive map with a Var\n# after\nfrom reflex import Var\nreactive_data = Var.create(data)\ndef index():\n    return rx.text(reactive_data[State.key])","handlingStrategy":"type-guard","validationCode":"from reflex import Var\n\ndef reactive_map(d: dict) -> Var:\n    return Var.create(d)","typeGuard":"from reflex import Var\n\ndef is_var(value) -> bool:\n    return isinstance(value, Var)","tryCatchPattern":"try:\n    node = rx.text(data[State.key])\nexcept TypeError as e:\n    if \"primitive map\" in str(e):\n        node = rx.text(Var.create(data)[State.key])\n    else:\n        raise","preventionTips":["Never index plain dicts with a Var key inside render functions","Move dynamic lookups into computed Vars in your State","Wrap static maps with rx.Var.create() before Var-keyed access"],"tags":["reflex","vars","reactivity","dict-access"],"backgroundTag":"reactive-dict-access-with-var","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}