{"record":{"id":"06eb43bf1c208e97","repo":"jax-ml/jax","slug":"dtype-argument-to-chisquare-must-be-a-float-dtyp","errorCode":null,"errorMessage":"dtype argument to `chisquare` must be a float dtype, got {dtype}","messagePattern":"dtype argument to `chisquare` must be a float dtype, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/random/core.py","lineNumber":2703,"sourceCode":"      :class:`~jax.sharding.NamedSharding`, a :class:`~jax.sharding.PartitionSpec`\n      (``P``), or ``None`` (default). When specified, the output will be sharded\n      according to the given sharding specification. Primarily used in explicit\n      sharding mode.\n      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 ``df.shape``.\n  \"\"\"\n  key, _ = _check_prng_key(\"chisquare\", key)\n  if method not in {\"exact\", \"approximate\"}:\n    raise ValueError(\"method argument to `chisquare` 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(\"dtype argument to `chisquare` must be a float \"\n                     f\"dtype, got {dtype}\")\n  shape = _check_broadcast_shapes(\"chisquare\", shape, df)\n  _check_all_safe_to_cast(\"chisquare\", dtype, df)\n  out_sharding = canonicalize_sharding_for_samplers(out_sharding, \"chisquare\", shape)\n  return maybe_auto_axes(_chisquare, out_sharding, method=method,\n                         shape=shape, dtype=dtype)(key, df)\n\n\n@jit(static_argnums=(2, 3, 4))\ndef _chisquare(key, df, method, shape, dtype) -> Array:\n  df = lax.convert_element_type(df, dtype)\n  two = lax._const(df, 2)\n  half_df = lax.div(df, two)\n  log_g = loggamma(key, a=half_df, shape=shape, dtype=dtype, method=method)\n  chi2 = lax.mul(jnp.exp(log_g), two)\n  return chi2\n\n","sourceCodeStart":2685,"sourceCodeEnd":2721,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/random/core.py#L2685-L2721","documentation":"jax.random.chisquare requires a floating-point dtype because it samples via gamma variates in float arithmetic. Integer or complex dtypes raise ValueError. Subsequent checks also require shape to broadcast against df.shape and df to be safely castable to dtype.","triggerScenarios":"jax.random.chisquare(key, df, dtype=jnp.int32) or any dtype where dtypes.issubdtype(dtype, np.floating) is False.","commonSituations":"Chi-square-like count data tempting int dtypes; shared dtype configs; porting numpy.random.chisquare which has no dtype parameter.","solutions":["Pass jnp.float32/jnp.float64 or omit dtype.","Check that shape broadcasts against df.shape and that df casts safely to the chosen dtype (e.g. avoid float32 dtype with float64 df under x64).","Validate configurable dtypes against np.floating."],"exampleFix":"// before\nx = jax.random.chisquare(key, 3.0, dtype=jnp.int32)\n\n// after\nx = jax.random.chisquare(key, 3.0, dtype=jnp.float32)","handlingStrategy":"type-guard","validationCode":"from jax._src import dtypes\nassert dtypes.issubdtype(dtypes.check_and_canonicalize_user_dtype(dtype or float), np.floating)","typeGuard":"def is_float_dtype(dtype) -> bool:\n    from jax._src import dtypes\n    import numpy as np\n    return dtypes.issubdtype(dtypes.check_and_canonicalize_user_dtype(dtype or float), np.floating)","tryCatchPattern":null,"preventionTips":["Chi-square samples are continuous — always float."],"tags":["jax","random","chisquare","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"}