jax-ml/jax · error · ValueError

condlist must be non-empty

Error message

condlist must be non-empty

What it means

jnp.select requires at least one condition/choice pair; empty condlist (and choicelist) is rejected because there would be nothing to select.

Source

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

    This is logically equivalent to the following nested ``where`` statement:

    >>> default = 0
    >>> jnp.where(condlist[0],
    ...   choicelist[0],
    ...   jnp.where(condlist[1],
    ...     choicelist[1],
    ...     jnp.where(condlist[2],
    ...       choicelist[2],
    ...       default)))
    Array([ 10,   2, 300,   0], dtype=int32)

    However, for efficiency it is implemented in terms of :func:`jax.lax.select_n`.
  """
  if len(condlist) != len(choicelist):
    msg = "condlist must have length equal to choicelist ({} vs {})"
    raise ValueError(msg.format(len(condlist), len(choicelist)))
  if len(condlist) == 0:
    raise ValueError("condlist must be non-empty")

  util.check_arraylike("select", *condlist, *choicelist, default)
  condlist = [asarray(cond) for cond in condlist]
  choicelist = [asarray(choice) for choice in choicelist]
  default = asarray(default)

  # Put the default at front with condition False because
  # argmax returns zero for an array of False values.
  choicelist = util.promote_dtypes(default, *choicelist)
  conditions = stack(broadcast_arrays(False, *condlist))
  idx = argmax(conditions.astype(bool), axis=0)
  return lax.select_n(*broadcast_arrays(idx, *choicelist))

def is_replicated_or_unreduced(sharding: NamedSharding) -> bool:
  if sharding.spec.partitions:
    return False
  if sharding.spec.reduced:
    return False

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Guard with a fallback before calling: if not conds: return default array
  2. Ensure at least one condition exists, e.g. add a final True catch-all: jnp.select([True], [default])

Example fix

// before
result = jnp.select(conds, choices, default=0)  # conds == []
// after
result = jnp.select(conds + [True], choices + [0], default=0) if conds else jnp.zeros_like(x)
Defensive patterns

Strategy: fallback

Validate before calling

if not condlist:
    result = jnp.full_like(x, default)
else:
    result = jnp.select(condlist, choicelist, default=default)

Prevention

When it happens

Trigger: Calling jnp.select([], []) — often when condlist was dynamically built from a filter that matched nothing.

Common situations: Data-driven branch lists where all cases are filtered out at runtime; empty category lists in preprocessing pipelines.

Related errors


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