{"record":{"id":"b0db5f4ea39ef336","repo":"reflex-dev/reflex","slug":"cannot-pass-a-var-to-a-built-in-function-consider","errorCode":null,"errorMessage":"Cannot pass a Var to a built-in function. Consider using .length() for accessing the length of an iterable Var.","messagePattern":"Cannot pass a Var to a built-in function\\. Consider using \\.length\\(\\) for accessing the length of an iterable Var\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"reflex/compiler/compiler.py","lineNumber":935,"sourceCode":"            raise\n        key = e.args[0] if e.args else None\n        if key is not None and isinstance(key, Var):\n            raise TypeError(\n                \"Cannot access a primitive map with a Var. Consider calling rx.Var.create() on the map.\"\n            ).with_traceback(e.__traceback__) from None\n        raise\n    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","sourceCodeStart":917,"sourceCodeEnd":953,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/reflex/compiler/compiler.py#L917-L953","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nrx.text(f\"You have {len(State.items)} items\")\n// after\nrx.text(\"You have \", State.items.length(), \" items\")","handlingStrategy":"type-guard","validationCode":"def safe_len(v):\n    return v.length() if isinstance(v, rx.Var) or hasattr(v, \"length\") else len(v)","typeGuard":"def is_var(v) -> bool:\n    return isinstance(v, rx.Var) or type(v).__name__.endswith(\"CastedVar\")","tryCatchPattern":null,"preventionTips":["Never call len() on State attributes in render functions","Search codebase for len(State. before adding pages","Use .length() for any iterable Var"],"tags":["reflex","var","frontend-state","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"}