{"record":{"id":"fdd32e8da07cc0c9","repo":"reflex-dev/reflex","slug":"cannot-index-into-a-primitive-sequence-with-a-var","errorCode":null,"errorMessage":"Cannot index into a primitive sequence with a Var. Consider calling rx.Var.create() on the sequence.","messagePattern":"Cannot index into a primitive sequence with a Var\\. Consider calling rx\\.Var\\.create\\(\\) on the sequence\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"reflex/compiler/compiler.py","lineNumber":942,"sourceCode":"    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((\n                \"indices must be integers or slices, not NumberCastedVar\",\n                \"indices must be integers or slices, not BooleanCastedVar\",\n            )):\n                raise TypeError(\n                    \"Cannot index into a primitive sequence with a Var. Consider calling rx.Var.create() on the sequence.\"\n                ).with_traceback(e.__traceback__) from None\n        if \"CastedVar\" in str(e):\n            raise TypeError(\n                \"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.\"\n            ).with_traceback(e.__traceback__) from None\n        raise\n    except ReflexError as e:\n        _modify_exception(e)\n        raise\n\n    if (converted := _into_component_once(component_called)) is not None:\n        return converted\n\n    msg = f\"Expected a Component, got {component_called!r} of type {type(component_called)}\"\n    raise TypeError(msg)\n\n","sourceCodeStart":924,"sourceCodeEnd":960,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/reflex/compiler/compiler.py#L924-L960","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Wrap the sequence in rx.Var.create(my_list) (often at module level) so indexing with a Var compiles to JS","Move the lookup into an event handler / computed var on the backend and reference the resulting state field","Use a Reflex-compatible structure (rx.Var mapping or dict Var) for lookups"],"exampleFix":"# before\nCOLORS = [\"red\", \"green\", \"blue\"]\nrx.text(COLORS[State.index])\n# after\nCOLORS = rx.Var.create([\"red\", \"green\", \"blue\"])\nrx.text(COLORS[State.index])","handlingStrategy":"type-guard","validationCode":"LOOKUPS = rx.Var.create([\"a\", \"b\", \"c\"])  # wrap once at module level","typeGuard":"def is_var(v) -> bool:\n    return isinstance(v, rx.Var) or type(v).__name__.endswith(\"CastedVar\")","tryCatchPattern":null,"preventionTips":["Wrap constant lookup tables in rx.Var.create at module level","Never index a plain Python list with a state Var","Keep lookup logic in computed vars when unsure"],"tags":["reflex","var","indexing","typeerror"],"backgroundTag":"frontend-var-used-as-python-value","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}