jax-ml/jax · error · TypeError
expected sequence object with len >= 0 or a single integer
Error message
expected sequence object with len >= 0 or a single integer
What it means
jnp.zeros rejects generator objects for shape. Generators are single-use and have no reliable length, so they cannot describe a static array shape under JAX's shape canonicalization; NumPy's more permissive behavior is deliberately not mirrored.
Source
Thrown at jax/_src/numpy/array_creation.py:88
Array of the specified shape and dtype, with the given device/sharding if specified.
See also:
- :func:`jax.numpy.zeros_like`
- :func:`jax.numpy.empty`
- :func:`jax.numpy.ones`
- :func:`jax.numpy.full`
Examples:
>>> jnp.zeros(4)
Array([0., 0., 0., 0.], dtype=float32)
>>> jnp.zeros((2, 3), dtype=bool)
Array([[False, False, False],
[False, False, False]], dtype=bool)
.. _explicit sharding: https://docs.jax.dev/en/latest/parallel.html
"""
if isinstance(shape, types.GeneratorType):
raise TypeError("expected sequence object with len >= 0 or a single integer")
if (m := _check_forgot_shape_tuple("zeros", shape, dtype)): raise TypeError(m)
dtype = dtypes.check_and_canonicalize_user_dtype(
float if dtype is None else dtype, "zeros")
shape = canonicalize_shape(shape)
sharding = util.choose_device_or_out_sharding(
device, out_sharding, 'jnp.zeros')
return lax.full(shape, 0, dtype, sharding=sharding)
@export
def ones(shape: Any, dtype: DTypeLike | None = None, *,
device: xc.Device | Sharding | None = None,
out_sharding: NamedSharding | P | None = None) -> Array:
"""Create an array full of ones.
JAX implementation of :func:`numpy.ones`.
Args:View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Materialize the generator: jnp.zeros(tuple(gen)) or jnp.zeros([*gen])
- Prefer explicit tuples: jnp.zeros((2, 3))
Example fix
# before a = jnp.zeros(d for d in [2, 3]) # after a = jnp.zeros(tuple(d for d in [2, 3])) # or simply (2, 3)
Defensive patterns
Strategy: validation
Validate before calling
import types
def canonical(shape):
return tuple(shape) if isinstance(shape, types.GeneratorType) else shape Type guard
null
Try / catch
null
Prevention
- Always pass explicit tuples for shapes
- Wrap computed shapes in tuple() at call sites
When it happens
Trigger: jnp.zeros((n) for n in dims) or jnp.zeros(range(3)) — wait, range passes; specifically passing a types.GeneratorType like jnp.zeros(x for x in [2,3]).
Common situations: Refactoring NumPy code that computed shapes lazily; passing a generator expression where a tuple was intended, often due to a trailing comma typo or comprehension misuse.
Related errors
- scan got `length` argument of {} which disagrees with leadin
- conv_general_dilated batch_group_count must divide lhs batch
- conv_general_dilated rhs output feature dimension size must
- conv_general_dilated window and window_strides must have the
- Wrong number of explicit pads for convolution: expected {},
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/6f10adc922c4e450.
Report an issue: GitHub.