{"record":{"id":"32e2068203fdf472","repo":"jax-ml/jax","slug":"fill-value-tuple-must-have-length-equal-to-number","errorCode":null,"errorMessage":"fill_value tuple must have length equal to number of axes ({len(axes)}); got {len(fill_value)}","messagePattern":"fill_value tuple must have length equal to number of axes \\((.+?)\\); got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/hijax.py","lineNumber":739,"sourceCode":"  Args:\n    a: N-dimensional array.\n    size: static integer specifying the number of nonzero entries to return.\n    fill_value: optional padding value when ``size`` is specified. Defaults to 0.\n    axes: optional tuple of integers specifying the axes to compute the result over.\n      Defaults to None (all axes).\n    dtype: optional datatype for the returned indices. Defaults to int32.\n\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)","sourceCodeStart":721,"sourceCodeEnd":757,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/hijax.py#L721-L757","documentation":"Raised by the public jax.numpy nonzero() function when fill_value is given as a tuple whose length differs from the number of axes. One fill value per returned index array (one per axis) is required; a scalar fill_value is broadcast to all axes automatically.","triggerScenarios":"nonzero(a, size=8, axes=(0, 1), fill_value=(-1,)) — two axes but one fill value; or fill_value=(0, 0, 0) with the default axes (a.ndim == 2).","commonSituations":"Adding an axis to the call but forgetting to extend the fill_value tuple; hardcoding a fill_value tuple while axes defaults to all ndim axes of a differently-ranked input.","solutions":["Match lengths: len(fill_value) == len(axes)","Pass a single scalar fill_value (e.g. fill_value=-1) and let it broadcast to every axis","Compute axes explicitly (util.canonicalize_axis_tuple) and derive the tuple length from it"],"exampleFix":"# before\nidx = nonzero(a, size=8, axes=(0, 1), fill_value=(-1,))\n# after\nidx = nonzero(a, size=8, axes=(0, 1), fill_value=(-1, -1))\n# or simply\nidx = nonzero(a, size=8, axes=(0, 1), fill_value=-1)","handlingStrategy":"validation","validationCode":"if isinstance(fill_value, tuple):\n    assert len(fill_value) == len(axes), (len(fill_value), len(axes))","typeGuard":"def fill_len_ok(fill_value, n_axes: int) -> bool:\n    return fill_value is None or not isinstance(fill_value, tuple) or len(fill_value) == n_axes","tryCatchPattern":null,"preventionTips":["Pass a scalar fill_value unless per-axis fills are truly needed","When axes is None remember it defaults to all ndim axes — size the tuple accordingly"],"tags":["jax","nonzero","fill-value","argument-validation"],"backgroundTag":"argument-count-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}