jax-ml/jax · error · ValueError

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

Error message

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

What it means

jax.random.t (Student's t) requires a floating-point dtype; the sampler combines gamma and normal variates in float arithmetic. Non-float dtypes raise ValueError. Additionally shape must broadcast against df.shape and df must be safely castable to dtype, both checked right after this dtype check.

Source

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

      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 ``df.shape``.
  """
  key, _ = _check_prng_key("t", 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 `t` must be a float "
                     f"dtype, got {dtype}")
  shape = _check_broadcast_shapes("t", shape, df)
  out_sharding = canonicalize_sharding_for_samplers(out_sharding, "t", shape)
  _check_all_safe_to_cast("t", dtype, df)
  return maybe_auto_axes(_t, out_sharding,
                         shape=shape, dtype=dtype)(key, df)

@jit(static_argnums=(2, 3))
def _t(key, df, shape, dtype) -> Array:
  if shape is None:
    shape = np.shape(df)
  else:
    _check_shape("t", shape, np.shape(df))

  df = lax.convert_element_type(df, dtype)
  key_n, key_g = _split(key)
  n = normal(key_n, shape, dtype)
  two = lax._const(n, 2)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Pass jnp.float32/jnp.float64 or omit dtype.
  2. Verify shape broadcasts against np.shape(df) to avoid the follow-up _check_broadcast_shapes error.
  3. Validate configurable dtypes against np.floating before the call.

Example fix

// before
x = jax.random.t(key, 5.0, dtype=jnp.int32)

// after
x = jax.random.t(key, 5.0, 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.t(key, df, dtype=jnp.int32) or any dtype failing dtypes.issubdtype(dtype, np.floating).

Common situations: Sampling noise models with a config-supplied dtype; porting scipy.stats.t code; using one dtype constant across a benchmark suite of samplers.

Related errors


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