{"record":{"id":"15c477a3aca0b81c","repo":"jax-ml/jax","slug":"can-only-specify-one-unknown-axis-size-with-a-1","errorCode":null,"errorMessage":"can only specify one unknown axis size with a `-1` value, got {orig_newshape}","messagePattern":"can only specify one unknown axis size with a `-1` value, got (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/array_methods.py","lineNumber":525,"sourceCode":"  Refer to :func:`jax.numpy.var` for full documentation.\n  \"\"\"\n  return reductions.var(self, axis=axis, dtype=dtype, out=out, ddof=ddof,\n                        keepdims=keepdims, where=where, correction=correction)\n\n\ndef _compute_newshape(arr: Array, newshape: DimSize | Shape) -> Shape:\n  \"\"\"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)})\")","sourceCodeStart":507,"sourceCodeEnd":543,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/array_methods.py#L507-L543","documentation":"When reshaping with a -1 placeholder, only one axis may be unspecified because its size is inferred from the others. `_compute_newshape` counts int entries equal to -1 and raises if there is more than one. This mirrors NumPy's rule and is checked before any shape arithmetic.","triggerScenarios":"Calling reshape with two or more -1s, e.g. `arr.reshape(-1, -1)` or `jnp.reshape(arr, (-1, 3, -1))`.","commonSituations":"Dynamically built reshape shapes where a batch axis and a feature axis are both left as -1; typos when copying shapes.","solutions":["Specify all but one dimension explicitly","Compute the inferred dimension yourself: `arr.reshape(arr.shape[0], -1)` for batch-major data"],"exampleFix":"# before\narr.reshape(-1, -1)\n\n# after\narr.reshape(arr.shape[0], -1)","handlingStrategy":"validation","validationCode":"def count_neg1s(shape):\n    return sum(1 for d in shape if isinstance(d, int) and d == -1)\n\ndef assert_valid_reshape(shape):\n    assert count_neg1s(shape) <= 1, 'at most one -1 allowed'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Leave exactly one inferred dimension","Prefer arr.reshape(arr.shape[0], -1) in batched code"],"tags":["jax","reshape","invalid-shape"],"backgroundTag":"invalid-reshape-shape","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}