jax-ml/jax · error · ValueError

Zero-dimensional arrays cannot be concatenated.

Error message

Zero-dimensional arrays cannot be concatenated.

What it means

In the single-ndarray fast path of concatenate, a 1-D input array (after axis is not None) has no remaining dimensions to concatenate over — numpy semantics treat it as a list of scalars which jnp rejects for 1-D with a set axis.

Source

Thrown at jax/_src/numpy/lax_numpy.py:4537

                   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:
    arrays: a sequence of arrays to concatenate; each must have the same shape
      except along the specified axis. If a single array is given it will be
      treated equivalently to `arrays = unstack(arrays)`, but the implementation
      will avoid explicit unstacking.

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Wrap the array in a list: jnp.concatenate([arr], axis)
  2. Use axis=None if you wanted a flattened single-array result

Example fix

// before
jnp.concatenate(arr, axis=0)
// after
jnp.concatenate([arr], axis=0)
Defensive patterns

Strategy: validation

Validate before calling

assert np.ndim(arrays) >= 2 or isinstance(arrays, (list, tuple)), 'wrap single arrays in a list'

Prevention

When it happens

Trigger: jnp.concatenate(np.array([1, 2, 3]), axis=0) — passing a 1-D ndarray directly instead of a list of arrays.

Common situations: Forgetting brackets: passing arr instead of [arr]; converting a list to ndarray before concatenate.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/f63e99ac546d117b. Report an issue: GitHub.