{"record":{"id":"34035d5ff58dbcb2","repo":"jax-ml/jax","slug":"expected-pad-width-to-have-shape-nd-2-got-p","errorCode":null,"errorMessage":"Expected pad_width to have shape {(nd, 2)}; got {pad_width_arr.shape}.","messagePattern":"Expected pad_width to have shape (.+?); got (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":4188,"sourceCode":"         constant_values: ArrayLike, stat_length: PadValueLike[int] | None,\n         end_values: PadValueLike[ArrayLike], reflect_type: str):\n  array = asarray(array)\n  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\")","sourceCodeStart":4170,"sourceCodeEnd":4206,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L4170-L4206","documentation":"After _broadcast_to_pairs normalization, jnp.pad requires the resulting pad_width array to have exactly shape (nd, 2) (one (before, after) per dimension). This check catches width specifications that normalized to a different shape.","triggerScenarios":"Rare: pad_width whose conversion via np.array yields unexpected shape, e.g. mixed types producing an object array or extra dimensions beyond the handled shapes in _broadcast_to_pairs.","commonSituations":"Passing pad_width as an object array of mixed scalars/arrays; inconsistent nesting depth.","solutions":["Convert pad_width to a plain (nd, 2) int array or list of pairs before calling jnp.pad","Avoid mixing arrays and scalars inside pad_width"],"exampleFix":"// before\njnp.pad(x, np.array([[1, 2], [np.array(3), 4]], dtype=object))\n// after\njnp.pad(x, np.array([[1, 2], [3, 4]]))","handlingStrategy":"validation","validationCode":"w = np.array(pad_width)\nassert w.shape == (x.ndim, 2), f'pad_width must be {(x.ndim, 2)}, got {w.shape}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Normalize pad_width to an (nd, 2) int numpy array before calling jnp.pad"],"tags":["jnp-pad","pad-width","shape-validation"],"backgroundTag":"invalid-argument-shape","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}