reflex-dev/reflex · error · TypeError

Var of type {self._var_type} does not support item access.

Error message

Var of type {self._var_type} does not support item access.

What it means

Subscripting a Reflex Var (var[key]) compiles to JS item access, which requires the Var's Python type to support __getitem__ (dict, list, tuple, etc.). If _var_type is a type without item access, Reflex raises this TypeError at Python render time. Untyped (Any) vars raise UntypedVarError instead.

Source

Thrown at packages/reflex-base/src/reflex_base/vars/base.py:1455

        def __getitem__(self, key: Any) -> Var:
            """Get the item from the var.

            Args:
                key: The key to get.

            Raises:
                UntypedVarError: If the var type is Any.
                TypeError: If the var type is Any.

            # noqa: DAR101 self
            """
            if self._var_type is Any:
                raise exceptions.UntypedVarError(
                    self,
                    f"access the item '{key}'",
                )
            msg = f"Var of type {self._var_type} does not support item access."
            raise TypeError(msg)

        def __getattr__(self, name: str):
            """Get an attribute of the var.

            Args:
                name: The name of the attribute.

            Raises:
                VarAttributeError: If the attribute does not exist.
                UntypedVarError: If the var type is Any.
                TypeError: If the var type is Any.

            # noqa: DAR101 self
            """
            if name.startswith("_"):
                msg = f"Attribute {name} not found."
                raise VarAttributeError(msg)

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Fix the state annotation to a subscriptable type (dict[str, int], list[str], etc.) matching how you use it
  2. If the var genuinely is a container at runtime, keep the annotation accurate — never annotate it as the scalar type
  3. Use attribute access (var.attr) instead of item access for objects/dataclasses
  4. For Any-typed vars, annotate properly or use .to(dict) before indexing

Example fix

# before
class State(rx.State):
    config: str
rx.text(State.config['key'])
# after
class State(rx.State):
    config: dict[str, str]
rx.text(State.config['key'])
Defensive patterns

Strategy: type-guard

Validate before calling

cls = State.__annotations__['items']
assert hasattr(cls, '__getitem__') or cls in (list, dict, tuple, str), f'{cls} is not subscriptable'

Type guard

def supports_item_access(var_type: object) -> bool:
    return isinstance(var_type, type) and (var_type in (list, dict, tuple, str) or hasattr(var_type, '__getitem__'))

Prevention

When it happens

Trigger: Writing state.items[0] or state.config['key'] where the state attribute is annotated as int, str (py<3.9 semantics differ), a dataclass, or any non-subscriptable type; annotating as a bare class then indexing in the UI.

Common situations: Annotating a field as the wrong type (e.g. str instead of dict) then indexing it; forgetting that Reflex type-checks attribute operations against the annotation; refactors that changed a field from dict to a custom object while the template still indexes it.

Related errors


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