{"record":{"id":"32d27ec5c1f7d4ce","repo":"jax-ml/jax","slug":"array-slice-indices-must-have-static-start-stop-st","errorCode":null,"errorMessage":"Array slice indices must have static start/stop/step to be used with NumPy indexing syntax. Got {idx.index} at position {position}. To index an array at a dynamic position with a static slice size, use x[jax.ds(start, size)] or lax.dynamic_slice/dynamic_update_slice instead (JAX does not support dynamically sized arrays within traced functions).","messagePattern":"Array slice indices must have static start/stop/step to be used with NumPy indexing syntax\\. Got (.+?) at position (.+?)\\. To index an array at a dynamic position with a static slice size, use x\\[jax\\.ds\\(start, size\\)\\] or lax\\.dynamic_slice/dynamic_update_slice instead \\(JAX does not support dynamically sized arrays within traced functions\\)\\.","errorType":"validation","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/indexing.py","lineNumber":237,"sourceCode":"\n    Raises an IndexError in case of non-static entries.\n    \"\"\"\n    for position, idx in enumerate(self.indices):\n      if idx.typ == IndexType.SLICE:\n        assert isinstance(idx.index, slice)\n        elts = [idx.index.start, idx.index.stop, idx.index.step]\n        if not all(_is_slice_element_none_or_constant_or_symbolic(val)\n                   for val in elts):\n          msg = (\"Array slice indices must have static start/stop/step to be used \"\n                 f\"with NumPy indexing syntax. Got {idx.index} at position \"\n                 f\"{position}. To index an array at a dynamic position with a \"\n                 \"static slice size, use x[jax.ds(start, size)] or \"\n                 \"lax.dynamic_slice/dynamic_update_slice instead (JAX does not \"\n                 \"support dynamically sized arrays within traced functions).\")\n          tracer = next((val for val in elts if isinstance(val, core.Tracer)), None)\n          if tracer is not None:\n            msg += tracer._origin_msg()\n          raise IndexError(msg)\n\n  @staticmethod\n  def is_sharded(arr) -> bool:\n    \"\"\"Check whether the array is sharded.\"\"\"\n    return isinstance(arr, array.ArrayImpl) and not arr.sharding.num_devices == 1\n\n  def has_partial_slices(self) -> bool:\n    \"\"\"Check whether the indexer contains partial slices.\n\n    For sharded arrays, partial slices cannot automatically propagate\n    sharding.\n    \"\"\"\n    for idx in self.indices:\n      if idx.typ in [IndexType.INTEGER, IndexType.DYNAMIC_SLICE]:\n        return True\n      if idx.typ == IndexType.SLICE:\n        slc = idx.index\n        assert isinstance(slc, slice)","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/indexing.py#L219-L255","documentation":"validate_slices rejects slice objects whose start/stop/step are non-static (contain Tracers) when NumPy slice syntax is used under tracing (jit/vmap/grad). JAX needs static slice bounds because array shapes must be static inside traces; the error points to jax.ds(start, size) / lax.dynamic_slice as the alternative.","triggerScenarios":"@jit-def f(x): return x[i:i+5] where i is a traced value (argument or computed from one); any slice built from Tracer values inside jit/vmap/scan.","commonSituations":"Sliding windows at runtime offsets, batching with dynamic start positions, porting NumPy windowing code under jit. Very common when enabling jit on existing code.","solutions":["Use jax.lax.dynamic_slice(x, (start,), (size,)) with a fixed size","Use the jax.ds(start, size) dynamic-slice index marker in x[...]","Keep slice bounds static: hoist i out of jit or close over Python ints"],"exampleFix":"# before\n@jax.jit\ndef f(x, i):\n    return x[i:i+5]\n# after\n@jax.jit\ndef f(x, i):\n    return jax.lax.dynamic_slice(x, (i,), (5,))","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Inside jit, use lax.dynamic_slice or jax.ds(start, size) for runtime offsets","Keep slice bounds as closed-over Python ints, not traced values","Design traced functions so output shapes are static"],"tags":["jax","jit","tracing","dynamic-slice"],"backgroundTag":"dynamic-slice-in-traced-function","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}