jax-ml/jax · error · ValueError
method argument to `gamma` must be one of {'exact', 'approxi
Error message
method argument to `gamma` must be one of {'exact', 'approximate'}, got {method!r} What it means
jax.random.gamma accepts only method='exact' (Marsaglia-Tsang rejection sampling, used under jit) or method='approximate' (a faster, less accurate transform). Any other string (including typos or None) is rejected with ValueError because there is no fallback sampling algorithm.
Source
Thrown at jax/_src/random/core.py:1686
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:
loggamma : sample gamma values in log-space, which can provide improved
accuracy for small values of ``a``.
"""
key, _ = _check_prng_key("gamma", key)
if method not in {'exact', 'approximate'}:
raise ValueError("method argument to `gamma` 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_for_samplers(out_sharding, "gamma", shape)
if method == 'approximate':
return maybe_auto_axes(_gamma_approx, out_sharding,
shape=shape, dtype=dtype)(key, a)
return maybe_auto_axes(_gamma, out_sharding, shape=shape, dtype=dtype)(key, a)
def loggamma(key: ArrayLike,
a: RealArray,
shape: Shape | None = None,View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Use method='exact' (default) or method='approximate', matching exactly including case.
- If the method comes from a config, validate it against {'exact','approximate'} at load time with a clear error listing valid values.
- Check the jax.random.gamma docstring for your JAX version to confirm supported methods.
Example fix
// before samples = jax.random.gamma(key, 2.0, method='fast') // after samples = jax.random.gamma(key, 2.0, method='approximate')
Defensive patterns
Strategy: validation
Validate before calling
method = method or 'exact'
assert method in {'exact', 'approximate'}, f"invalid gamma method: {method!r}" Type guard
def is_valid_gamma_method(method: str) -> bool:
return method in {'exact', 'approximate'} Prevention
- Validate method strings when loading config.
- Omit method to use the 'exact' default.
When it happens
Trigger: Calling jax.random.gamma(key, a, method='fast'), method=None, method=' Exact' (case/whitespace typo), or any string outside {'exact','approximate'}.
Common situations: Typos in hyperparameter configs; passing a method intended for a different library (e.g. numpy Generator method names); a version change where a previously tolerated value was removed or the argument was newly added.
Related errors
- dtype argument to `gamma` must be a float dtype, got {dtype}
- method argument to `loggamma` must be one of {'exact', 'appr
- method argument to `poisson` must be one of {'exact', 'appro
- method argument to `chisquare` must be one of {'exact', 'app
- dirichlet requires alpha.ndim >= 1, got alpha.ndim == {}
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/99e69c727a999a1b.
Report an issue: GitHub.