{"record":{"id":"45e5798ab741d55e","repo":"jax-ml/jax","slug":"the-array-passed-to-from-dlpack-must-have-dlpack","errorCode":null,"errorMessage":"The array passed to from_dlpack must have __dlpack__ and __dlpack_device__ methods.","messagePattern":"The array passed to from_dlpack must have __dlpack__ and __dlpack_device__ methods\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/dlpack.py","lineNumber":242,"sourceCode":"    A jax.Array\n\n  Note:\n    While JAX arrays are always immutable, dlpack buffers cannot be marked as\n    immutable, and it is possible for processes external to JAX to mutate them\n    in-place. If a jax Array is constructed from a dlpack buffer and the buffer\n    is later modified in-place, it may lead to undefined behavior when using\n    the associated JAX array.\n  \"\"\"\n  if isinstance(device, Sharding):\n    device_set = device.device_set\n    if len(device_set) > 1:\n      raise ValueError(\n        \"from_dlpack can only unpack a dlpack tensor onto a singular device, but \"\n        f\"a Sharding with {len(device_set)} devices was provided.\"\n      )\n    device, = device_set\n  if not hasattr(external_array, \"__dlpack__\") or not hasattr(external_array, \"__dlpack_device__\"):\n    raise TypeError(\n        \"The array passed to from_dlpack must have __dlpack__ and __dlpack_device__ methods.\"\n    )\n\n  dl_device_type, device_id = external_array.__dlpack_device__()\n  try:\n    dl_device_platform = _DL_DEVICE_TO_PLATFORM[dl_device_type]\n  except KeyError:\n    raise TypeError(\n        \"Array passed to from_dlpack is on unsupported device type \"\n        f\"(DLDeviceType: {dl_device_type}, array: {external_array}\"\n    ) from None\n\n  backend = xla_bridge.get_backend(dl_device_platform)\n  dlpack_device = backend.device_from_local_hardware_id(device_id)\n  _check_device(device, dlpack_device, copy)\n  if _is_tensorflow_tensor(external_array):\n    # TensorFlow does not support stream=.\n    stream = None","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/dlpack.py#L224-L260","documentation":"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.","triggerScenarios":"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).","commonSituations":"See trigger scenarios.","solutions":["Convert first: `jax.numpy.asarray(obj)` for array-likes","Verify hasattr(obj, '__dlpack__') before calling (upgrade numpy/torch if missing)","For lists/scalars, construct via jnp.array directly"],"exampleFix":"# before\njax.dlpack.from_dlpack([1, 2, 3])\n\n# after\nimport jax.numpy as jnp\njnp.array([1, 2, 3])","handlingStrategy":"type-guard","validationCode":"if not (hasattr(obj, '__dlpack__') and hasattr(obj, '__dlpack_device__')):\n    obj = jax.numpy.asarray(obj)","typeGuard":"def supports_dlpack(o) -> bool:\n    return hasattr(o, '__dlpack__') and hasattr(o, '__dlpack_device__')","tryCatchPattern":"try:\n    jax.dlpack.from_dlpack(obj)\nexcept TypeError:\n    import jax.numpy as jnp\n    arr = jnp.asarray(obj)","preventionTips":["Check the protocol attrs before interop","Prefer jnp.asarray for plain array-likes"],"tags":["jax","dlpack","type-check","buffer-protocol"],"backgroundTag":"unsupported-buffer-protocol","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}