{"record":{"id":"d8e164344d2c3f5f","repo":"jax-ml/jax","slug":"where-argument-must-have-dtype-bool-got-dtype-la","errorCode":null,"errorMessage":"where argument must have dtype=bool; got dtype={lax._dtype(where)}","messagePattern":"where argument must have dtype=bool; got dtype=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/ufunc_api.py","lineNumber":256,"sourceCode":"      >>> jnp.bitwise_or.reduce(x, axis=1)\n      Array([3, 7], dtype=int32)\n    \"\"\"\n    check_arraylike(f\"{self.__name__}.reduce\", a)\n    if self.nin != 2:\n      raise ValueError(\"reduce only supported for binary ufuncs\")\n    if self.nout != 1:\n      raise ValueError(\"reduce only supported for functions returning a single value\")\n    if out is not None:\n      raise NotImplementedError(f\"out argument of {self.__name__}.reduce()\")\n    if initial is not None:\n      check_arraylike(f\"{self.__name__}.reduce\", initial)\n    if where is not None:\n      check_arraylike(f\"{self.__name__}.reduce\", where)\n      if self.identity is None and initial is None:\n        raise ValueError(f\"reduction operation {self.__name__!r} does not have an identity, \"\n                         \"so to use a where mask one has to specify 'initial'.\")\n      if lax._dtype(where) != bool:\n        raise ValueError(f\"where argument must have dtype=bool; got dtype={lax._dtype(where)}\")\n    reduce = self.__static_props['reduce'] or self._reduce_via_scan\n    return reduce(a, axis=axis, dtype=dtype, keepdims=keepdims, initial=initial, where=where)\n\n  def _reduce_via_scan(self, arr: ArrayLike, axis: int | tuple[int, ...] | None = 0, dtype: DTypeLike | None = None,\n                       keepdims: bool = False, initial: ArrayLike | None = None,\n                       where: ArrayLike | None = None) -> Array:\n    assert self.nin == 2 and self.nout == 1\n    arr = lax.asarray(arr)\n    if initial is None:\n      initial = self.identity\n    if dtype is None:\n      dtype = api.eval_shape(self._func, lax._one(arr), lax._one(arr)).dtype\n    if where is not None:\n      where = _broadcast_to(where, arr.shape)\n    if isinstance(axis, tuple):\n      axis = tuple(canonicalize_axis(a, arr.ndim) for a in axis)\n      raise NotImplementedError(\"tuple of axes\")\n    elif axis is None:","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/ufunc_api.py#L238-L274","documentation":"ufunc.reduce's where mask selects which elements participate in the reduction and must be a boolean array. Passing a non-bool mask (e.g. int 0/1 array or float mask) raises this ValueError reporting the offending dtype.","triggerScenarios":"jnp.add.reduce(x, where=jnp.array([1,0,1])) — an int32/int64 mask instead of bool.","commonSituations":"Masks loaded from files or produced by arithmetic comparisons of integers; numpy code where int masks were implicitly truthy.","solutions":["Convert the mask: where=mask.astype(bool)","Create masks with comparison operators (x > 0) which naturally yield bool"],"exampleFix":"// before\njnp.add.reduce(x, where=jnp.array([1,0,1]))\n// after\njnp.add.reduce(x, where=jnp.array([1,0,1], dtype=bool))","handlingStrategy":"validation","validationCode":"where = where.astype(bool) if where is not None else where","typeGuard":"def is_bool_mask(m): return m is None or lax.dtype(m) == jnp.bool_","tryCatchPattern":null,"preventionTips":["Build masks from comparisons (x > t) so they are bool by construction"],"tags":["jax","ufunc","reduce","dtype","where-mask"],"backgroundTag":"mask-dtype-must-be-bool","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}