{"record":{"id":"95a9e7baf6d4a693","repo":"jax-ml/jax","slug":"len-of-unsized-object-95a9e7","errorCode":null,"errorMessage":"len() of unsized object","messagePattern":"len\\(\\) of unsized object","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/core.py","lineNumber":2584,"sourceCode":"  def to_tangent_aval(self):\n    return ShapedArray._create(\n        self.shape, primal_dtype_to_tangent_dtype(self.dtype),\n        self.weak_type, self.sharding, self.mat, self.memory_space,\n        self.layout)\n\n  def to_ct_aval(self):\n    dtype = primal_dtype_to_tangent_dtype(self.dtype)\n    sharding = primal_sharding_to_cotangent_sharding(self.sharding)\n    ct_mat = self.mat.to_ct_mat()\n    return ShapedArray._create(\n        self.shape, dtype, self.weak_type, sharding, ct_mat, self.memory_space,\n        self.layout)\n\n  def _len(self, ignored_tracer):\n    try:\n      return self.shape[0]\n    except IndexError as err:\n      raise TypeError(\"len() of unsized object\") from err  # same as numpy error\n\n  def update_manual_axis_type(self, mat):\n    mat = get_mat(mat, self.sharding.mesh)\n    if mat is self.manual_axis_type:\n      return self\n    return ShapedArray._create(self.shape, self.dtype, self.weak_type,\n                               self.sharding, mat, self.memory_space,\n                               self.layout)\n\n  def update_weak_type(self, weak_type):\n    if weak_type == self.weak_type:\n      return self\n    return ShapedArray._create(self.shape, self.dtype, weak_type, self.sharding,\n                               self.manual_axis_type, self.memory_space,\n                               self.layout)\n\n  def strip_weak_type(self) -> AbstractValue:\n    if not self.weak_type:","sourceCodeStart":2566,"sourceCodeEnd":2602,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/core.py#L2566-L2602","documentation":"ShapedArray._len mimics NumPy: calling len() on a 0-d (scalar) array-like aval has no first dimension, so shape[0] raises IndexError which is re-raised as TypeError('len() of unsized object').","triggerScenarios":"len(x) where x is a 0-dimensional tracer/aval, e.g. inside jit on a scalar from jax.random.randint((), ...) or a scalar loss value.","commonSituations":"Generic code doing len(batch) that also receives scalars; data pipelines where a tensor unexpectedly collapses to 0-d after squeeze/reduction.","solutions":["Check x.ndim > 0 (or x.shape) before calling len","Use x.shape[0] guarded, or restructure so scalars take a different branch","Audit squeeze()/reduction calls producing 0-d results"],"exampleFix":"// before\ndef f(x):\n    n = len(x)  # fails when x is scalar\n\n// after\ndef f(x):\n    n = x.shape[0] if x.ndim else 1","handlingStrategy":"type-guard","validationCode":"if arr.ndim == 0:\n    raise TypeError('scalar has no len')","typeGuard":"def has_len(x): return getattr(getattr(x, 'ndim', None), '__gt__', lambda _: False)(0)","tryCatchPattern":"try:\n    n = len(x)\nexcept TypeError:\n    n = 1  # scalar","preventionTips":["Prefer x.shape over len(x) in library code","Guard pipelines where squeeze/reduce can produce 0-d arrays"],"tags":["jax","numpy","len","zero-d-array"],"backgroundTag":"len-of-scalar-array","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}