reflex-dev/reflex · error · TypeError
Cannot pass a Var to a built-in function. Consider using .le
Error message
Cannot pass a Var to a built-in function. Consider using .length() for accessing the length of an iterable Var.
What it means
This TypeError is raised by reflex.compiler.compiler.into_component when rendering logic calls a Python built-in (typically len()) on a Var-backed value (ArrayCastedVar/ObjectCastedVar/StringCastedVar). Reflex Vars represent frontend state that only exists at runtime in JavaScript, so Python built-ins cannot operate on them during compilation. The error is a re-raise of the underlying 'has no len()' TypeError with a helpful message.
Source
Thrown at reflex/compiler/compiler.py:935
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((
"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
View on GitHub (pinned to 45b8ed5ab7)
Solutions
- Replace len(var) with var.length(), e.g. rx.text(State.items.length())
- For emptiness checks use var._bool() / boolean operations or var.length() > 0 via Var comparisons
- Compute the length in an event handler on the backend and store it as a separate state field
Example fix
// before
rx.text(f"You have {len(State.items)} items")
// after
rx.text("You have ", State.items.length(), " items") Defensive patterns
Strategy: type-guard
Validate before calling
def safe_len(v):
return v.length() if isinstance(v, rx.Var) or hasattr(v, "length") else len(v) Type guard
def is_var(v) -> bool:
return isinstance(v, rx.Var) or type(v).__name__.endswith("CastedVar") Prevention
- Never call len() on State attributes in render functions
- Search codebase for len(State. before adding pages
- Use .length() for any iterable Var
When it happens
Trigger: Calling len(State.some_list), len(State.some_string), or len(State.some_dict) inside a component render function, where the attribute is a Var. Any built-in function applied to an ArrayCastedVar, ObjectCastedVar, or StringCastedVar during component conversion triggers it.
Common situations: Rendering 'You have N items' with len(State.items), conditioning display on len(State.text) > 0, or migrating code from Python state objects to Reflex state vars without adjusting length checks.
Related errors
- Expected _js_expr to be a string, got value {self._js_expr!r
- Unsupported type {var_type} for guess_type.
- Cannot index into a primitive sequence with a Var. Consider
- Cannot pass a Var to a built-in function. Consider moving th
- The component `{comp_name}` only allows the components: {val
AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28).
Data as JSON: /api/errors/b0db5f4ea39ef336.
Report an issue: GitHub.