jax-ml/jax · error · ValueError

dtype argument to `laplace` must be a float dtype, got {dtyp

Error message

dtype argument to `laplace` must be a float dtype, got {dtype}

What it means

jax.random.laplace requires a floating-point dtype because the Laplace sampler uses log/exp float transforms of uniform bits. Non-float dtypes (int, complex) raise ValueError before tracing.

Source

Thrown at jax/_src/random/core.py:2468

    dtype: optional, a float dtype for the returned values (default float64 if
      jax_enable_x64 is true, otherwise float32).
    out_sharding: Optional. Specifies how the output array should be sharded
      across devices in multi-device computation. Can be a
      :class:`~jax.sharding.NamedSharding`, a :class:`~jax.sharding.PartitionSpec`
      (``P``), or ``None`` (default). When specified, the output will be sharded
      according to the given sharding specification. Primarily used in explicit
      sharding mode.
      See the `explicit sharding tutorial <https://docs.jax.dev/en/latest/parallel.html>`_
      for more details.

  Returns:
    A random array with the specified shape and dtype.
  """
  key, _ = _check_prng_key("laplace", key)
  dtype = dtypes.check_and_canonicalize_user_dtype(
      float if dtype is None else dtype)
  if not dtypes.issubdtype(dtype, np.floating):
    raise ValueError(f"dtype argument to `laplace` must be a float "
                     f"dtype, got {dtype}")
  shape = core.canonicalize_shape(shape)
  out_sharding = canonicalize_sharding_for_samplers(out_sharding, "laplace", shape)
  return maybe_auto_axes(_laplace, out_sharding,
                         shape=shape, dtype=dtype)(key)

@jit(static_argnums=(1, 2))
def _laplace(key, shape, dtype) -> Array:
  _check_shape("laplace", shape)
  u = uniform(
      key, shape, dtype, minval=-1. + dtypes.finfo(dtype).epsneg, maxval=1.)
  return lax.mul(lax.sign(u), lax.log1p(lax.neg(lax.abs(u))))


def logistic(key: ArrayLike,
             shape: Shape = (),
             dtype: DTypeLikeFloat | None = None,
             *,

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Pass jnp.float32/jnp.float64 or omit dtype.
  2. Validate configurable dtypes against np.floating before calling.
  3. For integer Laplace-like noise (rare), sample in float then convert with jnp.round(...).astype(jnp.int32) afterwards.

Example fix

// before
x = jax.random.laplace(key, (100,), dtype=jnp.int32)

// after
x = jax.random.laplace(key, (100,), dtype=jnp.float32)
Defensive patterns

Strategy: type-guard

Validate before calling

from jax._src import dtypes
assert dtypes.issubdtype(dtypes.check_and_canonicalize_user_dtype(dtype or float), np.floating)

Type guard

def is_float_dtype(dtype) -> bool:
    from jax._src import dtypes
    import numpy as np
    return dtypes.issubdtype(dtypes.check_and_canonicalize_user_dtype(dtype or float), np.floating)

Prevention

When it happens

Trigger: jax.random.laplace(key, shape, dtype=jnp.int32) or any dtype failing dtypes.issubdtype(dtype, np.floating).

Common situations: Shared dtype constants across samplers; porting noise-generation code that previously used int-based samplers; debugging scripts that hard-code one dtype for all distributions.

Related errors


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