{"record":{"id":"e47458424bc58a2d","repo":"jax-ml/jax","slug":"cannot-do-a-non-empty-jnp-take-from-an-empty-axi","errorCode":null,"errorMessage":"Cannot do a non-empty jnp.take() from an empty axis.","messagePattern":"Cannot do a non-empty jnp\\.take\\(\\) from an empty axis\\.","errorType":"validation","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/indexing.py","lineNumber":732,"sourceCode":"    gather_mode = slicing.GatherScatterMode.FILL_OR_DROP\n    # lax.gather() does not support negative indices, so we wrap them here\n    indices = util._where(indices < 0, indices + a.shape[axis_idx], indices)\n  elif mode == \"raise\":\n    # TODO(phawkins): we have no way to report out of bounds errors yet.\n    raise NotImplementedError(\"The 'raise' mode to jnp.take is not supported.\")\n  elif mode == \"wrap\":\n    indices = ufuncs.mod(indices, lax._const(indices, a.shape[axis_idx]))\n    gather_mode = slicing.GatherScatterMode.PROMISE_IN_BOUNDS\n  elif mode == \"clip\":\n    gather_mode = slicing.GatherScatterMode.CLIP\n  else:\n    raise ValueError(f\"Invalid mode '{mode}' for np.take\")\n\n  index_dims = len(np.shape(indices))\n  slice_sizes = list(np.shape(a))\n  if slice_sizes[axis_idx] == 0:\n    if indices.size != 0:\n      raise IndexError(\"Cannot do a non-empty jnp.take() from an empty axis.\")\n    return a\n\n  if indices.size == 0:\n    out_shape = (slice_sizes[:axis_idx] + list(indices.shape) +\n                 slice_sizes[axis_idx + 1:])\n    return lax.full_like(a, 0, shape=out_shape)\n\n  slice_sizes[axis_idx] = 1\n  dnums = slicing.GatherDimensionNumbers(\n    offset_dims=tuple(\n      list(range(axis_idx)) +\n      list(range(axis_idx + index_dims, len(a.shape) + index_dims - 1))),\n    collapsed_slice_dims=(axis_idx,),\n    start_index_map=(axis_idx,))\n  return slicing.gather(a, indices[..., None], dimension_numbers=dnums,\n                        slice_sizes=tuple(slice_sizes),\n                        mode=gather_mode, unique_indices=unique_indices,\n                        indices_are_sorted=indices_are_sorted, fill_value=fill_value)","sourceCodeStart":714,"sourceCodeEnd":750,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/indexing.py#L714-L750","documentation":"You cannot gather elements from an axis of size 0 with a non-empty index array — there is nothing to take. JAX raises IndexError mirroring NumPy's empty-axis take failure.","triggerScenarios":"jnp.take(a, indices, axis=k) where a.shape[k] == 0 and indices.size > 0 (e.g. taking from an empty list/array).","commonSituations":"Empty datasets, empty vocabulary lookups, filtered collections that became empty; take over an axis that a previous operation collapsed to zero length.","solutions":["Guard: if a.shape[axis] == 0 or indices.size == 0, skip or return an empty result","Fix upstream emptiness (ensure the source array is non-empty before taking)","Use mode='fill' semantics won't help — fix the empty-axis input"],"exampleFix":"// before\ny = jnp.take(a, idx)  # a may be empty\n// after\ny = jnp.take(a, idx) if a.size else jnp.array([], dtype=a.dtype)","handlingStrategy":"validation","validationCode":"if a.shape[axis] == 0:\n    assert indices.size == 0, 'cannot take from empty axis with non-empty indices'","typeGuard":null,"tryCatchPattern":"try:\n    y = jnp.take(a, idx, axis=axis)\nexcept IndexError:\n    y = jnp.empty((0,), dtype=a.dtype)  # empty fallback","preventionTips":["Check both a.shape[axis] and indices.size before take","Handle empty dataset/batch branches explicitly"],"tags":["jax","take","empty-array","out-of-bounds"],"backgroundTag":"index-out-of-bounds","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}