{"record":{"id":"b1414d1180a5f5c6","repo":"jax-ml/jax","slug":"jnp-fromiter-is-not-implemented-because-it-may-b","errorCode":null,"errorMessage":"jnp.fromiter() is not implemented because it may be non-pure and thus unsafe for use with JIT and other JAX transformations. Consider using jnp.asarray(np.fromiter(...)) instead, although care should be taken if np.fromiter is used within a jax transformations because of its potential side-effect of consuming the iterable object; for more information see https://docs.jax.dev/en/latest/notebooks/Common_Gotchas_in_JAX.html#pure-functions","messagePattern":"jnp\\.fromiter\\(\\) is not implemented because it may be non-pure and thus unsafe for use with JIT and other JAX transformations\\. Consider using jnp\\.asarray\\(np\\.fromiter\\(\\.\\.\\.\\)\\) instead, although care should be taken if np\\.fromiter is used within a jax transformations because of its potential side-effect of consuming the iterable object; for more information see https://docs\\.jax\\.dev/en/latest/notebooks/Common_Gotchas_in_JAX\\.html#pure-functions","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":5551,"sourceCode":"    \"jnp.fromfile() is not implemented because it may be non-pure and thus unsafe for use \"\n    \"with JIT and other JAX transformations. Consider using jnp.asarray(np.fromfile(...)) \"\n    \"instead, although care should be taken if np.fromfile is used within a jax transformations \"\n    \"because of its potential side-effect of consuming the file object; for more information see \"\n    \"https://docs.jax.dev/en/latest/notebooks/Common_Gotchas_in_JAX.html#pure-functions\")\n\n\n@export\ndef fromiter(*args, **kwargs):\n  \"\"\"Unimplemented JAX wrapper for jnp.fromiter.\n\n  This function is left deliberately unimplemented because it may be non-pure and thus\n  unsafe for use with JIT and other JAX transformations. Consider using\n  ``jnp.asarray(np.fromiter(...))`` instead, although care should be taken if ``np.fromiter``\n  is used within jax transformations because of its potential side-effect of consuming the\n  iterable object; for more information see `Common Gotchas: Pure Functions\n  <https://docs.jax.dev/en/latest/notebooks/Common_Gotchas_in_JAX.html#pure-functions>`_.\n  \"\"\"\n  raise NotImplementedError(\n    \"jnp.fromiter() is not implemented because it may be non-pure and thus unsafe for use \"\n    \"with JIT and other JAX transformations. Consider using jnp.asarray(np.fromiter(...)) \"\n    \"instead, although care should be taken if np.fromiter is used within a jax transformations \"\n    \"because of its potential side-effect of consuming the iterable object; for more information see \"\n    \"https://docs.jax.dev/en/latest/notebooks/Common_Gotchas_in_JAX.html#pure-functions\")\n\n\n@export\ndef from_dlpack(x: Any, /, *, device: xc.Device | Sharding | None = None,\n                copy: bool | None = None) -> Array:\n  \"\"\"Construct a JAX array via DLPack.\n\n  JAX implementation of :func:`numpy.from_dlpack`.\n\n  Args:\n    x: An object that implements the DLPack_ protocol via the ``__dlpack__``\n      and ``__dlpack_device__`` methods, or a legacy DLPack tensor on either\n      CPU or GPU.","sourceCodeStart":5533,"sourceCodeEnd":5569,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L5533-L5569","documentation":"jnp.fromiter is unimplemented for the same purity reason as jnp.fromfile: consuming an iterator is a side effect incompatible with JIT and JAX transformations. JAX requires array construction to be deterministic and side-effect free, so the message points to np.fromiter plus jnp.asarray.","triggerScenarios":"Any call to jnp.fromiter(iterable, dtype) — unconditionally raises NotImplementedError. Typical when porting code that streams values (generators, database cursors) into an array.","commonSituations":"Generator-based data ingestion code ported from NumPy; building arrays lazily inside functions later wrapped in jax.jit.","solutions":["Use jnp.asarray(np.fromiter(it, dtype, count))","Or materialize the iterable first: jnp.asarray(list(it), dtype=dtype)","Do the conversion outside of any jitted computation"],"exampleFix":"// before\nx = jnp.fromiter(gen, dtype=np.float32)\n// after\nx = jnp.asarray(np.fromiter(gen, dtype=np.float32))","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Grep for jnp.fromiter when porting; replace with jnp.asarray(np.fromiter(...))","Materialize iterables to lists/arrays outside jit boundaries"],"tags":["jax","fromiter","notimplemented","io","jit-purity"],"backgroundTag":"unsupported-api-in-jax","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}