reflex-dev/reflex · error · TypeError

`@rx.memo` on `{fn.__name__}` must return `rx.Component` or

Error message

`@rx.memo` on `{fn.__name__}` must return `rx.Component` or `rx.Var[...]`, got `{return_annotation}`.

What it means

The `@rx.memo` decorator requires the decorated function's return annotation to be an `rx.Component` (or subclass) or `rx.Var[...]`. Any other annotation (str, int, None, un-annotated return) fails immediately at decoration time.

Source

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

    Raises:
        TypeError: If the return annotation is not supported, or a non-default
            ``wrapper`` is given for a var-returning memo.
    """
    hints = get_type_hints(fn, include_extras=True)
    return_annotation = hints.get("return", inspect.Signature.empty)
    missing_return = return_annotation is inspect.Signature.empty
    if missing_return:
        return_annotation = Component
        hints["return"] = Component

    is_component = _is_component_annotation(return_annotation)
    if not is_component and not _is_var_annotation(return_annotation):
        msg = (
            f"`@rx.memo` on `{fn.__name__}` must return `rx.Component` or "
            f"`rx.Var[...]`, got `{return_annotation}`."
        )
        raise TypeError(msg)
    if not is_component and wrapper is not DEFAULT_MEMO_WRAPPER:
        msg = (
            "`@rx.memo` only supports `wrapper=` on component-returning memos; "
            f"`{fn.__name__}` returns `rx.Var[...]`, which compiles to a plain "
            "function."
        )
        raise TypeError(msg)

    defaulted_params: list[str] = []
    missing_params: list[str] = []
    params = _analyze_params(
        fn,
        for_component=is_component,
        hints=hints,
        defaulted_params=defaulted_params,
        missing_params=missing_params,
    )

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Annotate the return as `rx.Component` (or a specific component type) or `rx.Var[str]` etc.
  2. If the function is not a render/value function, remove `@rx.memo`

Example fix

// before
@rx.memo
def label() -> str:
    return "hi"

// after
@rx.memo
def label() -> rx.Var[str]:
    return "hi"
Defensive patterns

Strategy: type-guard

Validate before calling

import typing, reflex as rx

def memo_compatible(fn) -> bool:
    ret = typing.get_type_hints(fn).get("return")
    if ret is None:
        return False
    if isinstance(ret, type) and issubclass(ret, rx.Component):
        return True
    origin = typing.get_origin(ret)
    return origin is not None and origin is rx.Var

Type guard

import typing, reflex as rx

def is_valid_memo_return(fn) -> bool:
    ret = typing.get_type_hints(fn).get("return")
    return ret is rx.Component or ret is rx.Var or (
        typing.get_origin(ret) is rx.Var
    )

Prevention

When it happens

Trigger: `@rx.memo` on `def count() -> int:` or on a function with no return annotation that Reflex cannot resolve to a Component/Var type.

Common situations: Adding the decorator to a plain helper function by mistake, or annotating with a custom alias Reflex does not recognize as a component type.

Related errors


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