{"record":{"id":"ba6671b7bf5ae50f","repo":"jax-ml/jax","slug":"fill-value-must-be-a-scalar-or-tuple-of-scalars-g","errorCode":null,"errorMessage":"fill_value must be a scalar or tuple of scalars; got {fill_value}","messagePattern":"fill_value must be a scalar or tuple of scalars; got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/hijax.py","lineNumber":746,"sourceCode":"\n  Returns:\n    Tuple of length ``len(axes)`` containing the indices of each nonzero value.\n  \"\"\"\n  a, = core.auto_insert_reshard(a)\n  out_dtype = dtypes._maybe_canonicalize_explicit_dtype(np.dtype(dtype), \"nonzero\")\n  axes = util.canonicalize_axis_tuple(axes, np.ndim(a))\n\n  if fill_value is not None:\n    if isinstance(fill_value, tuple):\n      if len(fill_value) != len(axes):\n        raise ValueError(f\"fill_value tuple must have length equal to number of axes ({len(axes)}); got {len(fill_value)}\")\n      fill_value_tup = fill_value\n    else:\n      fill_value_tup = (fill_value,) * len(axes)\n    fill_value_tup = tuple(jnp.asarray(fv, dtype=out_dtype) for fv in fill_value_tup)\n    for fv in fill_value_tup:\n      if fv.ndim != 0:\n        raise ValueError(f\"fill_value must be a scalar or tuple of scalars; got {fill_value}\")\n  else:\n    fill_value_tup = ()\n\n  prim = Nonzero(\n    core.typeof(a),\n    *[core.typeof(fv) for fv in fill_value_tup],\n    size=size,\n    axes=axes,\n    out_dtype=out_dtype,\n  )\n  return prim(a, *fill_value_tup)\n\n\ndef einsum(\n    subscripts: str,\n    /,\n    *operands: ArrayLike,\n    optimize: str | bool | tuple[tuple[int, ...], ...] = \"auto\",","sourceCodeStart":728,"sourceCodeEnd":764,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/hijax.py#L728-L764","documentation":"Raised by the public jax.numpy nonzero() function when any fill_value element is not a scalar (after conversion to out_dtype). Fill values pad the fixed-size index outputs and must be 0-dimensional; arrays with any nonzero ndim are rejected.","triggerScenarios":"nonzero(a, size=8, fill_value=jnp.arange(3)) or fill_value=([0, 1],); any list/array fill_value with ndim > 0.","commonSituations":"Passing an array of fill values intended per-batch (not supported — fills must be scalars); passing a shape-(1,) array like jnp.array([0.0]) instead of 0.0.","solutions":["Use scalar fill values only: fill_value=-1 or fill_value=(-1, -1)","If a shape-(1,) sneaked in, squeeze it or pass the Python scalar directly","For per-batch behavior, construct the Nonzero primitive with broadcast-compatible fill arrays instead of the public function"],"exampleFix":"# before\nidx = nonzero(a, size=8, fill_value=jnp.array([-1]))\n# after\nidx = nonzero(a, size=8, fill_value=-1)","handlingStrategy":"validation","validationCode":"fv_tup = fill_value if isinstance(fill_value, tuple) else (fill_value,)\nassert all(jnp.asarray(fv).ndim == 0 for fv in fv_tup)","typeGuard":"def scalar_fills(fill_value) -> bool:\n    fv = fill_value if isinstance(fill_value, tuple) else (fill_value,)\n    return all(jnp.asarray(v).ndim == 0 for v in fv)","tryCatchPattern":null,"preventionTips":["Pass Python scalars or 0-d jnp arrays as fill values","Squeeze accidental shape-(1,) arrays: jnp.asarray(x).squeeze()"],"tags":["jax","nonzero","fill-value","argument-validation"],"backgroundTag":"non-scalar-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}