{"record":{"id":"ffba1935fee8083c","repo":"reflex-dev/reflex","slug":"expected-js-expr-to-be-a-string-got-value-self","errorCode":null,"errorMessage":"Expected _js_expr to be a string, got value {self._js_expr!r} of type {type(self._js_expr).__name__}","messagePattern":"Expected _js_expr to be a string, got value (.+?) of type (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/reflex-base/src/reflex_base/vars/base.py","lineNumber":692,"sourceCode":"            ToVarOperation.__qualname__ = (\n                ToVarOperation.__qualname__.removesuffix(ToVarOperation.__name__)\n                + new_to_var_operation_name\n            )\n            ToVarOperation.__name__ = new_to_var_operation_name\n\n            _register_var_subclass_entry(\n                VarSubclassEntry(cls, ToVarOperation, python_types)\n            )\n\n    def __post_init__(self):\n        \"\"\"Post-initialize the var.\n\n        Raises:\n            TypeError: If _js_expr is not a string.\n        \"\"\"\n        if not isinstance(self._js_expr, str):\n            msg = f\"Expected _js_expr to be a string, got value {self._js_expr!r} of type {type(self._js_expr).__name__}\"\n            raise TypeError(msg)\n\n        if self._var_data is not None and not isinstance(self._var_data, VarData):\n            msg = f\"Expected _var_data to be a VarData, got value {self._var_data!r} of type {type(self._var_data).__name__}\"\n            raise TypeError(msg)\n\n        # Decode any inline Var markup and apply it to the instance\n        var_data_, js_expr_ = _decode_var_immutable(self._js_expr)\n\n        if var_data_ or js_expr_ != self._js_expr:\n            self.__init__(\n                _js_expr=js_expr_,\n                _var_type=self._var_type,\n                _var_data=VarData.merge(self._var_data, var_data_),\n            )\n\n    def __hash__(self) -> int:\n        \"\"\"Define a hash function for the var.\n","sourceCodeStart":674,"sourceCodeEnd":710,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/vars/base.py#L674-L710","documentation":"Var is a dataclass whose _js_expr field must be a string containing the JavaScript expression it represents. __post_init__ validates this and raises TypeError otherwise. This variant of the message fires when the raw _js_expr argument itself is not a str (a companion check at line 696 covers _var_data). Constructing a Var directly with a non-string expression is therefore a programming error.","triggerScenarios":"Calling Var(123), Var(None), Var(some_fstring_object) directly instead of going through the public helpers (Var.create / .to(...) / operator overloads); passing a decoded Var object or bytes where the internal _js_expr string is expected; deserializing/pickling Vars whose markup decoding yields a non-string.","commonSituations":"Framework/advanced users constructing Var subclasses manually; copy-pasting internal Var code; version upgrades where internal Var storage changed from object to strictly str; bugs in code that f-strings Var internals.","solutions":["Don't construct Var directly — use Var.create(value) or existing var operations which build _js_expr for you","If you must construct it, ensure the first positional argument is the JS expression as a string: Var('state.count')","If it comes from serialization, re-encode with the public Var API instead of raw dataclass fields","Check for stale code compiled against an older reflex Var layout and update to Var.create"],"exampleFix":"# before\nv = Var(42)\n# after\nv = Var.create(42)","handlingStrategy":"type-guard","validationCode":"from reflex.vars import Var\nassert isinstance(expr, str)\nv = Var(expr)","typeGuard":"def is_valid_js_expr(value: object) -> bool:\n    return isinstance(value, str)","tryCatchPattern":"try:\n    v = Var(expr)\nexcept TypeError as e:\n    v = Var.create(expr)  # fall back to public factory","preventionTips":["Never construct Var directly; use Var.create","Treat _js_expr/_var_data as private internals"],"tags":["reflex","var","typeerror","internal-api"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}