{"record":{"id":"dfb1887e050ef05f","repo":"jax-ml/jax","slug":"dtype-argument-to-binomial-must-be-a-float-dtype","errorCode":null,"errorMessage":"dtype argument to `binomial` must be a float dtype, got {dtype}","messagePattern":"dtype argument to `binomial` must be a float dtype, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/random/core.py","lineNumber":3671,"sourceCode":"      representing the number of trials.\n    p: a float or array of floats broadcast-compatible with ``shape``\n      representing the probability of success of an individual trial.\n    shape: optional, a tuple of nonnegative integers specifying the result\n      shape. Must be broadcast-compatible with ``n`` and ``p``.\n      The default (None) produces a result shape equal to ``np.broadcast(n, p).shape``.\n    dtype: optional, a float dtype for the returned values (default float64 if\n      jax_enable_x64 is true, otherwise float32).\n\n  Returns:\n    A random array with the specified dtype and with shape given by\n    ``np.broadcast(n, p).shape``.\n  \"\"\"\n  key, _ = _check_prng_key(\"binomial\", key)\n  check_arraylike(\"binomial\", n, p)\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(\n        f\"dtype argument to `binomial` must be a float dtype, got {dtype}\"\n      )\n  if shape is not None:\n    shape = core.canonicalize_shape(shape)\n  return _binomial(key, n, p, shape, dtype)\n\n\n# Functions related to key reuse checking\nrandom_clone_p = core.Primitive(\"random_clone\")\ndispatch.simple_impl(random_clone_p)\nrandom_clone_p.def_abstract_eval(lambda x: x)\nbatching.defvectorized(random_clone_p)\nmlir.register_lowering(random_clone_p, lambda _, k: [k])\n\n\ndef multinomial(\n    key: Array,\n    n: RealArray,","sourceCodeStart":3653,"sourceCodeEnd":3689,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/random/core.py#L3653-L3689","documentation":"jax.random.binomial requires its dtype argument to be a floating-point dtype (default float), validated with dtypes.issubdtype(dtype, np.floating) after check_arraylike on n and p. Integer or bool dtypes raise this ValueError even though binomial counts are conceptually integral — JAX computes them in floating point.","triggerScenarios":"Calling jax.random.binomial(key, n, p, shape, dtype=np.int32) or dtype=np.bool_ despite n and p passing arraylike checks.","commonSituations":"Users assume a count-valued distribution should have an int dtype; porting NumPy/SciPy code that used int64 outputs.","solutions":["Omit dtype or pass jnp.float32/np.float64","Cast the result to int afterwards if needed: x.astype(jnp.int32)"],"exampleFix":"// before\nb = jax.random.binomial(key, 10, 0.5, dtype=jnp.int32)\n// after\nb = jax.random.binomial(key, 10, 0.5, dtype=jnp.float32).astype(jnp.int32)","handlingStrategy":"validation","validationCode":"import numpy as np\nassert dtype is None or np.issubdtype(np.dtype(dtype).type, np.floating), 'binomial needs float dtype (cast result to int afterwards)'","typeGuard":"def is_float_dtype(d) -> bool:\n    import numpy as np\n    return d is None or np.issubdtype(np.dtype(d).type, np.floating)","tryCatchPattern":null,"preventionTips":["Binomial returns float even though counts are integral — cast after","Check n and p are arraylike before the call"],"tags":["jax","random","dtype-validation"],"backgroundTag":"invalid-dtype-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}