reflex-dev/reflex · error · TypeError

Expected _js_expr to be a string, got value {self._js_expr!r

Error message

Expected _js_expr to be a string, got value {self._js_expr!r} of type {type(self._js_expr).__name__}

What it means

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.

Source

Thrown at packages/reflex-base/src/reflex_base/vars/base.py:692

            ToVarOperation.__qualname__ = (
                ToVarOperation.__qualname__.removesuffix(ToVarOperation.__name__)
                + new_to_var_operation_name
            )
            ToVarOperation.__name__ = new_to_var_operation_name

            _register_var_subclass_entry(
                VarSubclassEntry(cls, ToVarOperation, python_types)
            )

    def __post_init__(self):
        """Post-initialize the var.

        Raises:
            TypeError: If _js_expr is not a string.
        """
        if not isinstance(self._js_expr, str):
            msg = f"Expected _js_expr to be a string, got value {self._js_expr!r} of type {type(self._js_expr).__name__}"
            raise TypeError(msg)

        if self._var_data is not None and not isinstance(self._var_data, VarData):
            msg = f"Expected _var_data to be a VarData, got value {self._var_data!r} of type {type(self._var_data).__name__}"
            raise TypeError(msg)

        # Decode any inline Var markup and apply it to the instance
        var_data_, js_expr_ = _decode_var_immutable(self._js_expr)

        if var_data_ or js_expr_ != self._js_expr:
            self.__init__(
                _js_expr=js_expr_,
                _var_type=self._var_type,
                _var_data=VarData.merge(self._var_data, var_data_),
            )

    def __hash__(self) -> int:
        """Define a hash function for the var.

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Don't construct Var directly — use Var.create(value) or existing var operations which build _js_expr for you
  2. If you must construct it, ensure the first positional argument is the JS expression as a string: Var('state.count')
  3. If it comes from serialization, re-encode with the public Var API instead of raw dataclass fields
  4. Check for stale code compiled against an older reflex Var layout and update to Var.create

Example fix

# before
v = Var(42)
# after
v = Var.create(42)
Defensive patterns

Strategy: type-guard

Validate before calling

from reflex.vars import Var
assert isinstance(expr, str)
v = Var(expr)

Type guard

def is_valid_js_expr(value: object) -> bool:
    return isinstance(value, str)

Try / catch

try:
    v = Var(expr)
except TypeError as e:
    v = Var.create(expr)  # fall back to public factory

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28). Data as JSON: /api/errors/ffba1935fee8083c. Report an issue: GitHub.