reflex-dev/reflex · error · VarValueError

Cannot determine the source code for the var in {self.func!r

Error message

Cannot determine the source code for the var in {self.func!r}.

What it means

To track dependencies of a cached var, Reflex re-evaluates the var expression from the function's original source (via inspect). If inspect.getmodule(self.func) returns None or the recorded source positions are missing, Reflex cannot reconstruct the expression and raises VarValueError.

Source

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

            self.load_attr_or_method(instruction)

    def _eval_var(self, positions: dis.Positions) -> Var:
        """Evaluate instructions from the wrapped function to get the Var object.

        Args:
            positions: The disassembly positions of the get_var_value call.

        Returns:
            The Var object.

        Raises:
            VarValueError: if the source code for the var cannot be determined.
        """
        # Get the original source code and eval it to get the Var.
        module = inspect.getmodule(self.func)
        if module is None or self._get_var_value_positions is None:
            msg = f"Cannot determine the source code for the var in {self.func!r}."
            raise VarValueError(msg)
        start_line = self._get_var_value_positions.end_lineno
        start_column = self._get_var_value_positions.end_col_offset
        end_line = positions.end_lineno
        end_column = positions.end_col_offset
        if (
            start_line is None
            or start_column is None
            or end_line is None
            or end_column is None
        ):
            msg = f"Cannot determine the source code for the var in {self.func!r}."
            raise VarValueError(msg)
        source = inspect.getsource(module).splitlines(True)[start_line - 1 : end_line]
        # Create a python source string snippet.
        if len(source) > 1:
            snipped_source = "".join([
                *source[0][start_column:],
                *source[1:-1],

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Move the state/cached var definition into a real .py module file and import it normally.
  2. Remove decorators/wrappers (like functools.wraps-less wrappers) that strip __module__ or __code__ metadata from the cached var function.
  3. Ensure the source file is present and readable at runtime (not stripped in deployment).

Example fix

# before (REPL / exec'd string)
app_code = "class S(rx.State):\n  @rx.var(cached=True)\n  def v(self) -> int: return self.x"
exec(app_code)

# after
# state.py
class S(rx.State):
    @rx.var(cached=True)
    def v(self) -> int:
        return self.x
Defensive patterns

Strategy: validation

Validate before calling

import inspect

def inspectable(func) -> bool:
    return inspect.getmodule(func) is not None and inspect.getsourcefile(func) is not None

Prevention

When it happens

Trigger: A cached var function whose module cannot be determined by inspect, or whose return-expression bytecode positions are absent. Typical with functions defined in exec()/eval(), the REPL, Jupyter notebooks (sometimes), or objects wrapped so that __module__/code filename metadata is lost.

Common situations: Prototyping state in a notebook or REPL; building apps from strings with exec; functools.partial or custom decorators that hide the original function's module; frozen/compiled environments where source files are not on disk.

Related errors


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