{"record":{"id":"7c2f554e3ea07b6a","repo":"reflex-dev/reflex","slug":"the-render-function-must-take-2-arguments","errorCode":null,"errorMessage":"The render function must take 2 arguments.","messagePattern":"The render function must take 2 arguments\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"packages/reflex-base/src/reflex_base/components/tags/iter_tag.py","lineNumber":100,"sourceCode":"        from reflex_components_core.base.fragment import Fragment\n        from reflex_components_core.core.cond import Cond\n        from reflex_components_core.core.foreach import Foreach\n\n        from reflex.compiler.compiler import _into_component_once\n\n        # Get the render function arguments.\n        args = inspect.getfullargspec(self.render_fn).args\n        arg = self.get_arg_var()\n        index = self.get_index_var()\n\n        if len(args) == 1:\n            # If the render function doesn't take the index as an argument.\n            component = self.render_fn(arg)\n        else:\n            # If the render function takes the index as an argument.\n            if len(args) != 2:\n                msg = \"The render function must take 2 arguments.\"\n                raise ValueError(msg)\n            component = self.render_fn(arg, index)\n\n        # Nested foreach components or cond must be wrapped in fragments.\n        if isinstance(component, (Foreach, Cond)):\n            component = Fragment.create(component)\n\n        component = _into_component_once(component)\n\n        if component is None:\n            msg = \"The render function must return a component.\"\n            raise ValueError(msg)\n\n        # Set the component key.\n        if component.key is None:\n            component.key = index\n\n        return component\n","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/components/tags/iter_tag.py#L82-L118","documentation":"When a `rx.foreach` render function accepts the index (two parameters), Reflex calls it with `(item, index)`. This error means the render function's signature declares it takes the index (arity detected as 2) but Reflex ended up needing exactly two arguments and the function does not accept `(arg, index)` as provided — i.e. the arity contract is broken.","triggerScenarios":"A render function with a mismatched arity, e.g. default/keyword-only arguments making the effective parameter count ambiguous, or manually calling `render_component` with an index when the function takes only one argument plus a defaulted second.","commonSituations":"Adding a defaulted second parameter (`def render(item, i=0)`) to a foreach render fn, or wrapping a lambda with `functools.partial` that changes the effective arity.","solutions":["Make the render function take exactly `(item)` or exactly `(item, index)` with no defaults or keyword-only args","Avoid `functools.partial` on render functions; use a closure instead"],"exampleFix":"# before\nrx.foreach(State.items, lambda item, i=0: rx.text(item, key=i))\n\n# after\nrx.foreach(State.items, lambda item, i: rx.text(item, key=i))","handlingStrategy":"type-guard","validationCode":"import inspect\n\ndef valid_foreach_fn(fn) -> bool:\n    params = [p for p in inspect.signature(fn).parameters.values()\n              if p.default is inspect.Parameter.empty\n              and p.kind in (inspect.Parameter.POSITIONAL_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD)]\n    return len(params) in (1, 2)","typeGuard":"import inspect\n\ndef is_valid_render_fn(fn) -> bool:\n    \"\"\"Render fn must take exactly (item) or (item, index), no defaults.\"\"\"\n    ps = list(inspect.signature(fn).parameters.values())\n    return all(p.default is inspect.Parameter.empty for p in ps) and len(ps) in (1, 2)","tryCatchPattern":null,"preventionTips":["Write foreach lambdas as `lambda item: ...` or `lambda item, i: ...` with no default args","Don't wrap render fns in functools.partial; use closures"],"tags":["reflex","foreach","render-function","arity"],"backgroundTag":"callback-signature-mismatch","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}