jax-ml/jax · error · TypeError

static_slice: indices must be static scalars or slices. Got

Error message

static_slice: indices must be static scalars or slices. Got index of type {type(pidx.index)} at position {position}

What it means

to_static_slice requires every index to be a static scalar, slice, ellipsis, or None. If any index was classified as ARRAY, BOOLEAN, or DYNAMIC_SLICE (e.g. an integer array, boolean mask, or jax.ds marker), it raises TypeError naming the position and underlying type.

Source

Thrown at jax/_src/numpy/indexing.py:403

        slicing.GatherScatterMode.PROMISE_IN_BOUNDS, slicing.GatherScatterMode.CLIP]:
      raise ValueError("static_slice requires mode='promise_in_bounds' or mode='clip'")

    # Validation of the unmodified user indices.
    if parsed_mode == slicing.GatherScatterMode.PROMISE_IN_BOUNDS:
      self.validate_static_indices(normalize_indices=normalize_indices)
    self.validate_slices()

    # For sharded inputs, indexing (like x[0]) and partial slices (like x[:2] as
    # opposed to x[:]) lead to incorrect sharding semantics when computed via slice.
    # TODO(yashkatariya): fix slice with sharding
    if arr_is_sharded and self.has_partial_slices():
      raise ValueError("static_slice with partial slices does not support nontrivial array sharding.")

    for position, pidx in enumerate(self.indices):
      if pidx.typ in [IndexType.INTEGER, IndexType.ELLIPSIS, IndexType.SLICE, IndexType.NONE]:
        pass
      elif pidx.typ in [IndexType.ARRAY, IndexType.BOOLEAN, IndexType.DYNAMIC_SLICE]:
        raise TypeError("static_slice: indices must be static scalars or slices."
                        f" Got index of type {type(pidx.index)} at position {position}")
      else:
        raise TypeError(f"static_slice: unrecognized index {pidx.index} at position {position}.")

    # Now re-iterate to generate static slices.
    start_indices: list[int] = []
    limit_indices: list[int] = []
    strides: list[int] = []
    rev_axes: list[int] = []
    squeeze_axes: list[int] = []
    newaxis_dims: list[int] = []

    expanded = self.expand_ellipses()
    for pidx in expanded.indices:
      if pidx.typ in [IndexType.ARRAY, IndexType.BOOLEAN, IndexType.ELLIPSIS]:
        raise RuntimeError(f"Internal: unexpected index encountered: {pidx}")
      elif pidx.typ == IndexType.NONE:
        # Expanded axes indices are based on the rank of the array after slicing

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Use plain int/slice indices when the static-slice path is requested
  2. Let the default gather path handle array/boolean indices (don't force mode='slice')
  3. Split the expression: slice statically, then advanced-index the result

Example fix

// before
y = x[np.array([0, 2])]  # routed to static_slice
// after
y = x[np.array([0, 2])]  # via default gather: drop mode='slice' configuration
# or
y = x[0:3:2]
Defensive patterns

Strategy: validation

Validate before calling

assert all(i is None or i is Ellipsis or isinstance(i, (int, np.integer, slice)) for i in idx_tuple)

Type guard

def is_static_index(idx) -> bool:
    return isinstance(idx, (int, np.integer, slice, type(None), type(Ellipsis)))

Prevention

When it happens

Trigger: Calling the static-slice path with advanced indices, e.g. x[np.array([0,2])] or x[mask] routed into to_static_slice (typically via internal dispatch with mode='slice' or unique_indices settings).

Common situations: Downstream libraries or user code forcing the static-slice lowering while still using advanced indexing; mixing gather-style indices with slice lowering config.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/763a28d80a994966. Report an issue: GitHub.