reflex-dev/reflex · error · TypeError

Var-returning `@rx.memo` `{func_name}` cannot depend on embe

Error message

Var-returning `@rx.memo` `{func_name}` cannot depend on embedded components, custom code, or dynamic imports. Use a component-returning `@rx.memo` instead.

What it means

A `@rx.memo` function that returns an `rx.Var` (a computed value, not a component) had its body evaluated, and the resulting Var carries component dependencies (`var_data.components`). Var-returning memos are inlined as pure expressions into the client bundle, so they cannot embed components, custom code, or dynamic imports. Reflex raises this TypeError at decoration time to prevent unsupported usage.

Source

Thrown at packages/reflex-base/src/reflex_base/components/memo.py:816

    """
    var_data = VarData.merge(return_expr._get_all_var_data())
    if var_data is None:
        return

    if var_data.hooks:
        msg = (
            f"Var-returning `@rx.memo` `{func_name}` cannot depend on hooks. "
            "Use a component-returning `@rx.memo` instead."
        )
        raise TypeError(msg)

    if var_data.components:
        msg = (
            f"Var-returning `@rx.memo` `{func_name}` cannot depend on embedded "
            "components, custom code, or dynamic imports. Use a component-returning "
            "`@rx.memo` instead."
        )
        raise TypeError(msg)

    bundled_libraries = RegistrationContext.ensure_context().bundled_libraries
    for lib in dict(var_data.imports):
        if not lib:
            continue
        if lib.startswith((".", "/", "$/", "http")):
            continue
        if format.format_library_name(lib) in bundled_libraries:
            continue
        msg = (
            f"Var-returning `@rx.memo` `{func_name}` cannot import `{lib}` because "
            "it is not bundled. Use a component-returning `@rx.memo` instead."
        )
        raise TypeError(msg)


def _rest_placeholder(name: str) -> RestProp:
    """Create the placeholder RestProp.

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Convert the memo to return an `rx.Component` (annotate the return type as `rx.Component` or return a component) so components are legal
  2. Remove the embedded component / custom code / dynamic import from the Var-returning memo body and compute only plain values
  3. Split the logic: keep a Var-returning memo for the value and a separate component-returning memo for rendering

Example fix

// before
@rx.memo
def badge_text(count: rx.Var[int]) -> rx.Var[str]:
    return rx.cond(count > 0, rx.badge(count), "none")  # component in Var memo

// after
@rx.memo
def badge_text(count: rx.Var[int]) -> rx.Var[str]:
    rx.cond(count > 0, f"{count}", "none")  # values only

@rx.memo
def badge(count: rx.Var[int]) -> rx.Component:
    return rx.badge(count)
Defensive patterns

Strategy: validation

Validate before calling

import reflex as rx

def var_memo_is_pure(fn) -> bool:
    """Smoke-test a Var-returning memo body for component dependencies."""
    try:
        with rx.memo.Var evaluation context:  # pseudo; simply calling fn in dev is enough
            pass
    except TypeError:
        return False
    return True

Type guard

import reflex as rx

def returns_component(fn) -> bool:
    import inspect, typing
    ret = typing.get_type_hints(fn).get("return")
    if ret is None:
        return False
    args = typing.get_args(ret)
    return rx.Component in args or ret is rx.Component

Try / catch

try:
    @rx.memo
    def value_memo(n: rx.Var[int]) -> rx.Var[int]: ...
except TypeError as e:
    if "cannot depend on embedded components" in str(e):
        # fall back to a component-returning memo
        ...

Prevention

When it happens

Trigger: Decorating a function with `@rx.memo` whose return expression builds or references an `rx.Component` (e.g. returns something derived from `rx.cond(...)`, an embedded component, or code that registers components via `rx.el`), i.e. the computed Var's VarData contains a non-empty `components` list.

Common situations: Developers migrating a component-returning helper to a Var-returning memo and forgetting to remove component usage inside; referencing a child component in a computed string/format expression; using `rx.match`/`rx.cond` over components inside a memo expected to return a plain value.

Related errors


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