{"record":{"id":"39e134f3a94f8717","repo":"jax-ml/jax","slug":"zero-size-array-to-reduction-operation-self-nam","errorCode":null,"errorMessage":"zero-size array to reduction operation {self.__name__} which has no identity","messagePattern":"zero-size array to reduction operation (.+?) which has no identity","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/ufunc_api.py","lineNumber":298,"sourceCode":"      if where is not None:\n        where = where.ravel()\n      axis = 0\n    else:\n      axis = canonicalize_axis(axis, arr.ndim)\n      if keepdims:\n        final_shape = (*arr.shape[:axis], 1, *arr.shape[axis + 1:])\n      else:\n        final_shape = (*arr.shape[:axis], *arr.shape[axis + 1:])\n\n    # TODO: handle without transpose?\n    if axis != 0:\n      arr = _moveaxis(arr, axis, 0)\n      if where is not None:\n        where = _moveaxis(where, axis, 0)\n\n    if arr.shape[0] == 0:\n      if initial is None:\n        raise ValueError(f\"zero-size array to reduction operation {self.__name__} which has no identity\")\n      return lax.full(final_shape, initial, dtype)\n\n    def body_fun(i, val):\n      if where is None:\n        return self(val, arr[i].astype(dtype))\n      else:\n        return _where(where[i], self(val, arr[i].astype(dtype)), val)\n\n    start_value: ArrayLike\n    if initial is None:\n      start_index = 1\n      start_value = arr[0]\n    else:\n      start_index = 0\n      start_value = initial\n    start_value = _broadcast_to(lax.asarray(start_value).astype(dtype), arr.shape[1:])\n\n    result = control_flow.fori_loop(start_index, arr.shape[0], body_fun, start_value)","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/ufunc_api.py#L280-L316","documentation":"Reducing an empty (zero-size) array requires an identity or initial value to produce a well-defined result. In the scan-based fallback, if the leading reduced dimension is 0 and no initial is supplied, JAX raises this ValueError.","triggerScenarios":"jnp.maximum.reduce(jnp.zeros((0,3))) or any ufunc.reduce over an empty axis without initial, where the ufunc has no usable identity.","commonSituations":"Empty batches after filtering, dynamic data loading that yields zero rows, edge cases in dataloaders feeding reductions.","solutions":["Pass an explicit initial value","Guard upstream: skip the reduction when arr.shape[axis] == 0 and use a sensible default","Ensure data pipelines never produce empty inputs unexpectedly"],"exampleFix":"// before\nresult = jnp.maximum.reduce(x)  # x.shape[0] == 0\n// after\nresult = jnp.maximum.reduce(x, initial=-jnp.inf)","handlingStrategy":"validation","validationCode":"if x.shape[axis if isinstance(axis,int) else 0] == 0:\n    result = initial if initial is not None else raise_safe_default()","typeGuard":null,"tryCatchPattern":"try:\n    r = u.reduce(x)\nexcept ValueError:\n    r = initial  # e.g. -jnp.inf for maximum","preventionTips":["Validate batch sizes are non-zero before reductions","Always pass initial for reductions over dynamically-sized data"],"tags":["jax","ufunc","reduce","empty-array"],"backgroundTag":"zero-size-reduction-no-identity","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}