{"record":{"id":"14800e2c877e5a69","repo":"jax-ml/jax","slug":"jnp-asarray-cannot-convert-object-of-type-type-a","errorCode":null,"errorMessage":"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.","messagePattern":"jnp\\.asarray: cannot convert object of type (.+?) to JAX Array on platform=(.+?) with copy=False\\. Consider using copy=None or copy=True instead\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/array_constructors.py","lineNumber":451,"sourceCode":"\n    >>> jnp.asarray(np.linspace(0, 2, 5))\n    Array([0. , 0.5, 1. , 1.5, 2. ], dtype=float32)\n\n    Constructing a JAX array via the Python buffer interface, using Python's\n    built-in :mod:`array` module.\n\n    >>> from array import array\n    >>> pybuffer = array('i', [2, 3, 5, 7])\n    >>> jnp.asarray(pybuffer)\n    Array([2, 3, 5, 7], dtype=int32)\n  \"\"\"\n  # For copy=False, the array API specifies that we raise a ValueError if the input supports\n  # the buffer protocol but a copy is required. Since array() supports the buffer protocol\n  # via numpy, this is only the case when the default device is not 'cpu'\n  if (copy is False and not isinstance(a, Array)\n      and _get_platform(device) != \"cpu\"\n      and _supports_buffer_protocol(a)):\n    raise ValueError(f\"jnp.asarray: cannot convert object of type {type(a)} to JAX Array \"\n                     f\"on platform={_get_platform(device)} with \"\n                     \"copy=False. Consider using copy=None or copy=True instead.\")\n  if dtype is not None:\n    dtype = dtypes.check_and_canonicalize_user_dtype(dtype, \"asarray\")\n  return array(a, dtype=dtype, copy=bool(copy), order=order, device=device,\n               out_sharding=out_sharding)\n","sourceCodeStart":433,"sourceCodeEnd":458,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/array_constructors.py#L433-L458","documentation":"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.","triggerScenarios":"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').","commonSituations":"Adopting array API copy=False zero-copy semantics in code that runs with a GPU default device; passing bytes buffers with copy=False.","solutions":["Use copy=None (let JAX decide) or omit copy — usual default","Use copy=True to explicitly allow the device transfer","Force CPU: jnp.asarray(x, copy=False, device='cpu') if a CPU array is actually acceptable"],"exampleFix":"# before\na = jnp.asarray(np_buf, copy=False)  # default device is GPU\n# after\na = jnp.asarray(np_buf, copy=True)","handlingStrategy":"validation","validationCode":"import jax.numpy as jnp\nfrom jax._src.numpy.array_constructors import _get_platform  # or check jax.default_device\ndef safe_asarray(x, copy=False, device=None):\n    if copy is False and device not in (None, 'cpu'):\n        copy = None\n    return jnp.asarray(x, copy=copy, device=device)","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Use copy=None (default) unless zero-copy on CPU is required","Remember copy=False + non-CPU default device always fails"],"tags":["jax","copy-semantics","device-placement","array-api"],"backgroundTag":"zero-copy-not-possible","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}