reflex-dev/reflex · error · TypeError

Cannot access a primitive map with a Var. Consider calling r

Error message

Cannot access a primitive map with a Var. Consider calling rx.Var.create() on the map.

What it means

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.

Source

Thrown at reflex/compiler/compiler.py:920

        TypeError: If the component is not a Component.

    # noqa: DAR401
    """
    if (converted := _into_component_once(component)) is not None:
        return converted
    if not callable(component):
        msg = f"Expected a Component or callable, got {component!r} of type {type(component)}"
        raise TypeError(msg)

    try:
        component_called = component()
    except KeyError as e:
        if isinstance(e, ReflexError):
            _modify_exception(e)
            raise
        key = e.args[0] if e.args else None
        if key is not None and isinstance(key, Var):
            raise TypeError(
                "Cannot access a primitive map with a Var. Consider calling rx.Var.create() on the map."
            ).with_traceback(e.__traceback__) from None
        raise
    except TypeError as e:
        if isinstance(e, ReflexError):
            _modify_exception(e)
            raise
        message = e.args[0] if e.args else None
        if message and isinstance(message, str):
            if message.endswith("has no len()") and (
                "ArrayCastedVar" in message
                or "ObjectCastedVar" in message
                or "StringCastedVar" in message
            ):
                raise TypeError(
                    "Cannot pass a Var to a built-in function. Consider using .length() for accessing the length of an iterable Var."
                ).with_traceback(e.__traceback__) from None
            if message.endswith((

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Wrap the mapping with rx.Var.create(...) so it becomes a Var supporting Var-keyed access
  2. Use rx.cond() or computed Vars (in state) to perform the lookup reactively instead of dict indexing at render time
  3. If the key is static, use a plain string key, not a Var

Example fix

# before
def index():
    return rx.text(data[State.key])  # TypeError: Cannot access a primitive map with a Var
# after
from reflex import Var
reactive_data = Var.create(data)
def index():
    return rx.text(reactive_data[State.key])
Defensive patterns

Strategy: type-guard

Validate before calling

from reflex import Var

def reactive_map(d: dict) -> Var:
    return Var.create(d)

Type guard

from reflex import Var

def is_var(value) -> bool:
    return isinstance(value, Var)

Try / catch

try:
    node = rx.text(data[State.key])
except TypeError as e:
    if "primitive map" in str(e):
        node = rx.text(Var.create(data)[State.key])
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28). Data as JSON: /api/errors/c30d3f69781f3c53. Report an issue: GitHub.