jax-ml/jax · error · ValueError
attempt to get argmin of an empty sequence
Error message
attempt to get argmin of an empty sequence
What it means
Raised by jnp.argmin when the array has zero elements along the reduced axis; a minimum index over an empty sequence is undefined so JAX raises ValueError before dispatching.
Source
Thrown at jax/_src/numpy/lax_numpy.py:8347
Array([[0],
[2]], dtype=int32)
"""
arr = util.ensure_arraylike("argmin", a)
if out is not None:
raise NotImplementedError("The 'out' argument to jnp.argmin is not supported.")
return _argmin(arr, None if axis is None else operator.index(axis),
keepdims=bool(keepdims))
@api.jit(static_argnames=('axis', 'keepdims'), inline=True)
def _argmin(a: Array, axis: int | None = None, keepdims: bool = False) -> Array:
if axis is None:
dims = list(range(np.ndim(a)))
a = ravel(a)
axis = 0
else:
dims = [axis]
if a.shape[axis] == 0:
raise ValueError("attempt to get argmin of an empty sequence")
# TODO(phawkins): use an int64 index if the dimension is large enough.
result = lax.argmin(a, _canonicalize_axis(axis, a.ndim), int)
return expand_dims(result, dims) if keepdims else result
@export
def nanargmax(
a: ArrayLike,
axis: int | None = None,
out: None = None,
keepdims: bool | None = None,
) -> Array:
"""Return the index of the maximum value of an array, ignoring NaNs.
JAX implementation of :func:`numpy.nanargmax`.
Args:
a: input arrayView on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Check size before calling and handle empty explicitly (return a default index or skip)
- Ensure upstream filtering guarantees at least one element
- Pad with +inf sentinel and post-process the sentinel index
Example fix
// before best = jnp.argmin(losses) # losses may be empty // after best = jnp.argmin(losses) if losses.size else 0
Defensive patterns
Strategy: validation
Validate before calling
if a.size == 0: return 0 # or skip return int(jnp.argmin(a))
Type guard
def nonempty_for_argmin(a, axis=None):
a = jnp.asarray(a)
return a.size > 0 if axis is None else a.shape[axis] > 0 Try / catch
try:
idx = jnp.argmin(a)
except ValueError as e:
if 'empty sequence' in str(e):
idx = 0
else:
raise Prevention
- Check .size before argmin on masked data
- Guarantee filters keep >= 1 element
- Pad with +inf sentinel for data-dependent cases under jit
When it happens
Trigger: jnp.argmin(jnp.array([])); jnp.argmin(a, axis=k) with a.shape[k] == 0; empty result after boolean masking or slicing in jit-compiled code.
Common situations: Empty minibatches, all-elements-filtered arrays, boundary conditions in loops producing zero-length slices.
Related errors
- Invalid axis {axis} for operand shape {operand.shape}
- argmin and argmax require non-empty reduced dimension. opera
- index_dtype must be an integer type, but got {}
- index is out of bounds for axis {axis} with size 0
- Cannot do a non-empty jnp.take() from an empty axis.
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/a049a6198992ff59.
Report an issue: GitHub.