{"record":{"id":"6f10adc922c4e450","repo":"jax-ml/jax","slug":"expected-sequence-object-with-len-0-or-a-single","errorCode":null,"errorMessage":"expected sequence object with len >= 0 or a single integer","messagePattern":"expected sequence object with len >= 0 or a single integer","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/array_creation.py","lineNumber":88,"sourceCode":"    Array of the specified shape and dtype, with the given device/sharding if specified.\n\n  See also:\n    - :func:`jax.numpy.zeros_like`\n    - :func:`jax.numpy.empty`\n    - :func:`jax.numpy.ones`\n    - :func:`jax.numpy.full`\n\n  Examples:\n    >>> jnp.zeros(4)\n    Array([0., 0., 0., 0.], dtype=float32)\n    >>> jnp.zeros((2, 3), dtype=bool)\n    Array([[False, False, False],\n           [False, False, False]], dtype=bool)\n\n  .. _explicit sharding: https://docs.jax.dev/en/latest/parallel.html\n  \"\"\"\n  if isinstance(shape, types.GeneratorType):\n    raise TypeError(\"expected sequence object with len >= 0 or a single integer\")\n  if (m := _check_forgot_shape_tuple(\"zeros\", shape, dtype)): raise TypeError(m)\n  dtype = dtypes.check_and_canonicalize_user_dtype(\n      float if dtype is None else dtype, \"zeros\")\n  shape = canonicalize_shape(shape)\n  sharding = util.choose_device_or_out_sharding(\n      device, out_sharding, 'jnp.zeros')\n  return lax.full(shape, 0, dtype, sharding=sharding)\n\n\n@export\ndef ones(shape: Any, dtype: DTypeLike | None = None, *,\n         device: xc.Device | Sharding | None = None,\n         out_sharding: NamedSharding | P | None = None) -> Array:\n  \"\"\"Create an array full of ones.\n\n  JAX implementation of :func:`numpy.ones`.\n\n  Args:","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/array_creation.py#L70-L106","documentation":"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.","triggerScenarios":"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]).","commonSituations":"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.","solutions":["Materialize the generator: jnp.zeros(tuple(gen)) or jnp.zeros([*gen])","Prefer explicit tuples: jnp.zeros((2, 3))"],"exampleFix":"# before\na = jnp.zeros(d for d in [2, 3])\n# after\na = jnp.zeros(tuple(d for d in [2, 3]))  # or simply (2, 3)","handlingStrategy":"validation","validationCode":"import types\ndef canonical(shape):\n    return tuple(shape) if isinstance(shape, types.GeneratorType) else shape","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Always pass explicit tuples for shapes","Wrap computed shapes in tuple() at call sites"],"tags":["jax","shape-validation","generator"],"backgroundTag":"invalid-shape-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}