jax-ml/jax · error · ValueError

method argument to `loggamma` must be one of {'exact', 'appr

Error message

method argument to `loggamma` must be one of {'exact', 'approximate'}, got {method!r}

What it means

jax.random.loggamma accepts only method='exact' or method='approximate', mirroring jax.random.gamma since it is the log-space variant of the same sampler. Any other value is rejected with ValueError because no other sampling algorithm exists.

Source

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

    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 ``a.shape``.

  See Also:
    gamma : standard gamma sampler.
  """
  key, _ = _check_prng_key("loggamma", key)
  if method not in {'exact', 'approximate'}:
    raise ValueError("method argument to `loggamma` must be one of "
                     f"{{'exact', 'approximate'}}, got {method!r}")
  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 `gamma` must be a float "
                     f"dtype, got {dtype}")
  if shape is not None:
    shape = core.canonicalize_shape(shape)
  out_sharding = canonicalize_sharding(out_sharding, "loggamma")
  if method == 'approximate':
    return maybe_auto_axes(_gamma_approx, out_sharding, shape=shape,
                           dtype=dtype, log_space=True)(key, a)
  return maybe_auto_axes(_gamma, out_sharding, shape=shape, dtype=dtype, log_space=True)(key, a)


@jit(static_argnames=('shape', 'dtype', 'log_space'))
def _gamma(key, a, shape, dtype, log_space=False) -> Array:
  if shape is None:

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Use method='exact' (default) or method='approximate' verbatim.
  2. Validate config-driven method strings against the allowed set before use.
  3. Omit the method argument to get the 'exact' default.

Example fix

// before
lg = jax.random.loggamma(key, 0.01, method=None)

// after
lg = jax.random.loggamma(key, 0.01, method='exact')
Defensive patterns

Strategy: validation

Validate before calling

method = method or 'exact'
assert method in {'exact', 'approximate'}

Type guard

def is_valid_loggamma_method(method: str) -> bool:
    return method in {'exact', 'approximate'}

Prevention

When it happens

Trigger: Calling jax.random.loggamma(key, a, method=None), method='Fast', or any string not in {'exact','approximate'}.

Common situations: Sharing a method setting across gamma/loggamma/beta/dirichlet calls from one config string; typos; assuming None selects a default (the default is 'exact' via the keyword default, but explicitly passing None is not allowed).

Related errors


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