{"record":{"id":"178b0076cf808837","repo":"jax-ml/jax","slug":"too-many-indices-array-is-len-shape-dimensiona","errorCode":null,"errorMessage":"Too many indices: array is {len(shape)}-dimensional, but {total_consumed} were indexed","messagePattern":"Too many indices: array is (.+?)-dimensional, but (.+?) were indexed","errorType":"validation","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/indexing.py","lineNumber":164,"sourceCode":"      dimensions_consumed.append(0)\n    elif typ == IndexType.ELLIPSIS:\n      # We don't yet know how many dimensions are consumed, so set to zero\n      # for now and update later.\n      dimensions_consumed.append(0)\n      ellipses_indices.append(i)\n    elif typ == IndexType.BOOLEAN:\n      dimensions_consumed.append(np.ndim(idx))  # pyrefly: ignore[bad-argument-type]\n    elif typ in [IndexType.INTEGER, IndexType.ARRAY, IndexType.SLICE, IndexType.DYNAMIC_SLICE]:\n      dimensions_consumed.append(1)\n    else:\n      raise IndexError(f\"Unrecognized index type: {typ}\")\n\n  # 2. Validate the consumed dimensions and ellipses.\n  if len(ellipses_indices) > 1:\n    raise IndexError(\"an index can only have a single ellipsis ('...')\")\n  total_consumed = sum(dimensions_consumed)\n  if total_consumed > len(shape):\n    raise IndexError(f\"Too many indices: array is {len(shape)}-dimensional,\"\n                     f\" but {total_consumed} were indexed\")\n  if ellipses_indices:\n    dimensions_consumed[ellipses_indices[0]] = len(shape) - total_consumed\n\n  # 3. Generate the final sequence of parsed indices.\n  result: list[ParsedIndex] = []\n  current_dim = 0\n  for index, typ, n_consumed in safe_zip(indices, index_types, dimensions_consumed):\n    consumed_axes = tuple(range(current_dim, current_dim + n_consumed))\n    current_dim += len(consumed_axes)\n    result.append(ParsedIndex(index=index, typ=typ, consumed_axes=consumed_axes))\n  return result\n\n\n@register_pytree_node_class\n@dataclasses.dataclass(frozen=True, kw_only=True, slots=True)\nclass NDIndexer:\n  \"\"\"Object that implements NumPy-style indexing operations on top of JAX.","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/indexing.py#L146-L182","documentation":"The index expression consumes more dimensions than the array has: each integer/slice/array index consumes one axis and each boolean array consumes its ndim axes; the total exceeded len(shape). Matches NumPy's 'too many indices for array' error.","triggerScenarios":"x = jnp.zeros((3,4)); x[0, 0, 0] — three indices on a 2-D array; or a boolean mask of higher rank than the array.","commonSituations":"Code written for a higher-rank array run on squeezed/reshaped data, loops that append index tuples without tracking rank, or accidental unpacking of coordinates tuples.","solutions":["Check x.ndim and the length of your index tuple before indexing","Use an ellipsis to absorb extra axes: x[0, ...]","Audit reshapes/squeezes upstream that reduced rank unexpectedly"],"exampleFix":"// before\ny = x[i, j, k]  # x is 2-D\n// after\ny = x[i, j]  # or x[i, j, ...] when rank varies","handlingStrategy":"validation","validationCode":"assert len(nonnewaxis_idx) <= x.ndim, (x.ndim, nonnewaxis_idx)\n# account for boolean masks consuming multiple axes","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check x.ndim before unpacking coordinate tuples","Use ellipsis at the end of index tuples for rank-agnostic code"],"tags":["jax","indexing","rank","shape-mismatch"],"backgroundTag":"too-many-indices","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}