{"record":{"id":"760e5bc9f8595714","repo":"jax-ml/jax","slug":"cannot-reshape-array-of-shape-arr-shape-size-a","errorCode":null,"errorMessage":"cannot reshape array of shape {arr.shape} (size {arr.size}) into shape {orig_newshape} because the product of specified axis sizes ({math.prod(other_sizes)}) does not evenly divide {arr.size}","messagePattern":"cannot reshape array of shape (.+?) \\(size (.+?)\\) into shape (.+?) because the product of specified axis sizes \\((.+?)\\) does not evenly divide (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/array_methods.py","lineNumber":532,"sourceCode":"  \"\"\"Fixes a -1 value in newshape, if present.\"\"\"\n  orig_newshape = newshape  # for error messages\n  try:\n    iter(newshape)  # pyrefly: ignore[no-matching-overload]\n  except TypeError:\n    newshape = [newshape]\n  else:\n    newshape: Sequence[DimSize]  # pyrefly: ignore[redefinition]\n  newshape = core.canonicalize_shape(newshape)\n  neg1s = [i for i, d in enumerate(newshape) if type(d) is int and d == -1]\n  if len(neg1s) > 1:\n    raise TypeError(\"can only specify one unknown axis size with a `-1` value, \"\n                    f\"got {orig_newshape}\")\n  if neg1s:\n    i, = neg1s\n    other_sizes = (*newshape[:i], *newshape[i+1:])\n    if (all(isinstance(d, int) for d in (*arr.shape, *other_sizes)) and\n        arr.size % math.prod(other_sizes) != 0):\n      raise TypeError(f\"cannot reshape array of shape {arr.shape} (size {arr.size}) \"\n                      f\"into shape {orig_newshape} because the product of \"\n                      f\"specified axis sizes ({math.prod(other_sizes)}) does \"\n                      f\"not evenly divide {arr.size}\")\n    sz = core.cancel_divide_tracers(arr.shape, other_sizes)\n    if sz is not None:\n      return (*newshape[:i], sz, *newshape[i+1:])\n  else:\n    if (all(isinstance(d, int) for d in (*arr.shape, *newshape)) and\n        arr.size != math.prod(newshape)):\n      raise TypeError(f\"cannot reshape array of shape {arr.shape} (size {arr.size}) \"\n                      f\"into shape {orig_newshape} (size {math.prod(newshape)})\")\n  return tuple(-core.divide_shape_sizes(arr.shape, newshape)\n               if core.definitely_equal(d, -1) else d for d in newshape)\n\ndef _view(self: Array, dtype: DTypeLike | None = None, type: None = None) -> Array:\n  \"\"\"Return a bitwise copy of the array, viewed as a new dtype.\n\n  This is fuller-featured wrapper around :func:`jax.lax.bitcast_convert_type`.","sourceCodeStart":514,"sourceCodeEnd":550,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/array_methods.py#L514-L550","documentation":"When reshaping with a single -1, JAX checks that the product of the explicitly-given axis sizes evenly divides the array's total size; otherwise the inferred dimension would be non-integral. This divisibility check only runs when all relevant sizes are concrete ints (not tracers).","triggerScenarios":"`arr.reshape(-1, k)` where arr.size is not divisible by k, e.g. a size-10 array reshaped to (-1, 3).","commonSituations":"Hard-coded feature dimensions after a data change (e.g. flattened dims no longer match); off-by-one in sequence/window lengths feeding a reshape.","solutions":["Fix the target shape so sizes divide arr.size (e.g. use (-1, arr.size % k == 0 and k) or correct k)","Print/inspect arr.shape and arr.size before reshaping in data-dependent code","Use arr.reshape(arr.size // k, k) after asserting divisibility"],"exampleFix":"# before\nx = jnp.zeros(10)\nx.reshape(-1, 3)  # 10 % 3 != 0\n\n# after\nx.reshape(-1, 5)  # 10 % 5 == 0","handlingStrategy":"validation","validationCode":"def reshape_infer(arr, k):\n    assert arr.size % k == 0, f'{arr.size} not divisible by {k}'\n    return arr.reshape(-1, k)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Assert divisibility before -1 reshapes","Log arr.shape/arr.size in data pipelines where sizes vary"],"tags":["jax","reshape","size-mismatch"],"backgroundTag":"invalid-reshape-shape","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}