{"record":{"id":"8a4e14fb3811cd08","repo":"jax-ml/jax","slug":"jnp-pad-name-with-nd-has-unsupported-shape","errorCode":null,"errorMessage":"jnp.pad: {name} with {nd=} has unsupported shape {nvals.shape}. Valid shapes are ({nd}, 2), (1, 2), (2,), (1,), or ().","messagePattern":"jnp\\.pad: (.+?) with (.+?) has unsupported shape (.+?)\\. Valid shapes are \\((.+?), 2\\), \\(1, 2\\), \\(2,\\), \\(1,\\), or \\(\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":3925,"sourceCode":"    return tuple((as_scalar_dim(nval[0]), as_scalar_dim(nval[1])) for nval in nvals)\n  elif nvals.shape == (1, 2):\n    # ((before, after),)\n    v1_2 = as_scalar_dim(nvals[0, 0]), as_scalar_dim(nvals[0, 1])\n    return tuple(v1_2 for i in range(nd))\n  elif nvals.shape == (2,):\n    # (before, after)  (not in the numpy docstring but works anyway)\n    v1_2 = as_scalar_dim(nvals[0]), as_scalar_dim(nvals[1])\n    return tuple(v1_2 for i in range(nd))\n  elif nvals.shape == (1,):\n    # (pad,)\n    v = as_scalar_dim(nvals[0])\n    return tuple((v, v) for i in range(nd))\n  elif nvals.shape == ():\n    # pad\n    v = as_scalar_dim(nvals.flat[0])\n    return tuple((v, v) for i in range(nd))\n  else:\n    raise ValueError(f\"jnp.pad: {name} with {nd=} has unsupported shape {nvals.shape}. \"\n                     f\"Valid shapes are ({nd}, 2), (1, 2), (2,), (1,), or ().\")\n\n\ndef _check_no_padding(axis_padding: tuple[Any, Any], mode: str):\n  if (axis_padding[0] > 0 or axis_padding[1] > 0):\n    msg = \"Cannot apply '{}' padding to empty axis\"\n    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)","sourceCodeStart":3907,"sourceCodeEnd":3943,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L3907-L3943","documentation":"jnp.pad normalizes pad_width/constant_values/stat_length-like arguments to a shape of (nd, 2). Any input whose numpy shape is not (nd,2), (1,2), (2,), (1,), or () is rejected with this ValueError listing valid shapes.","triggerScenarios":"Passing e.g. jnp.pad(x, (1,2,3)) (shape (3,)), or a (4,2) width array for a 2-D input, or a shape like (2,1).","commonSituations":"Assuming jnp.pad accepts arbitrary per-axis triples; passing more entries than the array has dimensions; transposed width arrays.","solutions":["Provide exactly (before, after) pairs for each of the nd axes, or a single pair/scalar","Check x.ndim and reshape your width array to (x.ndim, 2)"],"exampleFix":"// before\njnp.pad(x, (1, 2, 3))\n// after\njnp.pad(x, [(1, 2), (1, 2)])  # for 2-D x","handlingStrategy":"validation","validationCode":"import numpy as np\nw = np.asarray(pad_width)\nnd = x.ndim\nassert w.shape in [(nd,2),(1,2),(2,),(1,),()], f'bad pad_width shape {w.shape}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the canonical (nd, 2) list-of-pairs form everywhere in your codebase"],"tags":["jnp-pad","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"}