jax-ml/jax · error · ValueError

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

Error message

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

What it means

jax.random.f (F-distribution) requires a floating-point dtype; the sampler combines two chi-square/gamma variates in float arithmetic. Non-float dtypes raise ValueError. The call also validates that shape broadcasts against both dfnum.shape and dfden.shape and that both cast safely to dtype.

Source

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

      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("f", 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 `f` must be a float "
                     f"dtype, got {dtype}")
  shape = _check_broadcast_shapes("f", shape, dfnum, dfden)
  out_sharding = canonicalize_sharding_for_samplers(out_sharding, "f", shape)
  _check_all_safe_to_cast("f", dtype, dfnum, dfden)
  return _f(key, dfnum, dfden, shape, dtype, out_sharding)

@jit(static_argnums=(3, 4, 5))
def _f(key, dfnum, dfden, shape, dtype, out_sharding) -> Array:
  dfden = lax.convert_element_type(dfden, dtype)
  dfnum = lax.convert_element_type(dfnum, dtype)
  key_dfd, key_dfn = _split(key)
  chi2_dfn = chisquare(key_dfn, dfnum, shape, dtype, out_sharding=out_sharding)
  chi2_dfd = chisquare(key_dfd, dfden, shape, dtype, out_sharding=out_sharding)
  num = lax.div(chi2_dfn, dfnum)
  den = lax.div(chi2_dfd ,dfden)
  f = lax.div(num, den)
  return f

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Pass jnp.float32/jnp.float64 or omit dtype.
  2. Ensure shape broadcasts against both dfnum and dfden shapes (both are checked via _check_broadcast_shapes).
  3. Validate configurable dtypes against np.floating and watch for the safe-cast check on dfnum/dfden.

Example fix

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

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

Common situations: Statistical testing utilities with a configurable dtype; sharing dtype across samplers; passing degrees-of-freedom arrays whose float64 values must be safely castable to a narrower dtype.

Related errors


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