jax-ml/jax · error · ValueError

dtype argument to `triangular` must be a float dtype, got {d

Error message

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

What it means

jax.random.triangular requires a floating-point dtype because its output is continuous. The canonicalized dtype must satisfy dtypes.issubdtype(dtype, np.floating); integer, bool, or complex dtypes raise this ValueError.

Source

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

      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 dtype and with shape given by ``shape`` if
    ``shape`` is not None, or else by ``left.shape``, ``mode.shape`` and ``right.shape``.
  """
  key, _ = _check_prng_key("triangular", key)
  dtype = dtypes.check_and_canonicalize_user_dtype(
      float if dtype is None else dtype)
  if not dtypes.issubdtype(dtype, np.floating):
    raise ValueError("dtype argument to `triangular` must be a float "
                     f"dtype, got {dtype}")
  shape = _check_broadcast_shapes("triangular", shape, left, mode, right)
  out_sharding = canonicalize_sharding_for_samplers(out_sharding, "triangular", shape)
  _check_all_safe_to_cast("triangular", dtype, left, mode, right)
  return maybe_auto_axes(_triangular, out_sharding, shape=shape, dtype=dtype)(key, left, mode, right)

@jit(static_argnums=(4, 5), inline=True)
def _triangular(key, left, mode, right, shape, dtype) -> Array:
  # https://en.wikipedia.org/wiki/Triangular_distribution#Generating_triangular-distributed_random_variates
  left = jnp.broadcast_to(lax.convert_element_type(left, dtype), shape)
  right = jnp.broadcast_to(lax.convert_element_type(right, dtype), shape)
  mode = jnp.broadcast_to(lax.convert_element_type(mode, dtype), shape)
  fc = (mode - left) / (right - left)
  u = uniform(key, shape, dtype)
  out1 = left + lax.sqrt(u * (right - left) * (mode - left))
  out2 = right - lax.sqrt((1 - u) * (right - left) * (right - mode))
  tri = lax.select(u < fc, out1, out2)
  return tri

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Omit dtype or pass jnp.float32/np.float64
  2. Ensure left/mode/right are float arrays so _check_all_safe_to_cast also passes

Example fix

// before
t = jax.random.triangular(key, 0.0, 0.5, 1.0, dtype=jnp.int32)
// after
t = jax.random.triangular(key, 0.0, 0.5, 1.0, dtype=jnp.float32)
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
assert dtype is None or np.issubdtype(np.dtype(dtype).type, np.floating), 'triangular needs float dtype'

Type guard

def is_float_dtype(d) -> bool:
    import numpy as np
    return d is None or np.issubdtype(np.dtype(d).type, np.floating)

Prevention

When it happens

Trigger: Calling jax.random.triangular(key, left, mode, right, shape, dtype) with a non-float dtype like np.int16 or jnp.complex64.

Common situations: Sharing one dtype config across a simulation that mixes discrete and continuous noise; passing bfloat16 works, but passing bool/int does not.

Related errors


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