{"record":{"id":"fad09aba2bf8d7ee","repo":"jax-ml/jax","slug":"cannot-reshape-array-of-shape-arr-shape-size-a-fad09a","errorCode":null,"errorMessage":"cannot reshape array of shape {arr.shape} (size {arr.size}) into shape {orig_newshape} (size {math.prod(newshape)})","messagePattern":"cannot reshape array of shape (.+?) \\(size (.+?)\\) into shape (.+?) \\(size (.+?)\\)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/array_methods.py","lineNumber":542,"sourceCode":"  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`.\n\n  If the source and target dtype have the same bitwidth, the result has the same\n  shape as the input array. If the bitwidth of the target dtype is different\n  from the source, the size of the last axis of the result is adjusted\n  accordingly.\n\n  >>> jnp.zeros([1,2,3], dtype=jnp.int16).view(jnp.int8).shape\n  (1, 2, 6)\n  >>> jnp.zeros([1,2,4], dtype=jnp.int8).view(jnp.int16).shape\n  (1, 2, 2)","sourceCodeStart":524,"sourceCodeEnd":560,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/array_methods.py#L524-L560","documentation":"For a fully-specified new shape, `_compute_newshape` verifies that the product of the new dimensions equals the array's total size (when all sizes are concrete ints). NumPy has the same rule ('cannot reshape array of size N into shape ...'); JAX phrases it with shape and size details.","triggerScenarios":"`arr.reshape(2, 3)` where arr.size != 6, e.g. reshaping a size-12 array into (2, 3).","commonSituations":"Data shape changes upstream (different batch size, image resolution, channels-last vs channels-first); stale hard-coded shapes in model input pipelines.","solutions":["Check arr.shape/arr.size and update the target shape","Derive one dimension from the data: arr.reshape(arr.shape[0], -1)","Add an assert on the incoming shape early in the pipeline to fail near the cause"],"exampleFix":"# before\nimgs.reshape(32, 28, 28)  # imgs.shape == (32, 784)\n\n# after\nimgs.reshape(32, 28, 28) if imgs.ndim == 2 else imgs  # or\nimgs.reshape(imgs.shape[0], 28, 28)","handlingStrategy":"validation","validationCode":"def reshape_exact(arr, shape):\n    import math\n    assert arr.size == math.prod(s for s in shape if s != -1) or math.prod(shape) == arr.size\n    return arr.reshape(shape)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive one dim from data: reshape(n, -1)","Fail fast on input shapes at pipeline entry"],"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"}