{"record":{"id":"e569a093b7f7dab5","repo":"jax-ml/jax","slug":"index-can-t-contain-negative-values","errorCode":null,"errorMessage":"index can't contain negative values","messagePattern":"index can't contain negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":4191,"sourceCode":"  nd = np.ndim(array)\n\n  if nd == 0:\n    return array\n\n  stat_funcs: dict[str, PadStatFunc] = {\n      \"maximum\": reductions.amax,\n      \"minimum\": reductions.amin,\n      \"mean\": reductions.mean,\n      \"median\": reductions.median\n  }\n\n  pad_width = _broadcast_to_pairs(pad_width, nd, \"pad_width\")\n  pad_width_arr = np.array(pad_width)\n  if pad_width_arr.shape != (nd, 2):\n    raise ValueError(f\"Expected pad_width to have shape {(nd, 2)}; got {pad_width_arr.shape}.\")\n\n  if np.any(pad_width_arr < 0):\n    raise ValueError(\"index can't contain negative values\")\n\n  if mode == \"constant\":\n    return _pad_constant(array, pad_width, asarray(constant_values))\n\n  elif mode == \"wrap\":\n    return _pad_wrap(array, pad_width)\n\n  elif mode in (\"symmetric\", \"reflect\"):\n    return _pad_symmetric_or_reflect(array, pad_width, str(mode), reflect_type)\n\n  elif mode == \"edge\":\n    return _pad_edge(array, pad_width)\n\n  elif mode == \"linear_ramp\":\n    end_values = _broadcast_to_pairs(end_values, nd, \"end_values\")\n    return _pad_linear_ramp(array, pad_width, end_values)\n\n  elif mode in stat_funcs:","sourceCodeStart":4173,"sourceCodeEnd":4209,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L4173-L4209","documentation":"jnp.pad rejects pad_width containing negative values; negative padding (numpy also disallows effective shrinking via negative widths in jnp) cannot be expressed, so any negative entry raises ValueError 'index can't contain negative values'.","triggerScenarios":"jnp.pad(x, (-1, 2)) or computed widths that go negative, e.g. (target_len - x.shape[0]) when the array is already longer than target.","commonSituations":"Padding sequences to a fixed length where some inputs already exceed the target, making the computed pad amount negative.","solutions":["Clamp computed widths: max(0, target - x.shape[0])","Use dynamic_slice or lax.slice to actually crop when shrink is intended"],"exampleFix":"// before\njnp.pad(x, (target - x.shape[0], 0))\n// after\njnp.pad(x, (max(0, target - x.shape[0]), 0))","handlingStrategy":"validation","validationCode":"assert np.all(np.asarray(pad_width) >= 0), 'negative pad width'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp computed widths with max(0, ...); use slicing for cropping"],"tags":["jnp-pad","negative-pad-width"],"backgroundTag":"negative-index-value","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}