reflex-dev/reflex · error · VarTypeError

The function passed to {operation_name} should take at most

Error message

The function passed to {operation_name} should take at most one argument.

What it means

For ArrayVar operations map/filter/flat_map, Reflex traces the passed Python function to generate the equivalent JS callback. The callback may take zero or one argument (the element); more than one parameter cannot be mapped to JS, so VarTypeError is raised.

Source

Thrown at packages/reflex-base/src/reflex_base/vars/sequence.py:456

    ) -> tuple[tuple[str, ...], Var]:
        """Call fn with a placeholder element to get function arg names and return expression.

        Args:
            fn: The function to trace, taking at most one argument.
            operation_name: The name of the calling operation, for error messages.

        Returns:
            A tuple of the function's argument names and its return expression.

        Raises:
            VarTypeError: If the function takes more than one argument.
        """
        if not callable(fn):
            raise_unsupported_operand_types(operation_name, (type(self), type(fn)))
        num_args = len(inspect.signature(fn).parameters)
        if num_args > 1:
            msg = f"The function passed to {operation_name} should take at most one argument."
            raise VarTypeError(msg)
        if num_args == 0:
            return (), Var.create(fn())
        element = self._element_placeholder()
        return (element._js_expr,), Var.create(fn(element))

    def map(self, fn: Any):
        """Apply a function to each element of the array.

        Args:
            fn: The function to apply, taking at most one argument.

        Returns:
            The array after applying the function.
        """
        from .function import ArgsFunctionOperation

        args, return_expr = self._trace_element_fn(fn, "map")
        return map_array_operation(

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Rewrite the callback to take exactly one argument (the element); if the index is needed, use a different approach (e.g. enumerate in a computed var).
  2. Close over extra configuration values instead of declaring them as parameters.

Example fix

# before
State.items.map(lambda item, idx: f"{idx}: {item}")

# after
class State(rx.State):
    @rx.var
    def items_indexed(self) -> list[str]:
        return [f"{i}: {x}" for i, x in enumerate(self.items)]
Defensive patterns

Strategy: validation

Validate before calling

import inspect

def takes_at_most_one_arg(fn) -> bool:
    return len(inspect.signature(fn).parameters) <= 1

Type guard

import inspect

def unary_callback(fn) -> bool:
    return callable(fn) and len(inspect.signature(fn).parameters) <= 1

Prevention

When it happens

Trigger: Passing a lambda/function with 2+ positional params to .map()/.filter()/.flat_map(), e.g. `items.map(lambda item, i: ...)` or a function with extra defaulted args counted by inspect.signature (e.g. keyword-only or *args in some forms).

Common situations: Trying to get the index alongside the element; reusing a helper that takes additional config arguments; copying JS .map((el, i) => ...) patterns into Python.

Related errors


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