{"record":{"id":"1c143f6784fb3da2","repo":"reflex-dev/reflex","slug":"could-not-foreach-over-var-iterable-s-of-type","errorCode":null,"errorMessage":"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/","messagePattern":"Could not foreach over var `(.+?)` 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/","errorType":"exception","errorClass":"ForeachVarError","httpStatus":null,"severity":"error","filePath":"packages/reflex-components-core/src/reflex_components_core/core/foreach.py","lineNumber":83,"sourceCode":"        # noqa: DAR402 UntypedVarError\n        \"\"\"\n        from reflex_base.vars import ArrayVar, ObjectVar, StringVar\n\n        from reflex.state import ComponentState\n\n        iterable = (\n            LiteralVar.create(iterable).guess_type()\n            if not isinstance(iterable, Var)\n            else iterable.guess_type()\n        )\n\n        if iterable._var_type == Any:\n            msg = (\n                f\"Could not foreach over var `{iterable!s}` of type Any. \"\n                \"(If you are trying to foreach over a state var, add a type annotation to the var). \"\n                \"See https://reflex.dev/docs/library/dynamic-rendering/foreach/\"\n            )\n            raise ForeachVarError(msg)\n\n        if (\n            hasattr(render_fn, \"__qualname__\")\n            and render_fn.__qualname__ == ComponentState.create.__qualname__\n        ):\n            msg = \"Using a ComponentState as `render_fn` inside `rx.foreach` is not supported yet.\"\n            raise TypeError(msg)\n\n        if isinstance(iterable, ObjectVar):\n            iterable = iterable.entries()\n\n        if isinstance(iterable, StringVar):\n            iterable = iterable.split()\n\n        if not isinstance(iterable, ArrayVar):\n            msg = (\n                f\"Could not foreach over var `{iterable!s}` of type {iterable._var_type}. \"\n                \"See https://reflex.dev/docs/library/dynamic-rendering/foreach/\"","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-components-core/src/reflex_components_core/core/foreach.py#L65-L101","documentation":"`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.","triggerScenarios":"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`.","commonSituations":"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]`.","solutions":["Annotate the state var with the full element type: items: list[str] = []","For objects, define a pydantic model and annotate list[Item]","For dicts, use dict[str, int] so foreach passes key/value to the render fn"],"exampleFix":"# before\nclass State(rx.State):\n    items = []\nrx.foreach(State.items, lambda i: rx.text(i))\n# after\nclass State(rx.State):\n    items: list[str] = []\nrx.foreach(State.items, lambda i: rx.text(i))","handlingStrategy":"type-guard","validationCode":"from typing import get_type_hints\nhints = get_type_hints(State)\nassert hints.get('items') is not None and hints['items'] != list, 'annotate items: list[T]'","typeGuard":"def is_typed_list_var(v) -> bool:\n    t = getattr(v, '_var_type', None)\n    import typing\n    return t is not None and t is not typing.Any and getattr(t, '__origin__', None) in (list, dict, set)","tryCatchPattern":null,"preventionTips":["Always fully annotate state fields: items: list[str] = []","Enable strict typing/pyright on state classes","Avoid bare 'list'/'dict' annotations"],"tags":["reflex","foreach","type-annotation","state"],"backgroundTag":"missing-type-annotation","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}