{"record":{"id":"bea49a4ceb59467c","repo":"reflex-dev/reflex","slug":"the-function-passed-to-reduce-should-take-exactly","errorCode":null,"errorMessage":"The function passed to reduce should take exactly two arguments (accumulator, element).","messagePattern":"The function passed to reduce should take exactly two arguments \\(accumulator, element\\)\\.","errorType":"exception","errorClass":"VarTypeError","httpStatus":null,"severity":"error","filePath":"packages/reflex-base/src/reflex_base/vars/sequence.py","lineNumber":560,"sourceCode":"        an empty array raises a TypeError at runtime.\n\n        Args:\n            fn: The reducer, taking the accumulator and the current element.\n            initial: The initial accumulator value.\n\n        Returns:\n            The reduced value.\n\n        Raises:\n            VarTypeError: If the function does not take exactly two arguments.\n        \"\"\"\n        from .function import ArgsFunctionOperation\n\n        if not callable(fn):\n            raise_unsupported_operand_types(\"reduce\", (type(self), type(fn)))\n        if len(inspect.signature(fn).parameters) != 2:\n            msg = \"The function passed to reduce should take exactly two arguments (accumulator, element).\"\n            raise VarTypeError(msg)\n\n        element = self._element_placeholder()\n        initial_var = None if isinstance(initial, types.Unset) else Var.create(initial)\n        accumulator_type = (\n            element._var_type if initial_var is None else initial_var._var_type\n        )\n        accumulator = Var(\n            _js_expr=get_unique_variable_name(),\n            _var_type=accumulator_type,\n        ).guess_type()\n        return_expr = Var.create(fn(accumulator, element))\n        function_var = ArgsFunctionOperation.create(\n            (accumulator._js_expr, element._js_expr),\n            return_expr,\n            _var_type=Callable[\n                [accumulator_type, element._var_type], return_expr._var_type\n            ],\n        )","sourceCodeStart":542,"sourceCodeEnd":578,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/vars/sequence.py#L542-L578","documentation":"ArrayVar.reduce() compiles the passed Python reducer into a JS reduce callback, which must accept exactly two parameters: the accumulator and the current element. Any other arity raises VarTypeError.","triggerScenarios":"Passing a one-arg function to reduce (`items.reduce(lambda x: ...)`), a three-arg function (trying to include the index like JS reduce), or a function using *args (inspect.signature().parameters count != 2).","commonSituations":"Copy-pasting JS reduce((acc, el, i) => ...) into Python lambdas; reusing min/max style helpers that take variadic args; wrapping reducers in decorators that alter signatures.","solutions":["Use exactly two parameters: lambda accumulator, element: ....","If you need the index, track it via a different mechanism (computed var with enumerate).","Ensure no default/keyword-only params inflate the signature count."],"exampleFix":"# before\nState.nums.reduce(lambda acc, x, i: acc + x)\n\n# after\nState.nums.reduce(lambda acc, x: acc + x, initial=0)","handlingStrategy":"validation","validationCode":"import inspect\n\ndef is_binary_reducer(fn) -> bool:\n    return len(inspect.signature(fn).parameters) == 2","typeGuard":"import inspect\n\ndef binary_callback(fn) -> bool:\n    return callable(fn) and len(inspect.signature(fn).parameters) == 2","tryCatchPattern":null,"preventionTips":["Always write reducers as lambda acc, el: ....","Don't add index or extra args to Python reducers.","Remember JS reduce's third arg has no Python equivalent here."],"tags":["reflex","arrayvar","reduce","callback-signature"],"backgroundTag":"var-callback-signature-mismatch","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}