{"record":{"id":"9f303d0ba191ec7e","repo":"jax-ml/jax","slug":"concatenate-expects-at-least-one-operand-got-0","errorCode":null,"errorMessage":"concatenate expects at least one operand, got 0.","messagePattern":"concatenate expects at least one operand, got 0\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":7231,"sourceCode":"                             sharding_rule=_clamp_sharding_rule,\n                             vma_rule=partial(core.standard_vma_rule, 'clamp'))\nad.defjvp(clamp_p,\n          lambda g, min, operand, max:\n          select(bitwise_and(gt(min, operand), lt(min, max)),\n                 g, _zeros(operand)),\n          lambda g, min, operand, max:\n          select(bitwise_and(gt(operand, min), lt(operand, max)),\n                 g, _zeros(operand)),\n          lambda g, min, operand, max:\n          select(lt(max, operand), g, _zeros(operand)))\nbatching.primitive_batchers[clamp_p] = _clamp_batch_rule\nmlir.register_lowering(clamp_p, partial(_nary_lower_hlo, hlo.clamp))\n\ndef _concatenate_shape_rule(*operands, **kwargs):\n  dimension = kwargs.pop('dimension')\n  if not operands:\n    msg = \"concatenate expects at least one operand, got 0.\"\n    raise TypeError(msg)\n  if not all(isinstance(operand, ShapedArray) for operand in operands):\n    msg = \"All objects to concatenate must be arrays, got {}.\"\n    op = next(op for op in operands if not isinstance(op, ShapedArray))\n    raise TypeError(msg.format(type(op)))\n  if len({operand.ndim for operand in operands}) != 1:\n    msg = \"Cannot concatenate arrays with different numbers of dimensions: got {}.\"\n    raise TypeError(msg.format(\", \".join(str(o.shape) for o in operands)))\n  if not 0 <= dimension < operands[0].ndim:\n    msg = \"concatenate dimension out of bounds: dimension {} for shapes {}.\"\n    raise TypeError(msg.format(dimension, \", \".join([str(o.shape) for o in operands])))\n  shapes = [operand.shape[:dimension] + operand.shape[dimension+1:]\n            for operand in operands]\n  if shapes[:-1] != shapes[1:]:\n    msg = (\"Cannot concatenate arrays with shapes that differ in dimensions \"\n           \"other than the one being concatenated: concatenating along \"\n           \"dimension {} for shapes {}.\")\n    shapes = [operand.shape for operand in operands]\n    raise TypeError(msg.format(dimension, \", \".join(map(str, shapes))))","sourceCodeStart":7213,"sourceCodeEnd":7249,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L7213-L7249","documentation":"Raised by JAX's internal concatenate shape-checking rule when lax.concatenate (and APIs built on it like jnp.concatenate) is called with an empty operand list. JAX needs at least one array to infer ndim and result shape, so zero operands is a TypeError at trace time.","triggerScenarios":"Calling jnp.concatenate([]) or jax.lax.concatenate([], dimension=0); building an operand list in a loop/filter that ends up empty.","commonSituations":"Dynamically collecting arrays to concat (e.g. filtering batches) where the filter removes everything; refactoring a list comprehension so it can return [].","solutions":["Ensure the list of arrays is non-empty before concatenating: guard with `if arrays:` and provide a default/zero-length array of the right ndim","If empty input is legitimate, construct the result shape explicitly, e.g. jnp.zeros((0, feat_dim)) instead of concatenating nothing","Check upstream code that produced the list (loops, filters, dataset iteration) for off-by-one or empty-input cases"],"exampleFix":"// before\nout = jnp.concatenate([x for x in batch if keep(x)], axis=0)\n// after\nparts = [x for x in batch if keep(x)]\nout = jnp.concatenate(parts, axis=0) if parts else jnp.zeros((0, feat_dim))","handlingStrategy":"validation","validationCode":"if not arrays:\n    out = jnp.zeros((0,) + trailing_shape)\nelse:\n    out = jnp.concatenate(arrays, axis=0)","typeGuard":"def non_empty_arrays(xs) -> bool:\n    return len(xs) > 0 and all(hasattr(x, 'shape') for x in xs)","tryCatchPattern":null,"preventionTips":["Never concatenate unconditionally on filtered/loop-built lists; check emptiness first","Standardize an empty-batch representation (e.g. shape (0, feat)) in data pipelines"],"tags":["jax","concatenate","shape-validation","empty-input"],"backgroundTag":"empty-sequence-operation","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}