jax-ml/jax · error · TypeError

The array passed to from_dlpack must have __dlpack__ and __d

Error message

The array passed to from_dlpack must have __dlpack__ and __dlpack_device__ methods.

What it means

from_dlpack requires an object implementing the DLPack producer protocol (__dlpack__ and __dlpack_device__). Passing anything else (plain lists, legacy objects, objects whose protocol methods were removed) raises TypeError.

Source

Thrown at jax/_src/dlpack.py:242

    A jax.Array

  Note:
    While JAX arrays are always immutable, dlpack buffers cannot be marked as
    immutable, and it is possible for processes external to JAX to mutate them
    in-place. If a jax Array is constructed from a dlpack buffer and the buffer
    is later modified in-place, it may lead to undefined behavior when using
    the associated JAX array.
  """
  if isinstance(device, Sharding):
    device_set = device.device_set
    if len(device_set) > 1:
      raise ValueError(
        "from_dlpack can only unpack a dlpack tensor onto a singular device, but "
        f"a Sharding with {len(device_set)} devices was provided."
      )
    device, = device_set
  if not hasattr(external_array, "__dlpack__") or not hasattr(external_array, "__dlpack_device__"):
    raise TypeError(
        "The array passed to from_dlpack must have __dlpack__ and __dlpack_device__ methods."
    )

  dl_device_type, device_id = external_array.__dlpack_device__()
  try:
    dl_device_platform = _DL_DEVICE_TO_PLATFORM[dl_device_type]
  except KeyError:
    raise TypeError(
        "Array passed to from_dlpack is on unsupported device type "
        f"(DLDeviceType: {dl_device_type}, array: {external_array}"
    ) from None

  backend = xla_bridge.get_backend(dl_device_platform)
  dlpack_device = backend.device_from_local_hardware_id(device_id)
  _check_device(device, dlpack_device, copy)
  if _is_tensorflow_tensor(external_array):
    # TensorFlow does not support stream=.
    stream = None

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Convert first: `jax.numpy.asarray(obj)` for array-likes
  2. Verify hasattr(obj, '__dlpack__') before calling (upgrade numpy/torch if missing)
  3. For lists/scalars, construct via jnp.array directly

Example fix

# before
jax.dlpack.from_dlpack([1, 2, 3])

# after
import jax.numpy as jnp
jnp.array([1, 2, 3])
Defensive patterns

Strategy: type-guard

Validate before calling

if not (hasattr(obj, '__dlpack__') and hasattr(obj, '__dlpack_device__')):
    obj = jax.numpy.asarray(obj)

Type guard

def supports_dlpack(o) -> bool:
    return hasattr(o, '__dlpack__') and hasattr(o, '__dlpack_device__')

Try / catch

try:
    jax.dlpack.from_dlpack(obj)
except TypeError:
    import jax.numpy as jnp
    arr = jnp.asarray(obj)

Prevention

When it happens

Trigger: jax.dlpack.from_dlpack(obj) where obj lacks __dlpack__/__dlpack_device__ (e.g. a numpy array on old numpy, a Python list, or a tensor library without DLPack support).

Common situations: See trigger scenarios.

Related errors


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