{"record":{"id":"4dc9e9c6b3cfe453","repo":"jax-ml/jax","slug":"argument-to-to-dlpack-must-be-a-jax-array-got-ty","errorCode":null,"errorMessage":"Argument to to_dlpack must be a jax.Array, got {type(x)}","messagePattern":"Argument to to_dlpack must be a jax\\.Array, got (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/dlpack.py","lineNumber":132,"sourceCode":"    copy: a boolean indicating whether or not to copy the input. If\n      ``copy=True`` then the function must always copy. When\n      ``copy=False`` then the function must never copy, and must raise an error\n      when a copy is deemed necessary. If ``copy=None`` then the function must\n      avoid a copy if possible but may copy if needed.\n\n  Returns:\n    A DLPack PyCapsule object.\n\n  Note:\n    While JAX arrays are always immutable, ``DLPackManagedTensor`` buffers\n    cannot be marked as immutable, and it is possible for processes external\n    to JAX to mutate them in-place. If a DLPack buffer derived from a JAX array\n    is mutated, it may lead to undefined behavior when using the associated JAX\n    array. When JAX eventually supports ``DLManagedTensorVersioned``\n    (DLPack 1.0), it will be possible to specify that a buffer is read-only.\n  \"\"\"\n  if not isinstance(x, array.ArrayImpl):\n    raise TypeError(\"Argument to to_dlpack must be a jax.Array, \"\n                    f\"got {type(x)}\")\n\n  device = None\n  dl_device_type, local_hardware_id = dl_device if dl_device else (None, None)\n  if dl_device_type:\n    try:\n      dl_device_platform = _DL_DEVICE_TO_PLATFORM[dl_device_type]\n      backend = xla_bridge.get_backend(dl_device_platform)\n      device = backend.device_from_local_hardware_id(local_hardware_id)\n    except KeyError:\n      # https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.__dlpack__.html\n      # recommends using BufferError.\n      raise BufferError(\n          \"The device specification passed to to_dlpack contains an\"\n          f\" unsupported device type (DLDeviceType: {dl_device_type})\"\n      ) from None\n\n  # As new versions are adopted over time, we can maintain some legacy paths","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/dlpack.py#L114-L150","documentation":"jax.dlpack.to_dlpack only accepts concrete jax.Array (ArrayImpl) instances; anything else (tracers, numpy arrays, torch tensors, Python scalars) is rejected with a TypeError.","triggerScenarios":"Passing a NumPy array, torch.Tensor, Python scalar, or a JAX tracer (inside jit) to jax.dlpack.to_dlpack().","commonSituations":"Interop helpers that receive 'array-like' inputs; passing a jitted function's internal tracer; passing numpy arrays after conversion assumptions break.","solutions":["Convert to a JAX array first: `x = jax.numpy.asarray(x)`","Check `isinstance(x, jax.Array)` before calling to_dlpack","For numpy inputs, use torch/other dlpack producers or convert via device_put"],"exampleFix":"# before\njax.dlpack.to_dlpack(np_array)\n\n# after\njax.dlpack.to_dlpack(jax.numpy.asarray(np_array))","handlingStrategy":"type-guard","validationCode":"import jax\nif not isinstance(x, jax.Array):\n    x = jax.numpy.asarray(x)","typeGuard":"import jax\ndef is_jax_array(x) -> bool:\n    return isinstance(x, jax.Array)","tryCatchPattern":"try:\n    jax.dlpack.to_dlpack(x)\nexcept TypeError:\n    jax.dlpack.to_dlpack(jax.numpy.asarray(x))","preventionTips":["Normalize inputs with jnp.asarray at API boundaries","Never call to_dlpack inside jit on tracers"],"tags":["jax","dlpack","type-check","typeguard"],"backgroundTag":"wrong-argument-type","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}