reflex-dev/reflex · error · TypeError

Var-returning `@rx.memo` `{func_name}` cannot import `{lib}`

Error message

Var-returning `@rx.memo` `{func_name}` cannot import `{lib}` because it is not bundled. Use a component-returning `@rx.memo` instead.

What it means

A Var-returning `@rx.memo` produced a Var whose data imports a JavaScript library that is not registered as bundled in the current `RegistrationContext`. Var-returning memos compile to inline expressions and can only rely on libraries that are bundled with the app. Reflex raises this TypeError at decoration time listing the offending import.

Source

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

            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.

    Args:
        name: The JavaScript identifier.

    Returns:
        The placeholder rest prop.
    """
    return RestProp(_js_expr=name, _var_type=dict[str, Any])


def _var_placeholder(
    name: str,
    annotation: Any,
    runtime_value: Any | None = None,

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Convert the memo to component-returning (`-> rx.Component`), which supports arbitrary library imports
  2. Ensure the library is bundled: install it via its Reflex package and import that module (e.g. `import reflex_icon_library`) before the memo is evaluated so it appears in `RegistrationContext.bundled_libraries`
  3. Refactor the memo body to avoid the operation that pulls in the unbundled import

Example fix

// before
@rx.memo
def label(icon: rx.Var[str]) -> rx.Var[str]:
    return icon + "!"  # VarData imports an unbundled icon lib

// after
import reflex_icon_library  # registers the lib as bundled

@rx.memo
def label(icon: rx.Var[str]) -> rx.Var[str]:
    return icon + "!"
Defensive patterns

Strategy: validation

Validate before calling

import reflex as rx
from reflex_base.compiler.registration_context import RegistrationContext

lib = "react-icons/fa"  # the lib your memo pulls in
bundled = RegistrationContext.ensure_context().bundled_libraries
assert lib in bundled or any(l.endswith(lib) for l in bundled), f"{lib} not bundled; import its installer first"

Type guard

def is_bundled(lib: str, bundled: set[str]) -> bool:
    from reflex.utils.format import format_library_name
    return format_library_name(lib) in bundled

Try / catch

try:
    @rx.memo
    def value_memo(...) -> rx.Var[str]: ...
except TypeError as e:
    if "it is not bundled" in str(e):
        raise  # or convert the memo to component-returning

Prevention

When it happens

Trigger: The memo body uses a Var operation (e.g. `.to_number()`, `.to_string()`, custom JS via `rx.Var.operation` or `rx.utils.format`) whose VarData imports a lib like `datetime`/`react-icons/...` that starts with `.`, `/`, `$/`, or `http` or is otherwise absent from `bundled_libraries`, so the `format.format_library_name(lib) in bundled_libraries` check fails.

Common situations: Using icon libraries or third-party npm imports inside a Var-returning memo; memoizing a function that calls a custom Var hook pulling in a CDN/URL import; forgetting to import/install the library (e.g. `import reflex_icon_library`) so it never gets registered as bundled.

Related errors


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