jax-ml/jax · error · TypeError
iteration over a 0-d array
Error message
iteration over a 0-d array
What it means
EArray (an etuple-backed lazy array used internally by JAX's symbolic/OOO machinery) mimics ndarray semantics; NumPy disallows iteration over 0-d arrays, so EArray.__iter__ raises TypeError for ndim==0 and NotImplementedError otherwise.
Source
Thrown at jax/_src/earray.py:58
@property
def aval(self):
return self._aval
def block_until_ready(self):
_ = self._data.block_until_ready()
return self
def copy_to_host_async(self):
self._data.copy_to_host_async()
def copy(self):
return EArray(self.aval, self._data.copy())
def __repr__(self):
return 'E' + repr(self._data)
def __iter__(self):
if self.ndim == 0: raise TypeError('iteration over a 0-d array')
raise NotImplementedError
# forward to aval
shape = property(lambda self: self.aval.shape)
dtype = property(lambda self: self.aval.dtype)
# computed from shape and dtype
ndim = property(lambda self: len(self.aval.shape))
size = property(lambda self: math.prod(self.aval.shape))
itemsize = property(lambda self: self.aval.dtype.itemsize)
def __len__(self):
if self.ndim == 0: raise TypeError('len() of unsized object')
return self.shape[0]
# forward to self._data
devices = property(lambda self: self._data.devices) # pyrefly: ignore[bad-override]
_committed = property(lambda self: self._data._committed)
is_fully_addressable = property(lambda self: self._data.is_fully_addressable)View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Check ndim/shape before iterating: if earr.ndim > 0: ...
- Index the scalar directly: earr[()] to extract the 0-d value
- Avoid generic iteration over unknown JAX container types; convert to a concrete array first
Example fix
# before vals = list(earr) # TypeError when 0-d # after vals = [earr[()]] if earr.ndim == 0 else list(earr)
Defensive patterns
Strategy: type-guard
Validate before calling
if earr.ndim == 0:
value = earr[()] # extract scalar instead of iterating Type guard
def is_iterable_earray(e) -> bool:
return getattr(e, 'ndim', 0) > 0 Prevention
- Treat JAX containers uniformly: check ndim before iter/list
- Prefer .reshape(-1) or indexing over blanket iteration
When it happens
Trigger: Iterating (for x in earr, list(earr), *earr) over an EArray whose aval shape is (). This class is internal to jax._src.earray and mostly encountered when embedding etuples in control-flow/metaprogramming code.
Common situations: Internal JAX development or libraries built on jax.experimental serialization / etuples hitting an EArray where a scalar was expected; debugging repr-based code that iterates values.
Related errors
- len() of unsized object
- iteration over a 0-d array
- len() of unsized object
- iteration over a 0-d key array
- numpy masked arrays are not supported as direct inputs to JA
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/93b6c7a4ea933b64.
Report an issue: GitHub.