{"record":{"id":"2183f56e1a4a19e9","repo":"jax-ml/jax","slug":"dtype-argument-to-lognormal-must-be-a-float-or-c","errorCode":null,"errorMessage":"dtype argument to `lognormal` must be a float or complex dtype, got {dtype}","messagePattern":"dtype argument to `lognormal` must be a float or complex dtype, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/random/core.py","lineNumber":3459,"sourceCode":"      shape. The default (None) produces a result shape equal to ``()``.\n    dtype: optional, a float dtype for the returned values (default float64 if\n      jax_enable_x64 is true, otherwise float32).\n    out_sharding: optional, specifies how the output array should be sharded\n      across devices in multi-device computation. Can be a\n      :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``.\n  \"\"\"\n  key, _ = _check_prng_key(\"lognormal\", key)\n  dtype = dtypes.check_and_canonicalize_user_dtype(float if dtype is None else dtype)\n  if not dtypes.issubdtype(dtype, np.inexact):\n    raise ValueError(f\"dtype argument to `lognormal` must be a float or complex dtype, \"\n                    f\"got {dtype}\")\n  shape = _check_broadcast_shapes(\"lognormal\", shape, sigma)\n  out_sharding = canonicalize_sharding(out_sharding, \"lognormal\")\n  _check_all_safe_to_cast(\"lognormal\", dtype, sigma)\n  return maybe_auto_axes(_lognormal, out_sharding, shape=shape, dtype=dtype)(key, sigma)\n\n@jit(static_argnums=(2, 3), inline=True)\ndef _lognormal(key, sigma, shape, dtype) -> Array:\n  sigma = lax.convert_element_type(sigma, dtype)\n  scaled_norm = normal(key, shape, dtype) * sigma\n  return lax.exp(scaled_norm)\n\n\ndef _stirling_approx_tail(k):\n  stirling_tail_vals = jnp.array(\n      [\n          0.0810614667953272,\n          0.0413406959554092,","sourceCodeStart":3441,"sourceCodeEnd":3477,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/random/core.py#L3441-L3477","documentation":"jax.random.lognormal accepts any inexact dtype (float or complex), checked via dtypes.issubdtype(dtype, np.inexact). Integer and boolean dtypes fail this check and raise a ValueError; complex dtypes are allowed here unlike most samplers.","triggerScenarios":"Calling jax.random.lognormal(key, sigma, shape, dtype=np.int32) or dtype=np.bool_.","commonSituations":"Trying to generate lognormal counts as integers directly; complex output is supported but int is not, which surprises users porting NumPy code.","solutions":["Pass a float dtype (or complex if intended), or omit dtype","Sample as float and round/cast afterwards if integer-like output is needed"],"exampleFix":"// before\nx = jax.random.lognormal(key, sigma, dtype=jnp.int32)\n// after\nx = jax.random.lognormal(key, sigma, dtype=jnp.float32)","handlingStrategy":"validation","validationCode":"import numpy as np\nassert dtype is None or np.issubdtype(np.dtype(dtype).type, np.inexact), 'lognormal needs float or complex dtype'","typeGuard":"def is_inexact_dtype(d) -> bool:\n    import numpy as np\n    return d is None or np.issubdtype(np.dtype(d).type, np.inexact)","tryCatchPattern":null,"preventionTips":["lognormal uniquely allows complex; all int dtypes are rejected","Sample float then round if integer output desired"],"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"}