{"record":{"id":"06e752688dd6d38b","repo":"jax-ml/jax","slug":"stack-expects-at-least-one-operand-got-0","errorCode":null,"errorMessage":"stack expects at least one operand, got 0.","messagePattern":"stack expects at least one operand, got 0\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":7347,"sourceCode":"  dimension_attr = mlir.i64_attr(dimension)\n  while len(current_xs) > 1:\n    current_xs = [hlo.concatenate(current_xs[i:i+k], dimension_attr)\n                  for i in range(0, len(current_xs), k)]\n  return current_xs[0]\n\ndef _concatenate_lower(ctx, *xs, dimension):\n  aval_out, = ctx.avals_out\n  out = _concatenate_tree(xs, dimension)\n  return [mlir.lower_with_sharding_in_types(ctx, out, aval_out)]\n\nmlir.register_lowering(concatenate_p, _concatenate_lower)\n\n# --- stack and unstack primitives ---\n\ndef _stack_shape_rule(*operands, axis):\n  if not operands:\n    msg = \"stack expects at least one operand, got 0.\"\n    raise ValueError(msg)\n  if len({op.ndim for op in operands}) != 1:\n    msg = \"Cannot stack arrays with different numbers of dimensions: got {}.\"\n    raise ValueError(msg.format(\", \".join(str(o.shape) for o in operands)))\n  if len({op.shape for op in operands}) != 1:\n    msg = \"All input arrays must have the same shape. Got {}.\"\n    raise ValueError(msg.format(\", \".join(str(o.shape) for o in operands)))\n\n  shape = list(operands[0].shape)\n  shape.insert(axis, len(operands))\n  return tuple(shape)\n\ndef _stack_dtype_rule(*operands, axis):\n  check_same_dtypes('stack', *operands)\n  return operands[0].dtype\n\ndef _stack_sharding_rule(*operands, axis):\n  non_empty_s = [o.sharding for o in operands if not o.sharding.mesh.empty]\n  if not non_empty_s:","sourceCodeStart":7329,"sourceCodeEnd":7365,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L7329-L7365","documentation":"lax.stack (backing jnp.stack) needs at least one array to determine the output shape; an empty operand list raises this ValueError during shape rule evaluation. The result shape is each input's shape with a new axis of length len(operands), which is undefined for zero operands.","triggerScenarios":"jnp.stack([]); lax.stack([], axis=0); stacking a dynamically filtered list that becomes empty.","commonSituations":"vmap/pmap pipelines where a mapped axis has size 0; collecting per-item results in a loop that never executes; empty-batch edge case in data loaders.","solutions":["Guard empty input: if not arrays: construct the stacked shape explicitly (insert 0 at the axis) using jnp.zeros","Fix upstream generation so at least one element exists (validate dataset/batch size)","Handle the empty case at a higher level, e.g. return an empty result of the right dtype/shape"],"exampleFix":"# before\nout = jnp.stack(parts, axis=0)  # parts == []\n# after\nout = jnp.stack(parts, axis=0) if parts else jnp.zeros((0,) + item_shape, dtype=dtype)","handlingStrategy":"validation","validationCode":"if not arrays:\n    stacked = jnp.zeros((0,) + item_shape, dtype=dtype)\nelse:\n    stacked = jnp.stack(arrays, axis=0)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Guard all stack sites built from loops with an emptiness check","Validate batch size > 0 in data loaders before stacking"],"tags":["jax","stack","empty-input","shape-validation"],"backgroundTag":"empty-sequence-operation","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}