jax-ml/jax · error · ValueError

None is not a valid value for jnp.array

Error message

None is not a valid value for jnp.array

What it means

After flattening the input object's pytree structure, jnp.array found a None leaf. JAX treats nested lists/tuples as pytrees and requires every leaf to be a convertible scalar/array; None (Python null) has no numeric representation and unlike some NumPy paths is rejected outright.

Source

Thrown at jax/_src/numpy/array_constructors.py:285

      backend = xla_bridge.get_backend()
      if 'rocm' in backend.platform_version.lower():
        gpu_plugin_extension = rocm_plugin_extension
      elif 'cuda' in backend.platform_version.lower():
        gpu_plugin_extension = cuda_plugin_extension
      else:
        gpu_plugin_extension = None
      if gpu_plugin_extension is None:
        device_id = None
      else:
        device_id = gpu_plugin_extension.get_device_ordinal(cai["data"][0])
      object = _jax.cuda_array_interface_to_buffer(
          cai=cai, gpu_backend=backend, device_id=device_id)

  # To handle nested lists & tuples, flatten the tree and process each leaf.
  leaves, treedef = tree_util.tree_flatten(
      object, is_leaf=lambda x: not isinstance(x, (list, tuple)))
  if any(leaf is None for leaf in leaves):
    raise ValueError("None is not a valid value for jnp.array")
  leaves = [
      leaf
      if (leaf_jax_array := getattr(leaf, "__jax_array__", None)) is None
      else leaf_jax_array()
      for leaf in leaves
  ]
  if dtype is None:
    # Use lattice_result_type rather than result_type to avoid canonicalization.
    # Otherwise, weakly-typed inputs would have their dtypes canonicalized.
    try:
      dtype = (
          dtypes.lattice_result_type(*leaves)[0]
          if leaves
          else dtypes.default_float_dtype()
      )
    except TypeError:
      # This happens if, e.g. one of the entries is a memoryview object.
      # This is rare, so we only handle it if the normal path fails.

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Replace None with a numeric sentinel (np.nan) or filter missing entries before conversion
  2. Convert via NumPy with an explicit dtype after cleaning: np.array(x, dtype=float) after substituting np.nan
  3. Validate leaves with jax.tree_util before calling jnp.array in data pipelines

Example fix

# before
import jax.numpy as jnp
a = jnp.array([1.0, None, 3.0])
# after
import numpy as np, jax.numpy as jnp
a = jnp.array([1.0, np.nan, 3.0])
Defensive patterns

Strategy: validation

Validate before calling

from jax.tree_util import tree_flatten
leaves, _ = tree_flatten(obj, is_leaf=lambda x: not isinstance(x, (list, tuple)))
assert all(l is not None for l in leaves), 'None leaf found'

Type guard

def has_no_none_leaves(obj) -> bool:
    from jax.tree_util import tree_flatten
    leaves, _ = tree_flatten(obj, is_leaf=lambda x: not isinstance(x, (list, tuple)))
    return all(l is not None for l in leaves)

Try / catch

null

Prevention

When it happens

Trigger: jnp.array([1.0, None, 3.0]) or a nested structure containing None, e.g. ragged data with missing values encoded as None.

Common situations: Loading JSON/CSV data with missing fields into nested lists, or downstream of a preprocessing step that yields None for absent values.

Related errors


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