jax-ml/jax · error · ValueError

method argument to `chisquare` must be one of {'exact', 'app

Error message

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

What it means

jax.random.chisquare accepts only method='exact' or method='approximate', mirroring the gamma/poisson samplers it builds on. Any other value raises ValueError before the dtype and shape checks run.

Source

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

      sampler. The ``'approximate'`` method is loop-free and faster but carries
      a small bias. The gradient w.r.t. ``df`` differs between the two
      methods because of the ambiguity in defining a gradient for random variates.
    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("chisquare", key)
  if method not in {"exact", "approximate"}:
    raise ValueError("method argument to `chisquare` 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("dtype argument to `chisquare` must be a float "
                     f"dtype, got {dtype}")
  shape = _check_broadcast_shapes("chisquare", shape, df)
  _check_all_safe_to_cast("chisquare", dtype, df)
  out_sharding = canonicalize_sharding_for_samplers(out_sharding, "chisquare", shape)
  return maybe_auto_axes(_chisquare, out_sharding, method=method,
                         shape=shape, dtype=dtype)(key, df)


@jit(static_argnums=(2, 3, 4))
def _chisquare(key, df, method, shape, dtype) -> Array:
  df = lax.convert_element_type(df, dtype)
  two = lax._const(df, 2)
  half_df = lax.div(df, two)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Use exactly 'exact' (default) or 'approximate'.
  2. Omit the method argument for the default 'exact'.
  3. Validate config-supplied method strings against the allowed set at load time.

Example fix

// before
x = jax.random.chisquare(key, 3.0, method='Gamma')

// after
x = jax.random.chisquare(key, 3.0, method='exact')
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: jax.random.chisquare(key, df, method='Gamma'), method=None passed explicitly, or any string outside {'exact','approximate'}.

Common situations: Config strings shared across gamma/poisson/chisquare; typos; discovering the argument from a signature and guessing values.

Related errors


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