{"record":{"id":"dc81fd1622fffd84","repo":"jax-ml/jax","slug":"dtype-argument-to-gamma-must-be-a-float-dtype-g","errorCode":null,"errorMessage":"dtype argument to `gamma` must be a float dtype, got {dtype}","messagePattern":"dtype argument to `gamma` must be a float dtype, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/random/core.py","lineNumber":1691,"sourceCode":"      See the `explicit sharding tutorial <https://docs.jax.dev/en/latest/parallel.html>`_\n      for more details.\n\n  Returns:\n    A random array with the specified dtype and with shape given by ``shape`` if\n    ``shape`` is not None, or else by ``a.shape``.\n\n  See Also:\n    loggamma : sample gamma values in log-space, which can provide improved\n      accuracy for small values of ``a``.\n  \"\"\"\n  key, _ = _check_prng_key(\"gamma\", key)\n  if method not in {'exact', 'approximate'}:\n    raise ValueError(\"method argument to `gamma` must be one of \"\n                     f\"{{'exact', 'approximate'}}, got {method!r}\")\n  dtype = dtypes.check_and_canonicalize_user_dtype(\n      float if dtype is None else dtype)\n  if not dtypes.issubdtype(dtype, np.floating):\n    raise ValueError(f\"dtype argument to `gamma` must be a float \"\n                     f\"dtype, got {dtype}\")\n  if shape is not None:\n    shape = core.canonicalize_shape(shape)\n  out_sharding = canonicalize_sharding_for_samplers(out_sharding, \"gamma\", shape)\n  if method == 'approximate':\n    return maybe_auto_axes(_gamma_approx, out_sharding,\n                           shape=shape, dtype=dtype)(key, a)\n  return maybe_auto_axes(_gamma, out_sharding, shape=shape, dtype=dtype)(key, a)\n\n\ndef loggamma(key: ArrayLike,\n             a: RealArray,\n             shape: Shape | None = None,\n             dtype: DTypeLikeFloat | None = None,\n             *,\n             method: str = 'exact',\n             out_sharding: NamedSharding | P | None =None) -> Array:\n  \"\"\"Sample log-gamma random values with given shape and float dtype.","sourceCodeStart":1673,"sourceCodeEnd":1709,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/random/core.py#L1673-L1709","documentation":"jax.random.gamma requires a floating-point dtype because it performs Gamma-distribution math (log/exp transforms, rejection sampling) that only makes sense in float arithmetic. Integer or complex dtypes are rejected with ValueError before the sampler is traced.","triggerScenarios":"Calling jax.random.gamma(key, a, dtype=jnp.int32) or dtype=jnp.complex64; passing a canonicalized custom dtype that resolves to a non-float type.","commonSituations":"Reusing one dtype variable across many samplers some of which are int-only (poisson); porting NumPy code where numpy.random.gamma had no dtype argument; f64 support toggles where a bfloat16/float16 choice fails upstream validation elsewhere and int is substituted during debugging.","solutions":["Pass jnp.float32 or jnp.float64 (with jax_enable_x64=True), or omit dtype to get the default float.","Validate the dtype with dtypes.issubdtype(dtype, np.floating) before the call in configurable pipelines.","Use jax.random.loggamma instead if you need small-alpha accuracy, still with a float dtype."],"exampleFix":"// before\ng = jax.random.gamma(key, 3.0, dtype=jnp.int32)\n\n// after\ng = jax.random.gamma(key, 3.0, dtype=jnp.float32)","handlingStrategy":"type-guard","validationCode":"from jax._src import dtypes\nimport numpy as np\ndtype = dtype or float\nassert dtypes.issubdtype(dtypes.check_and_canonicalize_user_dtype(dtype), np.floating)","typeGuard":"def is_float_dtype(dtype) -> bool:\n    return dtypes.issubdtype(dtypes.check_and_canonicalize_user_dtype(dtype or float), np.floating)","tryCatchPattern":null,"preventionTips":["Never pass int dtypes to continuous-distribution samplers.","Omit dtype unless you specifically need f16/f64."],"tags":["jax","random","gamma","dtype","input-validation"],"backgroundTag":"invalid-dtype-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}