jax-ml/jax · error · TypeError

Indexer must have integer or boolean type, got indexer with

Error message

Indexer must have integer or boolean type, got indexer with type {np.dtype(type(idx))}

What it means

IndexType.from_index raises TypeError when the index is a bare Python float, complex, or numpy scalar (np.generic) of non-integer type. JAX refuses float scalar indexing just like modern NumPy. The message shows the dtype inferred from the Python type (e.g. float64).

Source

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

        raise TypeError(
          f"Indexer must have integer or boolean type, got indexer with type {idx.dtype}")
    elif isinstance(idx, str):
      # TODO(jakevdp): this TypeError is for backward compatibility.
      # We should switch to IndexError for consistency.
      raise TypeError(f"JAX does not support string indexing; got {idx=}")
    elif isinstance(idx, Sequence):
      if not idx:  # empty indices default to float, so special-case this.
        return cls.ARRAY
      idx_aval = api.eval_shape(array_constructors.asarray, idx)
      if idx_aval.dtype == bool:
        return cls.BOOLEAN
      elif dtypes.issubdtype(idx_aval.dtype, np.integer):
        return cls.ARRAY
      else:
        raise TypeError(
          f"Indexer must have integer or boolean type, got indexer with type {idx_aval.dtype}")
    elif isinstance(idx, (float, complex, np.generic)):
      raise TypeError(
        f"Indexer must have integer or boolean type, got indexer with type {np.dtype(type(idx))}")
    else:
      raise IndexError("only integers, slices (`:`), ellipsis (`...`), newaxis (`None`)"
                       f" and integer or boolean arrays are valid indices. Got {idx}")


class ParsedIndex(NamedTuple):
  """Structure for tracking an indexer parsed within the context of an array shape."""
  index: Index
  typ: IndexType
  consumed_axes: tuple[int, ...]


def _parse_indices(
    indices: tuple[Index, ...],
    shape: tuple[int, ...],
) -> list[ParsedIndex]:
  """Parse indices in the context of an array shape.

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Wrap with int(): x[int(pos)]
  2. Fix the source producing floats (e.g. use // instead of / when halving lengths)
  3. Round intentionally if the float came from a real-valued computation: x[int(round(pos))]

Example fix

// before
mid = len(xs) / 2
y = x[mid]
// after
mid = len(xs) // 2
y = x[mid]
Defensive patterns

Strategy: type-guard

Validate before calling

idx = int(idx) if isinstance(idx, float) else idx

Type guard

import operator
def is_indexable_scalar(v) -> bool:
    return isinstance(v, (int, np.integer)) or (hasattr(v, '__index__') and not isinstance(v, float))

Prevention

When it happens

Trigger: x[1.0], x[2.5], x[np.float64(3)] — scalar float/complex indices directly inside x[...].

Common situations: Values returned from len()/dict lookups/computed positions stored as floats (common when reading JSON config values or dividing counts), then used as indices.

Related errors


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