{"record":{"id":"6b11f8bca51fdfb9","repo":"jax-ml/jax","slug":"name-entries-must-be-the-same-shape-nvals","errorCode":null,"errorMessage":"`{name}` entries must be the same shape: {nvals}","messagePattern":"`(.+?)` entries must be the same shape: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":3896,"sourceCode":"\ntype PadValueLike[T] = Union[T, Sequence[T], Sequence[Sequence[T]]]\ntype PadValue[T] = tuple[tuple[T, T], ...]\n\nclass PadStatFunc(Protocol):\n  def __call__(self, array: ArrayLike, /, *,\n               axis: int | None = None,\n               keepdims: bool = False) -> Array: ...\n\n\ndef _broadcast_to_pairs(nvals: PadValueLike[Any], nd: int, name: str) -> PadValue[Any]:\n  try:\n    nvals = np.asarray(tree_map(\n      lambda x: core.concrete_or_error(None, x, context=f\"{name} argument of jnp.pad\"),\n      nvals))\n  except ValueError as e:\n    # In numpy 1.24\n    if \"array has an inhomogeneous shape\" in str(e):\n      raise TypeError(f'`{name}` entries must be the same shape: {nvals}') from e\n    raise\n\n  def as_scalar_dim(v):\n    if core.is_dim(v) or not np.shape(v):\n      return v\n    else:\n      raise TypeError(f'`{name}` entries must be the same shape: {nvals}')\n\n  if nvals.shape == (nd, 2):\n    # ((before_1, after_1), ..., (before_N, after_N))\n    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])","sourceCodeStart":3878,"sourceCodeEnd":3914,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L3878-L3914","documentation":"jnp.pad converts pad_width/constant_values-like arguments into a concrete numpy array via tree_map. If the entries have inhomogeneous shapes (e.g. a Python list like [[1,2],[3]]), numpy >=1.24 raises 'array has an inhomogeneous shape', and JAX re-raises it as a TypeError explaining the `{name}` entries must all share the same shape.","triggerScenarios":"Calling jnp.pad with pad_width or constant_values containing ragged nested sequences, e.g. jnp.pad(x, [[1,2],[3]]) or constant_values=((0,1),(2,)).","commonSituations":"Dynamically building pad_width from per-axis lists where some axes got a scalar and others got a pair; passing a ragged list constructed in a loop.","solutions":["Make all entries the same shape: use (before, after) pairs for every axis or a single scalar/pair","If building pad_width programmatically, normalize each entry to a tuple of two ints before calling jnp.pad","Pass a numpy array or use np.broadcast_to on your width specification"],"exampleFix":"// before\njnp.pad(x, [[1, 2], [3]])\n// after\njnp.pad(x, [[1, 2], [3, 3]])","handlingStrategy":"validation","validationCode":"pad_w = np.array([[1,2],[3,3]])\nassert pad_w.dtype != object and len({len(r) for r in pad_w}) == 1, 'ragged pad_width'","typeGuard":"def is_regular_pairs(w) -> bool:\n    return isinstance(w, (list, tuple)) and all(isinstance(r, (list, tuple)) and len(r) == len(w[0]) for r in w)","tryCatchPattern":null,"preventionTips":["Always build pad_width as list of 2-tuples, one per axis","Convert width specs to a numpy int array before calling jnp.pad"],"tags":["jnp-pad","shape-mismatch","numpy"],"backgroundTag":"ragged-array-shape-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}