{"record":{"id":"ea917e2f2df281f0","repo":"jax-ml/jax","slug":"need-at-least-one-array-to-stack","errorCode":null,"errorMessage":"Need at least one array to stack.","messagePattern":"Need at least one array to stack\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":4429,"sourceCode":"    >>> jnp.stack([x, y])\n    Array([[1, 2, 3],\n           [4, 5, 6]], dtype=int32)\n    >>> jnp.stack([x, y], axis=1)\n    Array([[1, 4],\n           [2, 5],\n           [3, 6]], dtype=int32)\n\n    :func:`~jax.numpy.unstack` performs the inverse operation:\n\n    >>> arr = jnp.stack([x, y], axis=1)\n    >>> x, y = jnp.unstack(arr, axis=1)\n    >>> x\n    Array([1, 2, 3], dtype=int32)\n    >>> y\n    Array([4, 5, 6], dtype=int32)\n  \"\"\"\n  if not len(arrays):\n    raise ValueError(\"Need at least one array to stack.\")\n  if out is not None:\n    raise NotImplementedError(\"The 'out' argument to jnp.stack is not supported.\")\n  if isinstance(arrays, (np.ndarray, Array)):\n    axis = _canonicalize_axis(axis, arrays.ndim)\n    return concatenate(expand_dims(arrays, axis + 1), axis=axis, dtype=dtype)\n  else:\n    arrays = util.ensure_arraylike_tuple(\"stack\", arrays)\n    if dtype is not None:\n      arrays = [asarray(a, dtype=dtype) for a in arrays]\n    else:\n      arrays = util.promote_dtypes(*arrays)\n    return lax.stack(arrays, axis=axis)\n\n\n@export\n@api.jit(static_argnames=\"axis\", inline=True)\ndef unstack(x: ArrayLike, /, *, axis: int = 0) -> tuple[Array, ...]:\n  \"\"\"Unstack an array along an axis.","sourceCodeStart":4411,"sourceCodeEnd":4447,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L4411-L4447","documentation":"jnp.stack requires a non-empty sequence of arrays; stacking zero arrays has no defined dtype/shape, so it raises ValueError immediately.","triggerScenarios":"jnp.stack([]) or jnp.stack([], axis=1); also jnp.stack of an empty generator/list built by a comprehension that filters everything out.","commonSituations":"Dynamic batch accumulation where a filter or conditions yield an empty list; empty minibatches; empty results in data pipelines.","solutions":["Guard with `if not arrays: ...` and supply a default/zero-shaped result","Fix upstream logic so the list is never empty"],"exampleFix":"// before\njnp.stack(chunks, axis=0)\n// after\nif not chunks:\n    raise ValueError('no chunks to stack')\njnp.stack(chunks, axis=0)","handlingStrategy":"validation","validationCode":"if not arrays:\n    raise ValueError('nothing to stack')","typeGuard":"def non_empty(seq) -> bool:\n    return len(seq) > 0","tryCatchPattern":null,"preventionTips":["Guard dynamic list construction with emptiness checks; provide default outputs"],"tags":["jnp-stack","empty-sequence","value-error"],"backgroundTag":"empty-sequence-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}