jax-ml/jax · error · ValueError
Need at least one array to stack.
Error message
Need at least one array to stack.
What it means
jnp.stack must join at least one array along the new axis to determine the output shape. It fires when the arrays sequence is empty, leaving no elements to stack.
Solutions
- Guard with `if not arrays: ...` and supply a default/zero-shaped result
- Fix upstream logic so the list is never empty
Example fix
// before
jnp.stack(chunks, axis=0)
// after
if not chunks:
raise ValueError('no chunks to stack')
jnp.stack(chunks, axis=0) Defensive patterns
Strategy: validation
Validate before calling
if not arrays:
raise ValueError('nothing to stack') Type guard
def non_empty(seq) -> bool:
return len(seq) > 0 Prevention
- Guard dynamic list construction with emptiness checks; provide default outputs
When it happens
Trigger: jnp.stack([]) or jnp.stack([], axis=1); also jnp.stack of an empty generator/list built by a comprehension that filters everything out.
Common situations: Dynamic batch accumulation where a filter or conditions yield an empty list; empty minibatches; empty results in data pipelines.
Related errors
- Need at least one array to concatenate.
- Arguments to batch_matmul must be at least 2D, got
- Arguments to batch_matmul must have same ndim, got
- Invalid dimension range passed to collapse
- mode must be 'largest' or 'smallest', got
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/ea917e2f2df281f0.
Report an issue: GitHub.
Appendix: source
Thrown at jax/_src/numpy/lax_numpy.py:4429
>>> jnp.stack([x, y])
Array([[1, 2, 3],
[4, 5, 6]], dtype=int32)
>>> jnp.stack([x, y], axis=1)
Array([[1, 4],
[2, 5],
[3, 6]], dtype=int32)
:func:`~jax.numpy.unstack` performs the inverse operation:
>>> arr = jnp.stack([x, y], axis=1)
>>> x, y = jnp.unstack(arr, axis=1)
>>> x
Array([1, 2, 3], dtype=int32)
>>> y
Array([4, 5, 6], dtype=int32)
"""
if not len(arrays):
raise ValueError("Need at least one array to stack.")
if out is not None:
raise NotImplementedError("The 'out' argument to jnp.stack is not supported.")
if isinstance(arrays, (np.ndarray, Array)):
axis = _canonicalize_axis(axis, arrays.ndim)
return concatenate(expand_dims(arrays, axis + 1), axis=axis, dtype=dtype)
else:
arrays = util.ensure_arraylike_tuple("stack", arrays)
if dtype is not None:
arrays = [asarray(a, dtype=dtype) for a in arrays]
else:
arrays = util.promote_dtypes(*arrays)
return lax.stack(arrays, axis=axis)
@export
@api.jit(static_argnames="axis", inline=True)
def unstack(x: ArrayLike, /, *, axis: int = 0) -> tuple[Array, ...]:
"""Unstack an array along an axis.View on GitHub (pinned to 1e1c6a8fc0)