{"record":{"id":"32d7fd78f49e7437","repo":"jax-ml/jax","slug":"dtype-parameter-is-not-supported-by-buffer-array","errorCode":null,"errorMessage":"dtype parameter is not supported by Buffer.__array__.","messagePattern":"dtype parameter is not supported by Buffer\\.__array__\\.","errorType":"validation","errorClass":"nb::value_error","httpStatus":null,"severity":"error","filePath":"jaxlib/ffi.cc","lineNumber":443,"sourceCode":"\nnb::tuple PyFfiAnyBuffer::DLPackDevice() const {\n  return nb::make_tuple(static_cast<int32_t>(device_type_), device_ordinal_);\n}\n\nvoid RegisterFfiApis(nb::module_& m) {\n  nb::module_ ffi_module =\n      m.def_submodule(\"ffi\", \"Python bindings for the XLA FFI.\");\n\n  nb::class_<PyFfiAnyBuffer> buffer(ffi_module, \"Buffer\");\n  buffer.def_prop_ro(\"dtype\", xla::ValueOrThrowWrapper(&PyFfiAnyBuffer::dtype));\n  buffer.def_prop_ro(\"ndim\", &PyFfiAnyBuffer::ndim);\n  buffer.def_prop_ro(\"shape\", &PyFfiAnyBuffer::shape);\n  buffer.def_prop_ro(\"writeable\", &PyFfiAnyBuffer::writeable);\n  buffer.def(\n      \"__array__\",\n      [](PyFfiAnyBuffer self, nb::object dtype, nb::object copy) {\n        if (!dtype.is_none()) {\n          throw nb::value_error(\n              \"dtype parameter is not supported by Buffer.__array__.\");\n        }\n        if (!copy.is_none() && nb::cast<bool>(copy)) {\n          throw nb::value_error(\n              \"Buffer.__array__ with copy=True is not supported.\");\n        }\n        return xla::ValueOrThrow(self.NumpyArray());\n      },\n      nb::arg(\"dtype\") = nb::none(), nb::arg(\"copy\") = nb::none());\n  buffer.def_prop_ro(\n      \"__cuda_array_interface__\",\n      xla::ValueOrThrowWrapper(&PyFfiAnyBuffer::CudaArrayInterface));\n  buffer.def(\n      \"__dlpack__\",\n      [](PyFfiAnyBuffer self, nb::object stream, nb::object max_version,\n         nb::object dl_device, nb::object copy) {\n        if (!copy.is_none() && nb::cast<bool>(copy)) {\n          throw nb::value_error(","sourceCodeStart":425,"sourceCodeEnd":461,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jaxlib/ffi.cc#L425-L461","documentation":"The FFI Buffer type implements numpy's __array__ protocol but ignores the dtype parameter; passing a dtype would imply a cast that this zero-copy view cannot honor, so it is rejected.","triggerScenarios":"np.asarray(ffi_buffer, dtype=some_dtype) or calling buffer.__array__(dtype=...) on an xla.ffi Buffer object.","commonSituations":"Code doing np.asarray(x, dtype=np.float32) generically on any array-like, hitting an FFI buffer during custom-call/ffi kernel testing.","solutions":["Call __array__ without dtype and cast afterwards: arr.astype(np.float32)","Do the conversion inside the kernel on the raw data pointer instead"],"exampleFix":"# before\nnp.asarray(buf, dtype=np.float32)\n# after\nnp.asarray(buf).astype(np.float32)","handlingStrategy":"validation","validationCode":"arr = buf.__array__()  # never pass dtype","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Cast after conversion with ndarray.astype","Don't write dtype-generic np.asarray(x, dtype=...) helpers that hit FFI buffers"],"tags":["jax","ffi","numpy","dtype"],"backgroundTag":"unsupported-argument-value","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}