{"record":{"id":"89f5681e84fe8f0f","repo":"jax-ml/jax","slug":"expected-len-axes-fill-values-got-len-fill-va","errorCode":null,"errorMessage":"Expected {len(axes)} fill values, got {len(fill_value_avals)}","messagePattern":"Expected (.+?) fill values, got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/hijax.py","lineNumber":209,"sourceCode":"      a_aval: core.ShapedArray,\n      *fill_value_avals: core.ShapedArray,\n      size: int,\n      axes: tuple[int, ...],\n      out_dtype: np.dtype):\n    if core.is_symbolic_dim(size):\n      pass\n    else:\n      size = operator.index(size)\n      if size < 0:\n        raise ValueError(f\"size must be a positive integer; got {size=}\")\n    if not dtypes.issubdtype(out_dtype, np.integer):\n      raise ValueError(f\"out_dtype must be integer typed; got {out_dtype=}\")\n    if not all(0 <= ax < a_aval.ndim for ax in axes):\n      raise ValueError(f\"axes out of range for array with {a_aval.ndim} dimensions:  {axes=}\")\n    if len(axes) != len(set(axes)):\n      raise ValueError(f\"duplicate axes are not allowed: {axes=}\")\n    if fill_value_avals and len(fill_value_avals) != len(axes):\n      raise ValueError(f\"Expected {len(axes)} fill values, got {len(fill_value_avals)}\")\n    if any(fv.dtype != out_dtype for fv in fill_value_avals):\n      raise ValueError(f\"Expected fill values to have dtype {out_dtype}, got {fill_value_avals}\")\n    batch_shape = tuple(\n        s for i, s in enumerate(a_aval.shape) if i not in axes\n    )\n    for fv_aval in fill_value_avals:\n      try:\n        broadcasted = lax.broadcast_shapes(fv_aval.shape, batch_shape)\n      except ValueError as e:\n        raise ValueError(\n            f\"fill_value shape {fv_aval.shape} is not broadcast-compatible with \"\n            f\"batch shape {batch_shape}\"\n        ) from e\n      if broadcasted != batch_shape:\n        raise ValueError(\n            f\"fill_value shape {fv_aval.shape} cannot be broadcast to \"\n            f\"batch shape {batch_shape} without expanding it.\"\n        )","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/hijax.py#L191-L227","documentation":"Raised by the Nonzero HiJAX primitive when the number of provided fill_value avals does not equal the number of axes. Each reduced axis needs its own padding value for the returned index arrays when fewer than size nonzeros exist.","triggerScenarios":"Constructing Nonzero with axes=(0, 1) but supplying only one fill value aval (or three). The public nonzero() wrapper normally catches this earlier with its own message; direct primitive construction hits this one.","commonSituations":"Mismatch between the axes tuple length and a fill_value list built independently, e.g. after adding an axis but not a fill value.","solutions":["Make len(fill_value) == len(axes), or pass a single scalar fill_value so it is broadcast to all axes","Recompute fill values whenever the axes tuple changes"],"exampleFix":"# before\nprim = Nonzero(aval, fv_aval, size=n, axes=(0, 1), out_dtype=np.int32)\n# after\nprim = Nonzero(aval, fv_aval, fv_aval, size=n, axes=(0, 1), out_dtype=np.int32)","handlingStrategy":"validation","validationCode":"assert len(fill_value) == len(axes), (len(fill_value), len(axes))","typeGuard":"def fill_matches_axes(fill_value, axes) -> bool:\n    return fill_value is None or len(fill_value) == len(axes)","tryCatchPattern":null,"preventionTips":["Prefer a single scalar fill_value so it broadcasts to all axes","Derive fill-value tuples from the axes tuple in the same expression"],"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"}