jax-ml/jax · error · ValueError

static_slice requires mode='promise_in_bounds' or mode='clip

Error message

static_slice requires mode='promise_in_bounds' or mode='clip'

What it means

to_static_slice only supports the modes PROMISE_IN_BOUNDS and CLIP; other GatherScatterMode values (e.g. DROP or FILL with NaN/oob) cannot be expressed as a plain static slice, so a ValueError is raised.

Source

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

  def to_static_slice(
      self, *,
      arr_is_sharded: bool = False,
      normalize_indices: bool = True,
      mode: str | slicing.GatherScatterMode | None) -> _StaticSliceIndexer:
    """Convert to StaticSliceIndexer data structure.

    If this is not possible, raise a ValueError, TypeError, or IndexError.
    """
    if mode is None:
      parsed_mode = slicing.GatherScatterMode.PROMISE_IN_BOUNDS
    else:
      parsed_mode = slicing.GatherScatterMode.from_any(mode)
    if any(core.is_symbolic_dim(s) for s in self.shape):
      raise ValueError("mode='slice' is not valid for polymorphic shapes.")

    if parsed_mode not in [
        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}")

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Drop the mode argument (default promise_in_bounds) when slicing with static indices
  2. Use mode='clip' if out-of-range indices must be tolerated
  3. Switch to jnp.take(x, idx, mode=...) which supports fill/drop semantics

Example fix

// before
y = x[slice_indices]  # dispatch configured with mode='fill'
// after
y = jnp.take(x, slice_indices, mode='fill')
Defensive patterns

Strategy: validation

Validate before calling

assert mode in (None, 'promise_in_bounds', 'clip')

Prevention

When it happens

Trigger: Calling the internal static-slice path (rewriting_take/to_static_slice) with mode='drop' or mode=('fill', value), e.g. via x[...] dispatch configured with unique_indices/indices_are_sorted/mode kwargs.

Common situations: Reusing a mode argument intended for jax.lax.gather/jnp.take (where 'fill'/'drop' are valid) in a context that lowers to a static slice.

Related errors


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