jax-ml/jax · error · TypeError

No Python scalar type for {arr.dtype=}

Error message

No Python scalar type for {arr.dtype=}

What it means

`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.

Source

Thrown at jax/_src/numpy/array_methods.py:277

  """
  return tensor_contractions.dot(self, b, precision=precision, preferred_element_type=preferred_element_type)

def _flatten(self: Array, order: str = "C", *, out_sharding=None) -> Array:
  """Flatten array into a 1-dimensional shape.

  Refer to :func:`jax.numpy.ravel` for the full documentation.
  """
  return lax_numpy.ravel(self, order=order, out_sharding=out_sharding)

def _imag_property(self: Array) -> Array:
  """Return the imaginary part of the array."""
  return ufuncs.imag(self)

def _item(self: Array, *args: int) -> bool | int | float | complex:
  """Copy an element of an array to a standard Python scalar and return it."""
  arr = core.concrete_or_error(np.asarray, self, context="This occurred in the item() method of jax.Array")
  if dtypes.issubdtype(self.dtype, dtypes.extended):
    raise TypeError(f"No Python scalar type for {arr.dtype=}")
  return arr.item(*args)

def _itemsize_property(self: Array) -> int:
  """Length of one array element in bytes."""
  return self.dtype.itemsize

def _matrix_transpose_property(self: Array):
  """Compute the (batched) matrix transpose.

  Refer to :func:`jax.numpy.matrix_transpose` for details.
  """
  return lax_numpy.matrix_transpose(self)

def _max(self: Array, axis: reductions.Axis = None, out: None = None,
         keepdims: bool = False, initial: ArrayLike | None = None,
         where: ArrayLike | None = None) -> Array:
  """Return the maximum of array elements along a given axis.

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Convert to a regular uint32 representation first: `jax.random.key_data(arr).item()` or `jnp.asarray(key, dtype=jnp.uint32)`
  2. Use `jax.random.bits(key)` for raw bits
  3. Avoid .item() on key arrays; print the array itself for debugging

Example fix

# before
key = jax.random.key(0)
key.item()  # TypeError

# after
raw = jax.random.key_data(key)  # uint32 array
raw.item()
Defensive patterns

Strategy: validation

Validate before calling

from jax import dtypes

def safe_item(arr):
    if dtypes.issubdtype(arr.dtype, dtypes.extended):
        raise ValueError('cannot .item() an extended-dtype array')
    return arr.item()

Type guard

def has_python_scalar_dtype(arr) -> bool:
    from jax import dtypes
    return not dtypes.issubdtype(arr.dtype, dtypes.extended)

Prevention

When it happens

Trigger: Calling `arr.item()` where `arr.dtype` is an extended dtype, most commonly `jax.random.key(...)` arrays with dtype `key<fry>`.

Common situations: Calling `.item()` on a jax.random.PRNGKey/key array to inspect the raw key; debugging code that dumps array contents via item().

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/debaabe909cfc6f0. Report an issue: GitHub.