{"record":{"id":"bd1d5b7972e04315","repo":"jax-ml/jax","slug":"jnp-name-where-must-be-none-or-a-boolean-array","errorCode":null,"errorMessage":"jnp.{name}: where must be None or a boolean array; got {where.dtype=}.","messagePattern":"jnp\\.(.+?): where must be None or a boolean array; got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/reductions.py","lineNumber":93,"sourceCode":"  # default dtype as defined by dtypes.int_ or dtypes.uint.\n  if dtypes.issubdtype(dtype, np.bool_):\n    return dtypes.default_int_dtype()\n  elif dtypes.issubdtype(dtype, np.unsignedinteger):\n    default_uint_dtype = dtypes.default_uint_dtype()\n    if np.iinfo(dtype).bits < np.iinfo(default_uint_dtype).bits:\n      return default_uint_dtype\n  elif dtypes.issubdtype(dtype, np.integer):\n    default_int_dtype = dtypes.default_int_dtype()\n    if np.iinfo(dtype).bits < np.iinfo(default_int_dtype).bits:\n      return default_int_dtype\n  return dtype\n\ndef check_where(name: str, where: ArrayLike | None) -> Array | None:\n  if where is None:\n    return where\n  where = ensure_arraylike(name, where)\n  if where.dtype != bool:\n    raise ValueError(\n      f\"jnp.{name}: where must be None or a boolean array; got {where.dtype=}.\"\n    )\n  return where\n\nReductionOp = Callable[[Any, Any], Any]\n\ndef _reduction(a: ArrayLike, name: str, op: ReductionOp, init_val: ArrayLike,\n               *, has_identity: bool = True,\n               preproc: Callable[[Array], Array] | None = None,\n               bool_op: ReductionOp | None = None,\n               upcast_f16_for_computation: bool = False,\n               axis: Axis = None, dtype: DTypeLike | None = None, out: None = None,\n               keepdims: bool = False, initial: ArrayLike | None = None,\n               where_: ArrayLike | None = None,\n               parallel_reduce: Callable[..., Array] | None = None,\n               promote_integers: bool = False) -> Array:\n  bool_op = bool_op or op\n  # Note: we must accept out=None as an argument, because numpy reductions delegate to","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/reductions.py#L75-L111","documentation":"JAX reductions (sum, mean, var, logsumexp, etc.) require the where mask to be a boolean array (or None). Unlike numpy, JAX will not implicitly cast integer/float masks to bool, because such casts are a common source of silent bugs under jit.","triggerScenarios":"Passing where=x > 0 works (bool), but where=int_mask or where=float weights array to jnp.sum/mean/var/logsumexp raises this. E.g. jnp.mean(a, where=jnp.array([1,0,1])).","commonSituations":"Porting numpy code that uses 0/1 integer masks; using a weights array where a mask was expected (should multiply instead); masks produced by arithmetic rather than comparisons.","solutions":["Convert the mask: where=mask.astype(bool)","Build masks with comparisons: where=x > threshold","For weights, multiply the data instead: jnp.sum(a * w) / jnp.sum(w)"],"exampleFix":"// before\njnp.mean(a, where=weights)  # weights is float array\n// after\njnp.sum(a * weights) / jnp.sum(weights)\n# or for a 0/1 mask:\njnp.mean(a, where=mask.astype(bool))","handlingStrategy":"type-guard","validationCode":"import jax.numpy as jnp\nmask = jnp.asarray(mask)\nif mask.dtype != jnp.bool_:\n    mask = mask.astype(bool)\njnp.mean(a, where=mask)","typeGuard":"def is_bool_mask(w) -> bool:\n    import jax.numpy as jnp\n    return hasattr(w, 'dtype') and jnp.asarray(w).dtype == jnp.bool_","tryCatchPattern":null,"preventionTips":["Always create masks via comparisons (x > t)","Never reuse weights arrays as where masks"],"tags":["jax","reductions","where-mask","dtype"],"backgroundTag":"mask-dtype-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}