jax-ml/jax · error · ValueError
Need at least one array to concatenate.
Error message
Need at least one array to concatenate.
What it means
When concatenate receives a single ndarray (not a list) as fast path, _concatenate_array treats dimension 0 as the stack of inputs. A 0-d array or an empty leading dimension means there are zero arrays to concatenate, raising ValueError.
Source
Thrown at jax/_src/numpy/lax_numpy.py:4533
reps_tup = tuple(iter(reps)) # pyrefly: ignore[no-matching-overload]
except TypeError:
reps_tup: tuple[DimSize, ...] = (reps,)
reps_tup = tuple(operator.index(rep) if core.is_constant_dim(rep) else rep
for rep in reps_tup)
# lax.tile expects reps and A.shape to have the same rank.
reps_tup = (1,) * (A.ndim - len(reps_tup)) + reps_tup
if len(reps_tup) > np.ndim(A):
A = lax.expand_dims(
A, dimensions=tuple(range(len(reps_tup) - np.ndim(A))))
return lax.tile(A, reps_tup)
def _concatenate_array(arr: ArrayLike, axis: int | None,
dtype: DTypeLike | None = None) -> Array:
# Fast path for concatenation when the input is an ndarray rather than a list.
arr = asarray(arr, dtype=dtype)
if arr.ndim == 0 or arr.shape[0] == 0:
raise ValueError("Need at least one array to concatenate.")
if axis is None:
return lax.reshape(arr, (arr.size,))
if arr.ndim == 1:
raise ValueError("Zero-dimensional arrays cannot be concatenated.")
axis = _canonicalize_axis(axis, arr.ndim - 1)
shape = arr.shape[1:axis + 1] + (arr.shape[0] * arr.shape[axis + 1],) + arr.shape[axis + 2:]
dimensions = [*range(1, axis + 1), 0, *range(axis + 1, arr.ndim)]
return lax.reshape(arr, shape, dimensions)
@export
def concatenate(arrays: np.ndarray | Array | Sequence[ArrayLike],
axis: int | None = 0, dtype: DTypeLike | None = None) -> Array:
"""Join arrays along an existing axis.
JAX implementation of :func:`numpy.concatenate`.
Args:View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Pass a sequence of arrays: jnp.concatenate(list_of_arrays, axis)
- Guard empty inputs before calling concatenate
Example fix
// before jnp.concatenate(np.asarray(chunks)) // after jnp.concatenate(chunks, axis=0)
Defensive patterns
Strategy: validation
Validate before calling
assert not isinstance(arrays, np.ndarray) or arrays.ndim >= 2 and arrays.shape[0] > 0, 'no arrays to concatenate'
Prevention
- Always pass a list/tuple of arrays to concatenate, not an ndarray
When it happens
Trigger: jnp.concatenate(np.array([])) or jnp.concatenate(np.zeros((0, 3)), axis=1) — passing an ndarray whose first axis is empty instead of a list of arrays.
Common situations: A variable that is sometimes a list of arrays and sometimes a single array/empty array; np.asarray applied to a list before concatenate collapses it.
Related errors
- Need at least one array to stack.
- repeated axis in lax.expand_dims: {dimensions}
- Invalid dimension range passed to collapse: {operand.shape}[
- Arguments to batch_matmul must be at least 2D, got {}, {}
- Arguments to batch_matmul must have same ndim, got {}, {}
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/c60baa828fbb3cc1.
Report an issue: GitHub.