Registry / data / array-api-compat

array-api-compat

JSON →
library1.15.0pypypi✓ verified 24d ago

array-api-compat is a small wrapper around popular array libraries like NumPy, CuPy, PyTorch, Dask, JAX, ndonnx, and Sparse. Its primary goal is to make these libraries compatible with the Python Array API standard, allowing array-consuming libraries to write array-agnostic code today, even if the underlying array libraries do not yet fully implement the standard. The library is actively maintained by the Consortium for Python Data API Standards. The current version is 1.14.0, with ongoing development.

pip install array-api-compat
INSTALL
IMPORT
SIG · ARRAY-API-COMPAT
A
array-api-compat
datapythonv1.15.0
Install
1.6s avg
Import
41ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.15.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.042s · 18.5MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.040s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

array_namespace
from array_api_compat import array_namespace
The primary entry point to get an Array API compliant namespace from input arrays.
numpy_namespace
import array_api_compat.numpy as xp
import numpy as xp
Directly imports the Array API compatible wrapper for NumPy. The native NumPy namespace (especially pre-2.0) may not be fully compliant with the Array API standard.
torch_namespace
import array_api_compat.torch as xp
import torch as xp
Directly imports the Array API compatible wrapper for PyTorch. The native PyTorch namespace may not be fully compliant with the Array API standard.

This quickstart demonstrates the recommended usage of `array-api-compat` by obtaining an Array API compliant namespace using `array_namespace()`. This namespace (`xp`) can then be used with any supported array type (like NumPy, PyTorch, CuPy, etc.), enabling generic array-agnostic code.

import numpy as np from array_api_compat import array_namespace def process_arrays(x, y): xp = array_namespace(x, y) # Now use xp as the array API compliant namespace result = xp.mean(x, axis=0) + 2 * xp.std(y, axis=0) return result # Example usage with NumPy arrays a = np.array([1.0, 2.0, 3.0]) b = np.array([4.0, 5.0, 6.0]) output = process_arrays(a, b) print(output) # Expected output type is the same as input (e.g., numpy.ndarray) print(type(output))
Debug
Known issues
breakingSupport for Python 3.8 was dropped in version 1.15.0.dev0 (March 29, 2024). Users on Python 3.8 will need to upgrade their Python version to use recent `array-api-compat` releases.
fix
Upgrade to Python 3.9 or newer.
affects: >=1.15.0.dev0
gotcha`array-api-compat` is a wrapper and does not monkey-patch or modify the underlying array objects. This means array object *methods* or *operators* that behave differently from the Array API standard might remain non-compliant. Users should prefer elementwise functions (e.g., `xp.add(x, y)` instead of `x + y`) and provided helper functions (e.g., `array_api_compat.size(x)` instead of `x.size` for PyTorch) for consistent behavior.
fix
Rely on functions from the `xp` (array_api_compat) namespace rather than direct array object methods/operators for Array API compliant behavior. Use helper functions like `array_api_compat.size()` where native library methods are incompatible (e.g., `torch.Tensor.size`).
affects: all
gotchaThe behavior of `array_api_compat` with NumPy 2.0 has changed. Initially, NumPy 2.0 was unwrapped due to its inherent Array API support, but later versions of `array-api-compat` re-wrapped it for 2023.12 standard compatibility. This could lead to subtle behavioral differences or regressions depending on the specific versions of `array-api-compat` and NumPy in use.
fix
Consult the `array-api-compat` changelog and test thoroughly when upgrading NumPy 2.x or `array-api-compat` versions to ensure expected behavior.
affects: >=1.15.0.dev0
gotcha`array-api-compat` explicitly avoids implementing functions that are not part of the Array API standard. This design choice keeps the library minimal and prevents it from inadvertently becoming a de facto standard. Consequently, if a function is commonly available in a specific array library but not in the Array API standard, it will not be exposed via `array-api-compat`.
fix
Be aware that the `xp` namespace will only contain functions specified by the Array API standard. For non-standard functions, you may need to use library-specific imports or `array-api-extra`.
affects: all
gotchaDask support within `array-api-compat` has known limitations. Specifically, `sort`, `argsort`, and full support for the `linalg` and `fft` extensions may be missing or non-compliant with the Array API standard. Functions within `dask.array.fft` are typically the original, unwrapped Dask functions.
fix
Exercise caution when using sorting, linear algebra, or FFT operations with Dask arrays via `array-api-compat`. Verify behavior against Dask's native implementation if unexpected results occur.
affects: all
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'array_api_compat'
The 'array-api-compat' package is not installed in the Python environment.
fix
Install the package using pip: 'pip install array-api-compat'.
ImportError: cannot import name 'array_namespace' from 'array_api_compat'
The 'array_namespace' function is not available in the 'array_api_compat' module, possibly due to an outdated version.
fix
Ensure you have the latest version of 'array-api-compat' installed: 'pip install --upgrade array-api-compat'.
AttributeError: module 'array_api_compat' has no attribute 'numpy'
Attempting to access a submodule that does not exist in 'array_api_compat'.
fix
Use the correct import statement: 'import array_api_compat.numpy as np'.
TypeError: array_namespace() missing 1 required positional argument: 'x'
The 'array_namespace' function was called without the necessary arguments.
fix
Pass at least one array object to 'array_namespace()' to determine the appropriate namespace.
ValueError: Unsupported array type passed to array_namespace()
An unsupported array type was provided to 'array_namespace()'.
fix
Ensure that the array passed is from a supported library like NumPy, CuPy, PyTorch, Dask, JAX, ndonnx, or Sparse.
Upgrade
Version history
1.15.0latest on PyPI · released Jun 7, 2026
Audit
Dependencies
pythonrequiredRequires Python 3.10 or newer.
numpyoptionalProvides Array API compatible namespace for NumPy arrays. Automatically imported if a NumPy array is passed to array_namespace.
cupyoptionalProvides Array API compatible namespace for CuPy arrays. Automatically imported if a CuPy array is passed to array_namespace.
torchoptionalProvides Array API compatible namespace for PyTorch tensors. Automatically imported if a PyTorch tensor is passed to array_namespace.
daskoptionalProvides Array API compatible namespace for Dask arrays. Automatically imported if a Dask array is passed to array_namespace.
jaxoptionalSupports JAX arrays via helper functions; JAX itself implements Array API support in jax.numpy (v0.4.32+).
sparseoptionalSupports PyData/Sparse arrays via helper functions; Sparse itself implements Array API support.
ndonnxoptionalSupports ndonnx arrays via helper functions; ndonnx itself implements Array API support.
Agent activity
32 hits · last 30 days
node
26
OpenAI (training)
1
Resources