{"record":{"id":"80e73b6d7c7b0f4b","repo":"reflex-dev/reflex","slug":"cannot-pass-a-var-to-a-built-in-function-consider-80e73b","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"reflex/compiler/compiler.py","lineNumber":946,"sourceCode":"        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\ndef compile_unevaluated_page(\n    route: str,\n    page: UnevaluatedPage,\n    style: ComponentStyle | None = None,","sourceCodeStart":928,"sourceCodeEnd":964,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/reflex/compiler/compiler.py#L928-L964","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nrx.text(str(State.count))\n# after\nrx.text(State.count.to_string())","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"def is_var(v) -> bool:\n    return isinstance(v, rx.Var) or type(v).__name__.endswith(\"CastedVar\")","tryCatchPattern":"try:\n    result = transform(value)\nexcept TypeError as e:\n    if \"CastedVar\" in str(e):\n        # move transform into a computed var / event handler\n        ...\n    raise","preventionTips":["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"],"tags":["reflex","var","builtin","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"}