{"record":{"id":"3206e89d4db38768","repo":"jax-ml/jax","slug":"jnp-insert-obj-must-be-a-slice-a-one-dimension","errorCode":null,"errorMessage":"jnp.insert(): obj must be a slice, a one-dimensional array, or a scalar; got {obj}","messagePattern":"jnp\\.insert\\(\\): obj must be a slice, a one-dimensional array, or a scalar; got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":7795,"sourceCode":"    ...                     [12, 13]])\n    >>> jnp.insert(x, indices, values, axis=1)\n    Array([[ 1, 10,  2,  3, 11],\n           [ 4, 12,  5,  6, 13]], dtype=int32)\n  \"\"\"\n  a, _, values_arr = util.ensure_arraylike(\"insert\", arr, 0 if isinstance(obj, slice) else obj, values)\n\n  if axis is None:\n    a = ravel(a)\n    axis = 0\n  axis = core.concrete_or_error(None, axis, \"axis argument of jnp.insert()\")\n  axis = _canonicalize_axis(axis, a.ndim)\n  if isinstance(obj, slice):\n    indices = arange(*obj.indices(a.shape[axis]))\n  else:\n    indices = asarray(obj)\n\n  if indices.ndim > 1:\n    raise ValueError(\"jnp.insert(): obj must be a slice, a one-dimensional \"\n                     f\"array, or a scalar; got {obj}\")\n  if not np.issubdtype(indices.dtype, np.integer):\n    if indices.size == 0 and not isinstance(obj, Array):\n      indices = indices.astype(int)\n    else:\n      # Note: np.insert allows boolean inputs but the behavior is deprecated.\n      raise ValueError(\"jnp.insert(): index array must be \"\n                       f\"integer typed; got {obj}\")\n  values_arr = array(values_arr, ndmin=a.ndim, dtype=a.dtype, copy=False)\n\n  if indices.size == 1:\n    index = ravel(indices)[0]\n    if indices.ndim == 0:\n      values_arr = moveaxis(values_arr, 0, axis)\n    indices = array_creation.full(values_arr.shape[axis], index)\n  n_input = a.shape[axis]\n  n_insert = broadcast_shapes(indices.shape, (values_arr.shape[axis],))[0]\n  out_shape = list(a.shape)","sourceCodeStart":7777,"sourceCodeEnd":7813,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L7777-L7813","documentation":"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.","triggerScenarios":"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.","commonSituations":"Reusing a gridded/meshed index array (from meshgrid or indices) as insertion points; passing a matrix of positions where a flat list was intended.","solutions":["Flatten obj before calling: obj.ravel() or obj.reshape(-1)","Rework logic to insert sequentially per row instead of one 2-D call","Pass a slice or scalar if only one insertion point is needed"],"exampleFix":"// before\njnp.insert(a, positions_2d, values, axis=0)\n// after\njnp.insert(a, positions_2d.ravel(), values, axis=0)\n","handlingStrategy":"validation","validationCode":"obj = jnp.asarray(obj)\nif obj.ndim > 1: obj = obj.ravel()","typeGuard":"def is_valid_insert_obj(obj):\n    return jnp.asarray(obj).ndim <= 1 or isinstance(obj, slice)","tryCatchPattern":null,"preventionTips":["Keep insertion positions flat (ravel meshgrid outputs)","Pass slices for contiguous insertion","Use vmap for per-row insertion logic"],"tags":["jax","numpy-compat","insert","ndim"],"backgroundTag":"index-array-dimension-error","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}