jax-ml/jax · error · ValueError

dtype argument to jnp.std must be inexact; got {dtype}

Error message

dtype argument to jnp.std must be inexact; got {dtype}

What it means

jnp.std computes a floating-point result, so its dtype parameter must be an inexact type (float or complex). Passing an integer or boolean dtype raises ValueError after dtype canonicalization.

Source

Thrown at jax/_src/numpy/reductions.py:1282

    Array([[2., 1., 1., 0.]], dtype=float32)
  """
  if correction is None:
    correction = ddof
  elif not isinstance(ddof, int) or ddof != 0:
    raise ValueError("ddof and correction can't be provided simultaneously.")
  a = ensure_arraylike("std", a)
  return _std(a, axis=_ensure_optional_axes(axis), dtype=dtype, out=out, correction=correction, keepdims=keepdims,
              where=where, mean=mean)

@api.jit(static_argnames=('axis', 'dtype', 'keepdims'))
def _std(a: Array, *, axis: Axis = None, dtype: DTypeLike | None = None,
         out: None = None, correction: int | float = 0, keepdims: bool = False,
         where: ArrayLike | None = None, mean: ArrayLike | None = None) -> Array:
  where = check_where("std", where)
  if dtype is not None:
    dtype = dtypes.check_and_canonicalize_user_dtype(dtype, "std")
    if not dtypes.issubdtype(dtype, np.inexact):
      raise ValueError(f"dtype argument to jnp.std must be inexact; got {dtype}")
  if out is not None:
    raise NotImplementedError("The 'out' argument to jnp.std is not supported.")
  return lax.sqrt(var(a, axis=axis, dtype=dtype, correction=correction,
                      keepdims=keepdims, where=where, mean=mean))


@export
def ptp(a: ArrayLike, axis: Axis = None, out: None = None,
        keepdims: bool = False) -> Array:
  r"""Return the peak-to-peak range along a given axis.

  JAX implementation of :func:`numpy.ptp`.

  Args:
    a: input array.
    axis: optional, int or sequence of ints, default=None. Axis along which the
      range is computed. If None, the range is computed on the flattened array.
    keepdims: bool, default=False. If true, reduced axes are left in the result

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Omit dtype — JAX picks the default inexact dtype for integer inputs
  2. Pass an inexact dtype such as jnp.float32 or jnp.float64
  3. In generic code, map integer dtypes via jnp.promote_types(x.dtype, jnp.float32)

Example fix

// before
jnp.std(int_array, dtype=int_array.dtype)
// after
jnp.std(int_array, dtype=jnp.promote_types(int_array.dtype, jnp.float32))
Defensive patterns

Strategy: type-guard

Validate before calling

import jax.numpy as jnp, numpy as np

def std_dtype(x, dtype=None):
    if dtype is None:
        return None
    if not np.issubdtype(dtype, np.inexact):
        return jnp.promote_types(dtype, jnp.float32)
    return dtype

Type guard

import numpy as np

def is_inexact(dtype) -> bool:
    return np.issubdtype(dtype, np.inexact)

Prevention

When it happens

Trigger: Calling jnp.std(x, dtype=jnp.int32) or dtype=np.int64; any non-inexact canonicalized dtype reaching _std.

Common situations: Copying dtype from the input array (e.g. dtype=x.dtype where x is int) to preserve precision; generic reduction helpers that forward a user dtype verbatim.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/a2acdb60ba0622c5. Report an issue: GitHub.