{"record":{"id":"963b332c16acaefd","repo":"jax-ml/jax","slug":"jax-numpy-block-does-not-allow-empty-list-argument","errorCode":null,"errorMessage":"jax.numpy.block does not allow empty list arguments","messagePattern":"jax\\.numpy\\.block does not allow empty list arguments","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":4996,"sourceCode":"    arr = clip(a, 0, N - 1)\n  else:\n    raise ValueError(f\"mode={mode!r} not understood. Must be 'raise', 'wrap', or 'clip'\")\n\n  arr, *choices = broadcast_arrays(arr, *choices)\n  return array(choices)[(arr,) + indices(arr.shape, sparse=True)]\n\n\ndef _atleast_nd(x: ArrayLike, n: int) -> Array:\n  m = np.ndim(x)\n  return lax.broadcast(x, (1,) * (n - m)) if m < n else asarray(x)\n\ndef _block(xs: ArrayLike | list[Any]) -> tuple[Array, int]:\n  if isinstance(xs, tuple):\n    raise ValueError(\"jax.numpy.block does not allow tuples, got {}\"\n                     .format(xs))\n  elif isinstance(xs, list):\n    if len(xs) == 0:\n      raise ValueError(\"jax.numpy.block does not allow empty list arguments\")\n    xs_tup, depths = unzip2([_block(x) for x in xs])\n    if any(d != depths[0] for d in depths[1:]):\n      raise ValueError(\"Mismatched list depths in jax.numpy.block\")\n    rank = max(depths[0], max(np.ndim(x) for x in xs_tup))\n    xs_tup = tuple(_atleast_nd(x, rank) for x in xs_tup)\n    return concatenate(xs_tup, axis=-depths[0]), depths[0] + 1\n  else:\n    return asarray(xs), 1\n\n\n@export\n@api.jit\ndef block(arrays: ArrayLike | list[Any]) -> Array:\n  \"\"\"Create an array from a list of blocks.\n\n  JAX implementation of :func:`numpy.block`.\n\n  Args:","sourceCodeStart":4978,"sourceCodeEnd":5014,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L4978-L5014","documentation":"The nested list passed to jnp.block must not contain an empty list at any level. _block recurses into sublists and raises this ValueError when it encounters one of length zero, because an empty block has no shape and cannot be concatenated.","triggerScenarios":"jnp.block([[], []]), jnp.block([[a], []]), or any programmatically built nested list where a filter/comprehension produced an empty inner list.","commonSituations":"Dynamically assembled blocks where a condition excludes all elements of one row/column (e.g. [[x for x in row if keep(x)] for row in rows] with an empty result), often under varying batch conditions.","solutions":["Guard block construction: skip or pad empty groups before calling jnp.block","Check for empty sublists: if any(len(sub) == 0 for sub in blocks): handle fallback","Build the array with jnp.concatenate/stack with explicit empty handling instead"],"exampleFix":"// before\nout = jnp.block([row for row in rows])  # some row may be []\n// after\nrows = [r for r in rows if len(r) > 0]\nout = jnp.block(rows) if rows else jnp.zeros((0,))","handlingStrategy":"validation","validationCode":"def has_no_empty(blocks):\n    return isinstance(blocks, list) and all(\n        len(b) > 0 and (not isinstance(b, list) or has_no_empty(b)) for b in blocks)\nassert has_no_empty(blocks)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Filter empty groups before assembling blocks","Add a sanity check len(rows) > 0 and all(len(r) > 0 for r in rows) in dynamic assembly code"],"tags":["jax","block","empty-list","valueerror"],"backgroundTag":"empty-argument-rejected","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}