reflex-dev/reflex · error · ForeachVarError

Could not foreach over var `{iterable!s}` of type Any. (If y

Error message

Could not foreach over var `{iterable!s}` of type Any. (If you are trying to foreach over a state var, add a type annotation to the var). See https://reflex.dev/docs/library/dynamic-rendering/foreach/

What it means

`rx.foreach` needs to know the element type of the iterable to generate render code. If the state var (or any iterable Var) has `_var_type == Any` — typically because the State field has no type annotation — Reflex cannot determine what each item is and raises ForeachVarError.

Source

Thrown at packages/reflex-components-core/src/reflex_components_core/core/foreach.py:83

        # noqa: DAR402 UntypedVarError
        """
        from reflex_base.vars import ArrayVar, ObjectVar, StringVar

        from reflex.state import ComponentState

        iterable = (
            LiteralVar.create(iterable).guess_type()
            if not isinstance(iterable, Var)
            else iterable.guess_type()
        )

        if iterable._var_type == Any:
            msg = (
                f"Could not foreach over var `{iterable!s}` of type Any. "
                "(If you are trying to foreach over a state var, add a type annotation to the var). "
                "See https://reflex.dev/docs/library/dynamic-rendering/foreach/"
            )
            raise ForeachVarError(msg)

        if (
            hasattr(render_fn, "__qualname__")
            and render_fn.__qualname__ == ComponentState.create.__qualname__
        ):
            msg = "Using a ComponentState as `render_fn` inside `rx.foreach` is not supported yet."
            raise TypeError(msg)

        if isinstance(iterable, ObjectVar):
            iterable = iterable.entries()

        if isinstance(iterable, StringVar):
            iterable = iterable.split()

        if not isinstance(iterable, ArrayVar):
            msg = (
                f"Could not foreach over var `{iterable!s}` of type {iterable._var_type}. "
                "See https://reflex.dev/docs/library/dynamic-rendering/foreach/"

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Annotate the state var with the full element type: items: list[str] = []
  2. For objects, define a pydantic model and annotate list[Item]
  3. For dicts, use dict[str, int] so foreach passes key/value to the render fn

Example fix

# before
class State(rx.State):
    items = []
rx.foreach(State.items, lambda i: rx.text(i))
# after
class State(rx.State):
    items: list[str] = []
rx.foreach(State.items, lambda i: rx.text(i))
Defensive patterns

Strategy: type-guard

Validate before calling

from typing import get_type_hints
hints = get_type_hints(State)
assert hints.get('items') is not None and hints['items'] != list, 'annotate items: list[T]'

Type guard

def is_typed_list_var(v) -> bool:
    t = getattr(v, '_var_type', None)
    import typing
    return t is not None and t is not typing.Any and getattr(t, '__origin__', None) in (list, dict, set)

Prevention

When it happens

Trigger: Declaring `class State(rx.State): items = []` (no annotation) and calling `rx.foreach(State.items, render_fn)`. Same for `data: list = []` annotated only as bare `list`.

Common situations: Rapid prototyping without full annotations; loading items from an API into an untyped field; annotating as `list` (bare) instead of `list[str]` or `list[Item]`.

Related errors


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