reflex-dev/reflex · error · TypeError

Expected a Component, got {component_called!r} of type {type

Error message

Expected a Component, got {component_called!r} of type {type(component_called)}

What it means

into_component could not coerce the value returned/used as a component into a Component after trying every conversion path (_into_component_once). It means the render function produced a value Reflex cannot render — typically a non-component object such as a number, dict, or a raw string is handled, but arbitrary objects are not.

Source

Thrown at reflex/compiler/compiler.py:958

                "indices must be integers or slices, not BooleanCastedVar",
            )):
                raise TypeError(
                    "Cannot index into a primitive sequence with a Var. Consider calling rx.Var.create() on the sequence."
                ).with_traceback(e.__traceback__) from None
        if "CastedVar" in str(e):
            raise TypeError(
                "Cannot pass a Var to a built-in function. Consider moving the operation to the backend, using existing Var operations, or defining a custom Var operation."
            ).with_traceback(e.__traceback__) from None
        raise
    except ReflexError as e:
        _modify_exception(e)
        raise

    if (converted := _into_component_once(component_called)) is not None:
        return converted

    msg = f"Expected a Component, got {component_called!r} of type {type(component_called)}"
    raise TypeError(msg)


def compile_unevaluated_page(
    route: str,
    page: UnevaluatedPage,
    style: ComponentStyle | None = None,
    theme: Component | None = None,
) -> Component:
    """Compiles an uncompiled page into a component and adds meta information.

    Args:
        route: The route of the page.
        page: The uncompiled page object.
        style: The style of the page.
        theme: The theme of the page.

    Returns:
        The compiled component and whether state should be enabled.

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Ensure the render function returns rx.fragment(...) or a real component in every branch
  2. Check for a missing call: component references passed as children must be invoked
  3. Wrap non-component values: str/int/dict children should be passed inside a component like rx.text(State.value)

Example fix

# before
def index():
    if not State.ready:
        return
    return rx.text("hi")
# after
def index():
    if not State.ready:
        return rx.fragment()
    return rx.text("hi")
Defensive patterns

Strategy: validation

Validate before calling

root = index()
assert root is None or isinstance(root, (rx.Component, str)), f"bad page root: {root!r}"

Type guard

def is_renderable(v) -> bool:
    return v is None or isinstance(v, (rx.Component, str, rx.Var))

Prevention

When it happens

Trigger: A page/render function returns or yields something that is not a Component, str, Var, or convertible value — e.g. returning None implicitly, returning a list mixing in ints, or calling a component function incorrectly (forgetting parentheses so component_called is a function object).

Common situations: Forgetting call parentheses (rx.text vs rx.text(...)), early-return None from a conditional render branch, returning raw data structures from a page function, or using a custom object as a child.

Related errors


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