{"record":{"id":"31c30af55f799b49","repo":"jax-ml/jax","slug":"jnp-pad-constant-values-has-unsupported-shape-co","errorCode":null,"errorMessage":"jnp.pad: constant_values has unsupported shape {constant_values.shape}. If the shape is 1D or 2D, the last dimension must be of size 1 or 2.","messagePattern":"jnp\\.pad: constant_values has unsupported shape (.+?)\\. If the shape is 1D or 2D, the last dimension must be of size 1 or 2\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":3950,"sourceCode":"    raise ValueError(msg.format(mode))\n\n\ndef _pad_constant(array: Array, pad_width: PadValue[int], constant_values: Array) -> Array:\n  nd = np.ndim(array)\n  constant_values = lax._convert_element_type(\n      constant_values, array.dtype, dtypes.is_weakly_typed(array))\n  constant_values_nd = np.ndim(constant_values)\n\n  if constant_values_nd == 0:\n    widths = [(low, high, 0) for (low, high) in pad_width]\n    return lax.pad(array, constant_values, widths)\n\n  if constant_values_nd == 1:\n    if constant_values.shape[-1] == 1:\n      widths = [(low, high, 0) for (low, high) in pad_width]\n      return lax.pad(array, squeeze(constant_values), widths)\n    elif constant_values.shape[-1] != 2:\n      raise ValueError(\"jnp.pad: constant_values has unsupported shape \"\n                      f\"{constant_values.shape}. If the shape is 1D or 2D, the \"\n                      \"last dimension must be of size 1 or 2.\")\n\n  constant_values = broadcast_to(constant_values, (nd, 2))\n  for i in range(nd):\n    widths = [(0, 0, 0)] * nd\n    if pad_width[i][0] != 0:\n      widths[i] = (pad_width[i][0], 0, 0)\n      array = lax.pad(array, constant_values[i, 0], widths)\n    if pad_width[i][1] != 0:\n      widths[i] = (0, pad_width[i][1], 0)\n      array = lax.pad(array, constant_values[i, 1], widths)\n  return array\n\n\ndef _pad_wrap(array: Array, pad_width: PadValue[int]) -> Array:\n  for i in range(np.ndim(array)):\n    if array.shape[i] == 0:","sourceCodeStart":3932,"sourceCodeEnd":3968,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L3932-L3968","documentation":"For mode='constant', jnp.pad requires constant_values to be a scalar or have last dimension of size 1 or 2 (one value, or before/after values per axis). A 1-D constant_values with last dim other than 1 or 2 is invalid.","triggerScenarios":"jnp.pad(x, 2, constant_values=jnp.array([1,2,3])) — shape (3,), last dim is 3, invalid.","commonSituations":"Passing per-axis constant lists of the wrong length, e.g. 3 values for a 2-axis problem, or forgetting to nest per-axis pairs.","solutions":["Use a scalar constant, a (nd,1) or (nd,2) array, or a single pair"],"exampleFix":"// before\njnp.pad(x, 1, constant_values=[1, 2, 3])\n// after\njnp.pad(x, 1, constant_values=[(1, 2), (1, 2)])","handlingStrategy":"validation","validationCode":"cv = np.asarray(constant_values)\nassert cv.ndim == 0 or cv.shape[-1] in (1, 2), 'constant_values last dim must be 1 or 2'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer scalar constant_values; use per-axis (before, after) pairs only when needed"],"tags":["jnp-pad","constant-values","invalid-argument-shape"],"backgroundTag":"invalid-argument-shape","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}