{"record":{"id":"d91ed03f5451cb71","repo":"jax-ml/jax","slug":"initial-value-must-be-a-scalar-got-array-of-shape","errorCode":null,"errorMessage":"initial value must be a scalar. Got array of shape {initial_arr.shape}","messagePattern":"initial value must be a scalar\\. Got array of shape (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/reductions.py","lineNumber":160,"sourceCode":"  else:\n    computation_dtype = result_dtype\n  a = lax.convert_element_type(a, computation_dtype)\n  op = op if computation_dtype != np.bool_ else bool_op\n  # NB: in XLA, init_val must be an identity for the op, so the user-specified\n  # initial value must be applied afterward.\n  init_val = _reduction_init_val(a, init_val)\n  if where_ is not None:\n    a = _where(where_, a, init_val)\n  if pos_dims is not dims:\n    if parallel_reduce is None:\n      raise NotImplementedError(f\"Named reductions not implemented for jnp.{name}()\")\n    result = parallel_reduce(a, dims)\n  else:\n    result = lax.reduce(a, init_val, op, dims)\n  if initial is not None:\n    initial_arr = lax.convert_element_type(initial, lax.asarray(a).dtype)\n    if initial_arr.shape != ():\n      raise ValueError(\"initial value must be a scalar. \"\n                       f\"Got array of shape {initial_arr.shape}\")\n    result = op(initial_arr, result)\n  if keepdims:\n    result = lax.expand_dims(result, pos_dims)\n  return lax.convert_element_type(result, dtype or result_dtype)\n\ndef _canonicalize_axis_allow_named(x, rank):\n  return maybe_named_axis(x, lambda i: canonicalize_axis(i, rank), lambda name: name)\n\ndef _reduction_dims(a: ArrayLike, axis: Axis):\n  if axis is None:\n    return (tuple(range(np.ndim(a))),) * 2\n  if not isinstance(axis, (np.ndarray, tuple, list)):\n    axes = (axis,)\n  else:\n    axes = axis\n  canon_axis = tuple(_canonicalize_axis_allow_named(x, np.ndim(a))\n                     for x in axes)","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/reductions.py#L142-L178","documentation":"The initial value passed to a JAX reduction must be a scalar; arrays of any non-empty shape are rejected because there is no defined broadcasting of a per-element initial into the reduction.","triggerScenarios":"jnp.max(x, initial=jnp.array([0, 0])) or passing a (1,)-shaped array (which is not treated as a scalar in JAX, unlike some numpy cases); passing a per-axis vector of initials.","commonSituations":"Reusing a broadcastable numpy pattern where initial had shape (1,); building initial from config values that end up as arrays (jnp.asarray of a list).","solutions":["Pass a Python scalar or 0-d array: initial=float(v) or jnp.asarray(v).reshape(())","Compute per-axis initials as separate reduction calls if per-axis values are needed"],"exampleFix":"// before\njnp.max(x, initial=jnp.array([0.0]))\n// after\njnp.max(x, initial=0.0)  # or jnp.asarray(0.0)","handlingStrategy":"validation","validationCode":"import jax.numpy as jnp\ninitial = jnp.asarray(initial)\nif initial.ndim != 0:\n    initial = initial.reshape(())  # or take .item()\njnp.max(x, initial=initial)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass Python scalars for initial","Coerce config-derived arrays to 0-d before reductions"],"tags":["jax","reductions","initial-value","shape-mismatch"],"backgroundTag":"non-scalar-reduction-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}