reflex-dev/reflex · error · TypeError
Cannot pass a Var to a built-in function. Consider moving th
Error message
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.
What it means
A catch-all TypeError from into_component: any remaining exception whose message mentions 'CastedVar' is re-raised with guidance. It means a Python built-in or operator was applied to a Var (the CastedVar wrappers Reflex uses when a Var flows through Python operations), and Reflex has no frontend equivalent for that operation.
Source
Thrown at reflex/compiler/compiler.py:946
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)
def compile_unevaluated_page(
route: str,
page: UnevaluatedPage,
style: ComponentStyle | None = None,View on GitHub (pinned to 45b8ed5ab7)
Solutions
- Use Reflex Var operations (var.to_string(), var.to_int(), var.length(), etc.) instead of built-ins
- Move the computation into a backend event handler or computed var and reference the result
- Define a custom Var operation (VarOperation) if the transform must run on the frontend
Example fix
# before rx.text(str(State.count)) # after rx.text(State.count.to_string())
Defensive patterns
Strategy: type-guard
Type guard
def is_var(v) -> bool:
return isinstance(v, rx.Var) or type(v).__name__.endswith("CastedVar") Try / catch
try:
result = transform(value)
except TypeError as e:
if "CastedVar" in str(e):
# move transform into a computed var / event handler
...
raise Prevention
- Only use documented Var methods (.to_string(), .to_int(), .length()) on Vars
- Pass Vars only to Reflex-aware helpers
- Keep data transformations in event handlers or computed vars
When it happens
Trigger: Applying built-ins or unsupported operations to Vars: str(var), int(var), min/max(var, ...), var.lower() on a non-string Var, or any function that internally calls a dunder on a CastedVar during component rendering.
Common situations: Formatting Vars with f-strings that call implicit conversion, normalizing data with str()/int()/abs() in render code, or passing Vars into third-party helper functions that expect plain Python values.
Related errors
- Expected _js_expr to be a string, got value {self._js_expr!r
- Unsupported type {var_type} for guess_type.
- Cannot pass a Var to a built-in function. Consider using .le
- Cannot index into a primitive sequence with a Var. Consider
- 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/80e73b6d7c7b0f4b.
Report an issue: GitHub.