{"record":{"id":"e785c8bea12cfd1a","repo":"jax-ml/jax","slug":"bcoo-slice-input-should-be-bcoo-array-got-type-m","errorCode":null,"errorMessage":"bcoo_slice: input should be BCOO array, got type(mat)={type(mat)}","messagePattern":"bcoo_slice: input should be BCOO array, got type\\(mat\\)=(.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/bcoo.py","lineNumber":1976,"sourceCode":"\ndef bcoo_slice(mat: BCOO, *, start_indices: Sequence[int], limit_indices: Sequence[int],\n               strides: Sequence[int] | None = None) -> BCOO:\n  \"\"\"Sparse implementation of :func:`jax.lax.slice`.\n\n  Args:\n    mat: BCOO array to be reshaped.\n    start_indices: sequence of integers of length `mat.ndim` specifying the starting\n      indices of each slice.\n    limit_indices: sequence of integers of length `mat.ndim` specifying the ending\n      indices of each slice\n    strides: (not implemented) sequence of integers of length `mat.ndim` specifying\n      the stride for each slice\n\n  Returns:\n    out: BCOO array containing the slice.\n  \"\"\"\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","sourceCodeStart":1958,"sourceCodeEnd":1994,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/bcoo.py#L1958-L1994","documentation":"bcoo_slice requires the input to be a jax.experimental.sparse.BCOO array; any other type (dense jnp.ndarray, JAX Sparse (COO/CSR/CSC), numpy array) is rejected with a TypeError before any slicing logic runs. This mirrors lax.slice semantics but for the sparse BCOO representation. Convert your array to BCOO first.","triggerScenarios":"Calling jax.experimental.sparse.bcoo_slice(mat, start_indices, limit_indices) (or sparse.BCOO-safe code paths) with mat being a dense jnp.ndarray, np.ndarray, or a jax_sparse COO/CSR object instead of sparse.BCOO.","commonSituations":"Mixing jax_sparse (older external library) with jax.experimental.sparse; passing a dense array returned by a previous dense computation; refactoring code from jnp slicing to sparse slicing without converting the operand.","solutions":["Convert the operand: mat = jax.experimental.sparse.BCOO.fromdense(mat)","If coming from another sparse format, convert via BCOO: sparse.BCOO.from_scipy_sparse(m) or reconstruct with BCOO((data, indices), shape=...)",",If you intended dense slicing, use lax.slice / mat[start:stop] directly instead of bcoo_slice"],"exampleFix":"// before\nout = sparse.bcoo_slice(dense_mat, (0,), (4,))\n// after\nout = sparse.bcoo_slice(sparse.BCOO.fromdense(dense_mat), (0,), (4,))","handlingStrategy":"type-guard","validationCode":"from jax.experimental import sparse\nif not isinstance(mat, sparse.BCOO):\n    mat = sparse.BCOO.fromdense(mat) if hasattr(mat, 'ndim') else mat","typeGuard":"import jax.experimental.sparse as sparse\nfrom jax.experimental.sparse import BCOO\ndef is_bcoo(x) -> bool:\n    return isinstance(x, BCOO)","tryCatchPattern":null,"preventionTips":["Standardize on BCOO as the sparse type at module boundaries","Convert scipy/jax_sparse formats to BCOO immediately on ingestion"],"tags":["jax","sparse","bcoo","type-error","slice"],"backgroundTag":"wrong-argument-type","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}