reflex-dev/reflex · error · TypeError

Cannot index into a primitive sequence with a Var. Consider

Error message

Cannot index into a primitive sequence with a Var. Consider calling rx.Var.create() on the sequence.

What it means

Raised by into_component when render code indexes a plain Python sequence (list/tuple/str) with a Var, producing 'indices must be integers or slices, not NumberCastedVar/BooleanCastedVar'. Because the sequence is a Python object, Reflex cannot generate a dynamic JavaScript index into it; it must be wrapped in rx.Var.create() so it becomes a serialized Var supporting Var indexing.

Source

Thrown at reflex/compiler/compiler.py:942

    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((
                "indices must be integers or slices, not NumberCastedVar",
                "indices must be integers or slices, not BooleanCastedVar",
            )):
                raise TypeError(
                    "Cannot index into a primitive sequence with a Var. Consider calling rx.Var.create() on the sequence."
                ).with_traceback(e.__traceback__) from None
        if "CastedVar" in str(e):
            raise TypeError(
                "Cannot pass a Var to a built-in function. Consider moving the operation to the backend, using existing Var operations, or defining a custom Var operation."
            ).with_traceback(e.__traceback__) from None
        raise
    except ReflexError as e:
        _modify_exception(e)
        raise

    if (converted := _into_component_once(component_called)) is not None:
        return converted

    msg = f"Expected a Component, got {component_called!r} of type {type(component_called)}"
    raise TypeError(msg)

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Wrap the sequence in rx.Var.create(my_list) (often at module level) so indexing with a Var compiles to JS
  2. Move the lookup into an event handler / computed var on the backend and reference the resulting state field
  3. Use a Reflex-compatible structure (rx.Var mapping or dict Var) for lookups

Example fix

# before
COLORS = ["red", "green", "blue"]
rx.text(COLORS[State.index])
# after
COLORS = rx.Var.create(["red", "green", "blue"])
rx.text(COLORS[State.index])
Defensive patterns

Strategy: type-guard

Validate before calling

LOOKUPS = rx.Var.create(["a", "b", "c"])  # wrap once at module level

Type guard

def is_var(v) -> bool:
    return isinstance(v, rx.Var) or type(v).__name__.endswith("CastedVar")

Prevention

When it happens

Trigger: Writing something like my_python_list[State.index] or my_tuple[State.bool_flag] inside a component, where the sequence is a module-level/constant Python list and the index is a state Var (NumberCastedVar or BooleanCastedVar).

Common situations: Storing lookup tables (e.g. COLORS, WEEKDAYS) as plain lists at module scope and indexing them with a state-driven index in the UI; mixing constant Python data structures with reactive state.

Related errors


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