jax-ml/jax · error · ValueError

reduce only supported for functions returning a single value

Error message

reduce only supported for functions returning a single value

What it means

ufunc.reduce requires the ufunc to return a single output (nout == 1). Some numpy ufuncs return multiple values, and reducing those is undefined, so JAX raises this ValueError.

Source

Thrown at jax/_src/numpy/ufunc_api.py:245

      :func:`jax.numpy.all`:

      >>> jnp.logical_and.reduce(x > 2)
      Array([False, False,  True], dtype=bool)
      >>> jnp.all(x > 2, axis=0)
      Array([False, False,  True], dtype=bool)

      Some reductions do not correspond to any built-in aggregation function;
      for example here is the reduction of :func:`jax.numpy.bitwise_or` along
      the first axis of ``x``:

      >>> jnp.bitwise_or.reduce(x, axis=1)
      Array([3, 7], dtype=int32)
    """
    check_arraylike(f"{self.__name__}.reduce", a)
    if self.nin != 2:
      raise ValueError("reduce only supported for binary ufuncs")
    if self.nout != 1:
      raise ValueError("reduce only supported for functions returning a single value")
    if out is not None:
      raise NotImplementedError(f"out argument of {self.__name__}.reduce()")
    if initial is not None:
      check_arraylike(f"{self.__name__}.reduce", initial)
    if where is not None:
      check_arraylike(f"{self.__name__}.reduce", where)
      if self.identity is None and initial is None:
        raise ValueError(f"reduction operation {self.__name__!r} does not have an identity, "
                         "so to use a where mask one has to specify 'initial'.")
      if lax._dtype(where) != bool:
        raise ValueError(f"where argument must have dtype=bool; got dtype={lax._dtype(where)}")
    reduce = self.__static_props['reduce'] or self._reduce_via_scan
    return reduce(a, axis=axis, dtype=dtype, keepdims=keepdims, initial=initial, where=where)

  def _reduce_via_scan(self, arr: ArrayLike, axis: int | tuple[int, ...] | None = 0, dtype: DTypeLike | None = None,
                       keepdims: bool = False, initial: ArrayLike | None = None,
                       where: ArrayLike | None = None) -> Array:
    assert self.nin == 2 and self.nout == 1

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Check ufunc.nout == 1 before calling .reduce
  2. Reduce over the specific single-output component you need instead
Defensive patterns

Strategy: validation

Validate before calling

assert ufunc.nout == 1

Type guard

def is_single_output_ufunc(u): return u.nout == 1

Prevention

When it happens

Trigger: Calling .reduce on a ufunc whose nout != 1 (multi-output ufuncs, e.g. divmod-style ops registered as ufuncs).

Common situations: Rare; typically metaprogramming that iterates over the ufunc registry and blindly calls .reduce.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/f79631912fc1a5d2. Report an issue: GitHub.