jax-ml/jax · error · ValueError

jnp.insert(): obj must be a slice, a one-dimensional array,

Error message

jnp.insert(): obj must be a slice, a one-dimensional array, or a scalar; got {obj}

What it means

Raised by jnp.insert when the obj argument (the insertion positions), after conversion to an array, has more than one dimension. JAX only supports a scalar, 1-D index array, or slice as positions.

Source

Thrown at jax/_src/numpy/lax_numpy.py:7795

    ...                     [12, 13]])
    >>> jnp.insert(x, indices, values, axis=1)
    Array([[ 1, 10,  2,  3, 11],
           [ 4, 12,  5,  6, 13]], dtype=int32)
  """
  a, _, values_arr = util.ensure_arraylike("insert", arr, 0 if isinstance(obj, slice) else obj, values)

  if axis is None:
    a = ravel(a)
    axis = 0
  axis = core.concrete_or_error(None, axis, "axis argument of jnp.insert()")
  axis = _canonicalize_axis(axis, a.ndim)
  if isinstance(obj, slice):
    indices = arange(*obj.indices(a.shape[axis]))
  else:
    indices = asarray(obj)

  if indices.ndim > 1:
    raise ValueError("jnp.insert(): obj must be a slice, a one-dimensional "
                     f"array, or a scalar; got {obj}")
  if not np.issubdtype(indices.dtype, np.integer):
    if indices.size == 0 and not isinstance(obj, Array):
      indices = indices.astype(int)
    else:
      # Note: np.insert allows boolean inputs but the behavior is deprecated.
      raise ValueError("jnp.insert(): index array must be "
                       f"integer typed; got {obj}")
  values_arr = array(values_arr, ndmin=a.ndim, dtype=a.dtype, copy=False)

  if indices.size == 1:
    index = ravel(indices)[0]
    if indices.ndim == 0:
      values_arr = moveaxis(values_arr, 0, axis)
    indices = array_creation.full(values_arr.shape[axis], index)
  n_input = a.shape[axis]
  n_insert = broadcast_shapes(indices.shape, (values_arr.shape[axis],))[0]
  out_shape = list(a.shape)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Flatten obj before calling: obj.ravel() or obj.reshape(-1)
  2. Rework logic to insert sequentially per row instead of one 2-D call
  3. Pass a slice or scalar if only one insertion point is needed

Example fix

// before
jnp.insert(a, positions_2d, values, axis=0)
// after
jnp.insert(a, positions_2d.ravel(), values, axis=0)
Defensive patterns

Strategy: validation

Validate before calling

obj = jnp.asarray(obj)
if obj.ndim > 1: obj = obj.ravel()

Type guard

def is_valid_insert_obj(obj):
    return jnp.asarray(obj).ndim <= 1 or isinstance(obj, slice)

Prevention

When it happens

Trigger: jnp.insert(a, obj_2d, values) where obj is e.g. shape (2,2); broadcasting produced a multi-dimensional positions array; passing a nested list of indices.

Common situations: Reusing a gridded/meshed index array (from meshgrid or indices) as insertion points; passing a matrix of positions where a flat list was intended.

Related errors


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