jax-ml/jax · error · TypeError
len() of unsized object
Error message
len() of unsized object
What it means
RefRValue._len converts an IndexError from shape[0] into the numpy-style TypeError 'len() of unsized object', mirroring NumPy's behavior for 0-d arrays. It means the ref has a shape but it's empty (scalar-like).
Source
Thrown at jax/_src/state/types.py:523
return self.update(inner_aval=self.inner_aval.update_weak_type(weak_type))
def update_manual_axis_type(self, mat):
return self.update(inner_aval=self.inner_aval.update_manual_axis_type(mat))
def update(self, inner_aval=None, memory_space=None, kind=None): # pyrefly: ignore[bad-override]
inner_aval = self.inner_aval if inner_aval is None else inner_aval
memory_space = self.memory_space if memory_space is None else memory_space
kind = self.kind if kind is None else kind
return AbstractRef(inner_aval, memory_space, kind)
ndim = property(lambda self: len(self.shape))
size = property(lambda self: math.prod(self.shape))
def _len(self, ignored_tracer) -> int:
try:
return self.shape[0]
except IndexError as err:
raise TypeError("len() of unsized object") from err # same as numpy error
@property
def shape(self):
try:
return self.inner_aval.shape # pyrefly: ignore[missing-attribute]
except AttributeError:
raise AttributeError(
f"{self!r} has no `shape`."
) from None
@property
def dtype(self):
try:
return self.inner_aval.dtype # pyrefly: ignore[missing-attribute]
except AttributeError:
raise AttributeError(
f"{self!r} has no `dtype`."
) from NoneView on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Check ref.ndim > 0 before len()
- Use ref.size to detect emptiness instead
- Reshape the underlying buffer to at least 1D
Example fix
# before n = len(ref) # after n = ref.shape[0] if ref.ndim > 0 else 1
Defensive patterns
Strategy: validation
Validate before calling
if ref.ndim == 0:
raise TypeError("ref is unsized") Type guard
def is_sized(ref):
return getattr(ref, "ndim", 0) > 0 Prevention
- Check ndim before len()
- Treat refs as possibly scalar in generic code
When it happens
Trigger: Calling len(ref) on a ref whose shape is () (0-dimensional / unsized).
Common situations: Generic code doing len(x) on containers that may hold scalars; refs created from scalar buffers.
Related errors
- len() of unsized object
- len() of unsized object
- numpy masked arrays are not supported as direct inputs to JA
- Python int {value} too large to convert to int64
- Python int {value} too large to convert to int32
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/cff9dc86af85e49e.
Report an issue: GitHub.