{"record":{"id":"f1018665ce613075","repo":"jax-ml/jax","slug":"dtype-argument-to-laplace-must-be-a-float-dtype","errorCode":null,"errorMessage":"dtype argument to `laplace` must be a float dtype, got {dtype}","messagePattern":"dtype argument to `laplace` must be a float dtype, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/random/core.py","lineNumber":2468,"sourceCode":"    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 shape and dtype.\n  \"\"\"\n  key, _ = _check_prng_key(\"laplace\", key)\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 `laplace` must be a float \"\n                     f\"dtype, got {dtype}\")\n  shape = core.canonicalize_shape(shape)\n  out_sharding = canonicalize_sharding_for_samplers(out_sharding, \"laplace\", shape)\n  return maybe_auto_axes(_laplace, out_sharding,\n                         shape=shape, dtype=dtype)(key)\n\n@jit(static_argnums=(1, 2))\ndef _laplace(key, shape, dtype) -> Array:\n  _check_shape(\"laplace\", shape)\n  u = uniform(\n      key, shape, dtype, minval=-1. + dtypes.finfo(dtype).epsneg, maxval=1.)\n  return lax.mul(lax.sign(u), lax.log1p(lax.neg(lax.abs(u))))\n\n\ndef logistic(key: ArrayLike,\n             shape: Shape = (),\n             dtype: DTypeLikeFloat | None = None,\n             *,","sourceCodeStart":2450,"sourceCodeEnd":2486,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/random/core.py#L2450-L2486","documentation":"jax.random.laplace requires a floating-point dtype because the Laplace sampler uses log/exp float transforms of uniform bits. Non-float dtypes (int, complex) raise ValueError before tracing.","triggerScenarios":"jax.random.laplace(key, shape, dtype=jnp.int32) or any dtype failing dtypes.issubdtype(dtype, np.floating).","commonSituations":"Shared dtype constants across samplers; porting noise-generation code that previously used int-based samplers; debugging scripts that hard-code one dtype for all distributions.","solutions":["Pass jnp.float32/jnp.float64 or omit dtype.","Validate configurable dtypes against np.floating before calling.","For integer Laplace-like noise (rare), sample in float then convert with jnp.round(...).astype(jnp.int32) afterwards."],"exampleFix":"// before\nx = jax.random.laplace(key, (100,), dtype=jnp.int32)\n\n// after\nx = jax.random.laplace(key, (100,), 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":["Omit dtype unless a specific float width is needed."],"tags":["jax","random","laplace","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"}