{"record":{"id":"debaabe909cfc6f0","repo":"jax-ml/jax","slug":"no-python-scalar-type-for-arr-dtype","errorCode":null,"errorMessage":"No Python scalar type for {arr.dtype=}","messagePattern":"No Python scalar type for (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/array_methods.py","lineNumber":277,"sourceCode":"  \"\"\"\n  return tensor_contractions.dot(self, b, precision=precision, preferred_element_type=preferred_element_type)\n\ndef _flatten(self: Array, order: str = \"C\", *, out_sharding=None) -> Array:\n  \"\"\"Flatten array into a 1-dimensional shape.\n\n  Refer to :func:`jax.numpy.ravel` for the full documentation.\n  \"\"\"\n  return lax_numpy.ravel(self, order=order, out_sharding=out_sharding)\n\ndef _imag_property(self: Array) -> Array:\n  \"\"\"Return the imaginary part of the array.\"\"\"\n  return ufuncs.imag(self)\n\ndef _item(self: Array, *args: int) -> bool | int | float | complex:\n  \"\"\"Copy an element of an array to a standard Python scalar and return it.\"\"\"\n  arr = core.concrete_or_error(np.asarray, self, context=\"This occurred in the item() method of jax.Array\")\n  if dtypes.issubdtype(self.dtype, dtypes.extended):\n    raise TypeError(f\"No Python scalar type for {arr.dtype=}\")\n  return arr.item(*args)\n\ndef _itemsize_property(self: Array) -> int:\n  \"\"\"Length of one array element in bytes.\"\"\"\n  return self.dtype.itemsize\n\ndef _matrix_transpose_property(self: Array):\n  \"\"\"Compute the (batched) matrix transpose.\n\n  Refer to :func:`jax.numpy.matrix_transpose` for details.\n  \"\"\"\n  return lax_numpy.matrix_transpose(self)\n\ndef _max(self: Array, axis: reductions.Axis = None, out: None = None,\n         keepdims: bool = False, initial: ArrayLike | None = None,\n         where: ArrayLike | None = None) -> Array:\n  \"\"\"Return the maximum of array elements along a given axis.\n","sourceCodeStart":259,"sourceCodeEnd":295,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/array_methods.py#L259-L295","documentation":"`Array.item()` converts an element to a Python scalar, but JAX's extended dtypes (e.g. its experimental bfloat16-adjacent extended types like key dtypes for random keys or custom tracers) have no corresponding Python scalar type, so conversion is impossible. The check is done on the concrete array after the tracer check.","triggerScenarios":"Calling `arr.item()` where `arr.dtype` is an extended dtype, most commonly `jax.random.key(...)` arrays with dtype `key<fry>`.","commonSituations":"Calling `.item()` on a jax.random.PRNGKey/key array to inspect the raw key; debugging code that dumps array contents via item().","solutions":["Convert to a regular uint32 representation first: `jax.random.key_data(arr).item()` or `jnp.asarray(key, dtype=jnp.uint32)`","Use `jax.random.bits(key)` for raw bits","Avoid .item() on key arrays; print the array itself for debugging"],"exampleFix":"# before\nkey = jax.random.key(0)\nkey.item()  # TypeError\n\n# after\nraw = jax.random.key_data(key)  # uint32 array\nraw.item()","handlingStrategy":"validation","validationCode":"from jax import dtypes\n\ndef safe_item(arr):\n    if dtypes.issubdtype(arr.dtype, dtypes.extended):\n        raise ValueError('cannot .item() an extended-dtype array')\n    return arr.item()","typeGuard":"def has_python_scalar_dtype(arr) -> bool:\n    from jax import dtypes\n    return not dtypes.issubdtype(arr.dtype, dtypes.extended)","tryCatchPattern":null,"preventionTips":["Use jax.random.key_data for raw key bits","Check dtype before generic .item() dumps"],"tags":["jax","dtype","item","extended-dtype"],"backgroundTag":"unsupported-dtype-conversion","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}