{"record":{"id":"bb2e74fe6357c515","repo":"jax-ml/jax","slug":"first-argument-to-bcoo-extract-should-be-a-bcoo-ar","errorCode":null,"errorMessage":"First argument to bcoo_extract should be a BCOO array. Got {type(sparr)=}","messagePattern":"First argument to bcoo_extract should be a BCOO array\\. Got (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/bcoo.py","lineNumber":386,"sourceCode":"bcoo_extract_p = core.Primitive('bcoo_extract')\n\n\ndef bcoo_extract(sparr: BCOO, arr: ArrayLike, *, assume_unique: bool | None = None) -> BCOO:\n  \"\"\"Extract values from a dense array according to the sparse array's indices.\n\n  Args:\n    sparr : BCOO array whose indices will be used for the output.\n    arr : ArrayLike with shape equal to self.shape\n    assume_unique : bool, defaults to sparr.unique_indices\n      If True, extract values for every index, even if index contains duplicates.\n      If False, duplicate indices will have their values summed and returned in\n      the position of the first index.\n\n  Returns:\n    extracted : a BCOO array with the same sparsity pattern as self.\n  \"\"\"\n  if not isinstance(sparr, BCOO):\n    raise TypeError(f\"First argument to bcoo_extract should be a BCOO array. Got {type(sparr)=}\")\n  a = jnp.asarray(arr)\n  if a.shape != sparr.shape:\n    raise ValueError(f\"shape mismatch: {sparr.shape=} {a.shape=}\")\n  if assume_unique is None:\n    assume_unique = sparr.unique_indices\n  data = _bcoo_extract(sparr.indices, a, assume_unique=assume_unique)\n  return BCOO((data, sparr.indices), **sparr._info._asdict())\n\n\ndef _bcoo_extract(indices: Array, arr: Array, *, assume_unique=True) -> Array:\n  \"\"\"Extract BCOO data values from a dense array at given BCOO indices.\n\n  Args:\n    indices: An ndarray; see BCOO indices.\n    arr: A dense array.\n    assume_unique: bool, default=True\n      If True, then indices will be assumed unique and a value will be extracted\n      from arr for each index. Otherwise, extra work will be done to de-duplicate","sourceCodeStart":368,"sourceCodeEnd":404,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/bcoo.py#L368-L404","documentation":"jax.experimental.sparse.bcoo_extract requires its first argument to be a BCOO instance; it extracts the dense array's values at the BCOO's stored indices. Passing anything else (dense array, CSR, BCSR) raises TypeError.","triggerScenarios":"Calling bcoo_extract(csr_mat, dense_arr), bcoo_extract(jnp.array(...), ...), or feeding a BCSR where a BCOO is required.","commonSituations":"Assuming bcoo_extract works for any sparse format; passing a raw indices array; format confusion when porting between CSR and BCOO code paths.","solutions":["Pass a BCOO: convert with sparr.tobcoo() if needed","For raw indices, use the internal _bcoo_extract(indices, arr) instead of the public wrapper","For BCSR use bcsr_extract"],"exampleFix":"// before\nvals = bcoo_extract(csr_mat, dense_arr)\n// after\nvals = bcoo_extract(csr_mat.tobcoo(), dense_arr)","handlingStrategy":"type-guard","validationCode":"from jax.experimental.sparse import BCOO\nif not isinstance(sparr, BCOO):\n    sparr = sparr.tobcoo() if hasattr(sparr, 'tobcoo') else BCOO.fromdense(jnp.asarray(sparr))","typeGuard":"def is_bcoo(x) -> bool:\n    from jax.experimental.sparse import BCOO\n    return isinstance(x, BCOO)","tryCatchPattern":"try:\n    bcoo_extract(sparr, arr)\nexcept TypeError:\n    bcoo_extract(sparr.tobcoo(), arr)","preventionTips":["Check isinstance(x, BCOO) before bcoo_extract","Use bcsr_extract for BCSR operands"],"tags":["jax","sparse","type-check","bcoo"],"backgroundTag":"wrong-argument-type","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}