jax-ml/jax · error · TypeError
len() of unsized object
Error message
len() of unsized object
What it means
len() of an EArray with ndim==0 has no first-axis length, matching NumPy's 'len() of unsized object' TypeError for 0-d arrays. EArray.__len__ returns shape[0], which does not exist for scalars.
Source
Thrown at jax/_src/earray.py:70
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)
is_fully_replicated = property(lambda self: self._data.is_fully_replicated)
delete = property(lambda self: self._data.delete) # pyrefly: ignore[bad-override]
is_deleted = property(lambda self: self._data.is_deleted) # pyrefly: ignore[bad-override]
on_device_size_in_bytes = property(lambda self: self._data.on_device_size_in_bytes) # pyrefly: ignore[bad-override]
unsafe_buffer_pointer = property(lambda self: self._data.unsafe_buffer_pointer) # pyrefly: ignore[bad-override]
# defer to extended dtype rules
@property
def sharding(self):
phys_sharding = self._data.sharding
return sharding_impls.logical_sharding(self.shape, self.dtype, phys_sharding)
View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Guard with ndim: n = earr.shape[0] if earr.ndim > 0 else 1
- Use size for element count: earr.size works for 0-d (returns 1)
- Use np.ndim(earr)==0 checks in generic helpers before calling len
Example fix
# before n = len(earr) # after n = earr.shape[0] if earr.ndim > 0 else 1
Defensive patterns
Strategy: validation
Validate before calling
n = earr.shape[0] if getattr(earr, 'ndim', 0) > 0 else 1
Type guard
def has_len(e) -> bool:
return getattr(e, 'ndim', None) not in (0, None) Prevention
- Use .size instead of len() for element counts
- In generic helpers, branch on np.ndim(x) == 0 before calling len
When it happens
Trigger: len(earr) where the EArray's aval shape is (); often inside generic container code (dataclasses, serializers) that calls len() on every value.
Common situations: Serialization or logging utilities that call len(x) to size containers; mixed scalar/array pipelines where a value is sometimes 0-d; etuple-driven symbolic expressions.
Related errors
- len() of unsized object
- iteration over a 0-d array
- len() of unsized object
- numpy masked arrays are not supported as direct inputs to JA
- Python int {value} too large to convert to int64
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/8cb9a8cab183a382.
Report an issue: GitHub.