{"record":{"id":"34053e2931f06224","repo":"jax-ml/jax","slug":"prng-key-seed-must-be-a-scalar-got-seed-r-34053e","errorCode":null,"errorMessage":"PRNG key seed must be a scalar; got {seed!r}.","messagePattern":"PRNG key seed must be a scalar; got (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/random/philox4x32.py","lineNumber":152,"sourceCode":"\ndef _is_philox4x32_key(key: typing.Array) -> bool:\n  \"\"\"Return True if key is a Philox 4x32 key.\"\"\"\n  try:\n    return key.shape == (2,) and key.dtype == np.uint32\n  except AttributeError:\n    return False\n\n\ndef philox4x32_seed(seed: typing.Array) -> typing.Array:\n  \"\"\"Create a single Philox 4x32 PRNG key from an integer seed.\"\"\"\n  return _philox4x32_seed(seed)\n\n\n@api.jit(inline=True)\ndef _philox4x32_seed(seed: typing.Array) -> typing.Array:\n  \"\"\"Internal implementation of philox4x32_seed.\"\"\"\n  if seed.shape:\n    raise TypeError(f\"PRNG key seed must be a scalar; got {seed!r}.\")\n  if not np.issubdtype(seed.dtype, np.integer):\n    raise TypeError(f\"PRNG key seed must be an integer; got {seed!r}\")\n  convert = lambda k: lax.convert_element_type(k, np.uint32)\n  k0 = convert(\n      lax.shift_right_logical(seed, lax.convert_element_type(32, seed.dtype))\n  )\n  with config.numpy_dtype_promotion(\"standard\"):\n    k1 = convert(jnp.bitwise_and(seed, np.uint32(0xFFFFFFFF)))\n  # Hash through philox4x32 to mix the seed bits into a 2-word key.\n  # Use the seed halves as counter words so both influence the output.\n  out = philox4x32_p.bind(\n      np.uint32(0),\n      np.uint32(0),\n      k0,\n      k1,\n      np.uint32(0),\n      np.uint32(0),\n  )","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/random/philox4x32.py#L134-L170","documentation":"philox4x32_seed requires a scalar seed; if seed.shape is non-empty it raises this TypeError with the offending value. This is the key-construction path for the Philox 4x32 PRNG implementation.","triggerScenarios":"Passing an array seed (e.g. np.array([1,2,3])) or a shape-(1,) array when creating a philox4x32 key.","commonSituations":"Vectorized per-sample seeds passed in one call instead of vmap; seeds unpacked from data files as arrays.","solutions":["Pass a scalar: PRNG seed of Python int or 0-d array","Use jax.vmap(philox4x32_seed) or jax.random.split for many keys","Squeeze accidental extra dims: seed.squeeze() before use"],"exampleFix":"# before\nseeds = np.array([1, 2, 3])\nkeys = philox4x32_seed(seeds)\n# after\nkeys = jax.vmap(philox4x32_seed)(seeds)","handlingStrategy":"validation","validationCode":"import numpy as np\nassert np.asarray(seed).ndim == 0, 'philox4x32 seed must be scalar'","typeGuard":"def is_scalar_seed(seed) -> bool:\n    import numpy as np\n    return np.asarray(seed).ndim == 0","tryCatchPattern":null,"preventionTips":["vmap the seed function for seed arrays","Squeeze stray (1,) dims before use"],"tags":["jax","prng","seed","philox"],"backgroundTag":"prng-seed-validation","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}