{"record":{"id":"f77e0a6a6c760dc2","repo":"jax-ml/jax","slug":"bcoo-slice-invalid-indices-got-start-indices","errorCode":null,"errorMessage":"bcoo_slice: invalid indices. Got {start_indices=}, {limit_indices=} and shape={mat.shape}","messagePattern":"bcoo_slice: invalid indices\\. Got (.+?), (.+?) and shape=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/bcoo.py","lineNumber":1992,"sourceCode":"  \"\"\"\n  if not isinstance(mat, BCOO):\n    raise TypeError(f\"bcoo_slice: input should be BCOO array, got type(mat)={type(mat)}\")\n  start_indices = [operator.index(i) for i in start_indices]\n  limit_indices = [operator.index(i) for i in limit_indices]\n  if strides is not None:\n    strides = [operator.index(i) for i in strides]\n  else:\n    strides = [1] * mat.ndim\n  if len(start_indices) != len(limit_indices) != len(strides) != mat.ndim:\n    raise ValueError(f\"bcoo_slice: indices must have size mat.ndim={mat.ndim}\")\n  if len(strides) != mat.ndim:\n    raise ValueError(f\"len(strides) = {len(strides)}; expected {mat.ndim}\")\n  if any(s <= 0 for s in strides):\n    raise ValueError(f\"strides must be a sequence of positive integers; got {strides}\")\n\n  if not all(0 <= start <= end <= size\n             for start, end, size in safe_zip(start_indices, limit_indices, mat.shape)):\n    raise ValueError(f\"bcoo_slice: invalid indices. Got {start_indices=}, \"\n                     f\"{limit_indices=} and shape={mat.shape}\")\n\n  start_batch, start_sparse, start_dense = split_list(start_indices, [mat.n_batch, mat.n_sparse])\n  end_batch, end_sparse, end_dense = split_list(limit_indices, [mat.n_batch, mat.n_sparse])\n  stride_batch, stride_sparse, stride_dense = split_list(strides, [mat.n_batch, mat.n_sparse])\n\n  data_slices = []\n  index_slices = []\n  for i, (start, end, stride) in enumerate(zip(start_batch, end_batch, stride_batch)):\n    data_slices.append(slice(None) if mat.data.shape[i] != mat.shape[i] else slice(start, end, stride))\n    index_slices.append(slice(None) if mat.indices.shape[i] != mat.shape[i] else slice(start, end, stride))\n  data_slices.append(slice(None))\n  index_slices.extend([slice(None), slice(None)])\n  for i, (start, end, stride) in enumerate(zip(start_dense, end_dense, stride_dense)):\n    data_slices.append(slice(start, end, stride))\n  new_data = mat.data[tuple(data_slices)]\n  new_indices = mat.indices[tuple(index_slices)]\n  new_shape = tuple(","sourceCodeStart":1974,"sourceCodeEnd":2010,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/bcoo.py#L1974-L2010","documentation":"bcoo_slice requires, for every dimension, 0 <= start <= limit <= shape[dim]. Violating any of these (negative start, limit beyond the axis size, or limit < start) raises this ValueError echoing the offending indices and mat.shape.","triggerScenarios":"Calling bcoo_slice with start_indices containing negatives, limit_indices exceeding mat.shape, or limits smaller than starts, e.g. start=(5,), limit=(3,) on an axis of size 4.","commonSituations":"Using -1 as an end index (NumPy convention) instead of the axis size; computing limits from dynamic values without clamping; off-by-one errors after shape changes.","solutions":["Clamp indices: start = max(0, start); limit = min(limit, dim_size)","Replace NumPy-style negative ends with explicit axis lengths (use mat.shape, not -1)","Assert limits >= starts per axis before calling"],"exampleFix":"# before\nbcoo_slice(mat, start_indices=(0, -1), limit_indices=(4, 10), strides=None)  # shape (4, 10) ok but -1 invalid\n# after\nbcoo_slice(mat, start_indices=(0, 0), limit_indices=(4, 10))","handlingStrategy":"validation","validationCode":"starts = [max(0, s) for s in start_indices]\nlimits = [min(l, d) for l, d in zip(limit_indices, mat.shape)]\nassert all(s <= l for s, l in zip(starts, limits))","typeGuard":null,"tryCatchPattern":"try:\n    out = bcoo_slice(mat, starts, limits, strides)\nexcept ValueError as e:\n    if 'invalid indices' in str(e):\n        limits = [min(l, d) for l, d in zip(limit_indices, mat.shape)]\n        out = bcoo_slice(mat, starts, limits, strides)\n    else:\n        raise","preventionTips":["Never use NumPy-style -1 endpoints; use mat.shape[i]","Clamp dynamic limits against the current shape before slicing"],"tags":["jax","sparse","bcoo","index-out-of-bounds","slice"],"backgroundTag":"slice-index-out-of-bounds","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}