jax-ml/jax · error · ValueError

jnp.asarray: cannot convert object of type {type(a)} to JAX

Error message

jnp.asarray: cannot convert object of type {type(a)} to JAX Array on platform={_get_platform(device)} with copy=False. Consider using copy=None or copy=True instead.

What it means

With copy=False, jnp.asarray promises not to copy the input. Inputs supporting the buffer protocol (bytes, memoryview, NumPy arrays) are converted through NumPy on CPU, but if the target platform is not CPU the data must be transferred to device — impossible without a copy — so a ValueError is raised per the array API copy=False semantics.

Source

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

    >>> jnp.asarray(np.linspace(0, 2, 5))
    Array([0. , 0.5, 1. , 1.5, 2. ], dtype=float32)

    Constructing a JAX array via the Python buffer interface, using Python's
    built-in :mod:`array` module.

    >>> from array import array
    >>> pybuffer = array('i', [2, 3, 5, 7])
    >>> jnp.asarray(pybuffer)
    Array([2, 3, 5, 7], dtype=int32)
  """
  # For copy=False, the array API specifies that we raise a ValueError if the input supports
  # the buffer protocol but a copy is required. Since array() supports the buffer protocol
  # via numpy, this is only the case when the default device is not 'cpu'
  if (copy is False and not isinstance(a, Array)
      and _get_platform(device) != "cpu"
      and _supports_buffer_protocol(a)):
    raise ValueError(f"jnp.asarray: cannot convert object of type {type(a)} to JAX Array "
                     f"on platform={_get_platform(device)} with "
                     "copy=False. Consider using copy=None or copy=True instead.")
  if dtype is not None:
    dtype = dtypes.check_and_canonicalize_user_dtype(dtype, "asarray")
  return array(a, dtype=dtype, copy=bool(copy), order=order, device=device,
               out_sharding=out_sharding)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Use copy=None (let JAX decide) or omit copy — usual default
  2. Use copy=True to explicitly allow the device transfer
  3. Force CPU: jnp.asarray(x, copy=False, device='cpu') if a CPU array is actually acceptable

Example fix

# before
a = jnp.asarray(np_buf, copy=False)  # default device is GPU
# after
a = jnp.asarray(np_buf, copy=True)
Defensive patterns

Strategy: validation

Validate before calling

import jax.numpy as jnp
from jax._src.numpy.array_constructors import _get_platform  # or check jax.default_device
def safe_asarray(x, copy=False, device=None):
    if copy is False and device not in (None, 'cpu'):
        copy = None
    return jnp.asarray(x, copy=copy, device=device)

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: jnp.asarray(np_array_or_bytes, copy=False) while jax.default_device/jax_default_platform is a GPU or TPU (i.e. _get_platform(device) != 'cpu').

Common situations: Adopting array API copy=False zero-copy semantics in code that runs with a GPU default device; passing bytes buffers with copy=False.

Related errors


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