{"record":{"id":"e4bf45ed296af571","repo":"reflex-dev/reflex","slug":"could-not-foreach-over-var-iterable-s-of-type-e4bf45","errorCode":null,"errorMessage":"Could not foreach over var `{iterable!s}` of type {iterable._var_type}. See https://reflex.dev/docs/library/dynamic-rendering/foreach/","messagePattern":"Could not foreach over var `(.+?)` of type (.+?)\\. 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":103,"sourceCode":"        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/\"\n            )\n            raise ForeachVarError(msg)\n\n        if types.is_optional(iterable._var_type):\n            iterable = cond(iterable, iterable, [])\n\n        component = cls._create(\n            children=[],\n            iterable=iterable,\n            render_fn=render_fn,\n        )\n        try:\n            # Keep a ref to a rendered component to determine correct imports/hooks/styles.\n            component.children = [component._render().render_component()]\n        except UntypedVarError as e:\n            raise UntypedVarError(\n                iterable,\n                \"foreach\",\n                \"https://reflex.dev/docs/library/dynamic-rendering/foreach/\",\n            ).with_traceback(e.__traceback__) from None","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-components-core/src/reflex_components_core/core/foreach.py#L85-L121","documentation":"After unwrapping (objects → entries, strings → split), `rx.foreach` requires an ArrayVar — an iterable whose type is a list/array. If the var's type is, say, a plain non-iterable type, a set, a generator annotation, or something foreach cannot convert to ArrayVar, it raises ForeachVarError.","triggerScenarios":"`rx.foreach(State.count, ...)` where count: int; annotating items as a set or custom non-list iterable; foreach over a var typed as tuple in some unsupported path.","commonSituations":"Passing the wrong state var to foreach; annotating with non-list container types; refactoring a list field into a scalar and forgetting the foreach call.","solutions":["Annotate the state field as a list: items: list[str] = []","Pass an actual iterable var: rx.foreach(State.tags, ...) where tags: list[str]","Convert other containers to lists in an event handler before rendering"],"exampleFix":"# before\nclass State(rx.State):\n    tags: set[str] = set()\nrx.foreach(State.tags, lambda t: rx.badge(t))\n# after\nclass State(rx.State):\n    tags: list[str] = []\nrx.foreach(State.tags, lambda t: rx.badge(t))","handlingStrategy":"type-guard","validationCode":"from reflex.vars import ArrayVar\nassert isinstance(State.tags, ArrayVar) or str(getattr(State.tags, '_var_type', '')).startswith(('list', 'dict')), 'need list-typed var'","typeGuard":"def is_foreachable(v) -> bool:\n    from reflex.vars import ArrayVar\n    return isinstance(v, ArrayVar) or str(getattr(v, '_var_type', type(v))).startswith(('list', 'dict'))","tryCatchPattern":null,"preventionTips":["Annotate iterated state fields as list[T]","Convert sets/generators to lists in event handlers before storing"],"tags":["reflex","foreach","type-annotation"],"backgroundTag":"invalid-iterable-type","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}