shap and NumPy 2 compatibility (tested)

Under NumPy 2, shap 0.45.1 and older print _ARRAY_API not found and C extension was not built during install! at import, then TreeExplainer fails with ImportError: numpy.core.multiarray failed to import. shap 0.46.0 is the first release that supports NumPy 2. Tested below.

1. Reproduce: a plain pip install of shap 0.45.1

A fresh Python 3.11 venv and pip install shap==0.45.1 scikit-learn. shap's metadata says only numpy (no version cap, in every release), so the resolver picks NumPy 2.4.6 (plus numba 0.68.0, scikit-learn 1.9.1, pandas 3.1.0). b1.py:

import numpy
print("numpy", numpy.__version__)
import shap
print("shap", shap.__version__)
from sklearn.tree import DecisionTreeRegressor
X = numpy.arange(40.0).reshape(20, 2)
y = X[:, 0] * 2
model = DecisionTreeRegressor(max_depth=2, random_state=0).fit(X, y)
sv = shap.TreeExplainer(model).shap_values(X[:2])
print(sv.shape)

Output, exit code 1 (paths shortened to /venv):

numpy 2.4.6

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.4.6 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

Traceback (most recent call last):  File "b1.py", line 3, in <module>
    import shap
  File "/venv/lib/python3.11/site-packages/shap/__init__.py", line 4, in <module>
    from .explainers import other
  File "/venv/lib/python3.11/site-packages/shap/explainers/__init__.py", line 4, in <module>
    from ._gpu_tree import GPUTreeExplainer
  File "/venv/lib/python3.11/site-packages/shap/explainers/_gpu_tree.py", line 5, in <module>
    from ._tree import (
  File "/venv/lib/python3.11/site-packages/shap/explainers/_tree.py", line 29, in <module>
    from .. import _cext
AttributeError: _ARRAY_API not found
shap 0.45.1
C extension was not built during install!
Traceback (most recent call last):
  File "b1.py", line 9, in <module>
    sv = shap.TreeExplainer(model).shap_values(X[:2])
         ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.11/site-packages/shap/explainers/_tree.py", line 195, in __init__
    self.model = TreeEnsemble(model, self.data, self.data_missing, model_output)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.11/site-packages/shap/explainers/_tree.py", line 823, in __init__
    self.trees = [SingleTree(model.tree_, data=data, data_missing=data_missing)]
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.11/site-packages/shap/explainers/_tree.py", line 1468, in __init__
    assert_import("cext")
  File "/venv/lib/python3.11/site-packages/shap/utils/_general.py", line 19, in assert_import
    raise e
  File "/venv/lib/python3.11/site-packages/shap/explainers/_tree.py", line 29, in <module>
    from .. import _cext
ImportError: numpy.core.multiarray failed to import

Two things are misleading here. import shap itself succeeds: shap catches the failed import of its compiled _cext module, prints the NumPy banner and _ARRAY_API not found, then prints C extension was not built during install!. The extension was built, but against NumPy 1.x. The real error, ImportError: numpy.core.multiarray failed to import, only surfaces when you call TreeExplainer. shap 0.44.1 and 0.45.0 fail the same way.

2. Which shap version supports NumPy 2

shap 0.46.0 is the first release that works with NumPy 2. Same script, same fresh-venv install:

$ pip install "shap==0.46.0" scikit-learn
$ python b1.py
numpy 2.4.6
shap 0.46.0
(2, 2)

The newest shap (0.54.1) requires Python 3.12+ and numpy>=2; on Python 3.11 an upgrade stops at 0.51.0.

3. Fix: upgrade shap, or downgrade NumPy (and pandas)

$ pip install -U shap numpy
$ python b1.py
numpy 2.4.6
shap 0.51.0
(2, 2)

To keep an old shap, pin NumPy 1. Pin pandas too: pinning only numpy<2 left pandas 3.1.0 installed, and import shap then failed with ImportError: Please upgrade numpy to >= 2.0.2 to use this pandas version.

$ pip install "numpy<2" "pandas<3"
$ python b1.py
numpy 1.26.4
shap 0.45.1
(2, 2)

Restart any Jupyter kernel after changing versions. shap needs numba, which has its own NumPy ceiling: Numba needs NumPy 2.2 or less. Same NumPy 2 problem elsewhere: scikit-learn, statsmodels, the general banner. All changes: NumPy 2 breaking changes.

Frequently asked questions

Which shap version supports NumPy 2?

shap 0.46.0. 0.44.1, 0.45.0 and 0.45.1 failed with numpy 2.4.6 in this test; shap never caps numpy in its metadata, so pip installs NumPy 2 next to old shap.

Why does shap say C extension was not built during install?

On NumPy 2 with shap 0.45 or older, the extension is built but against NumPy 1.x and fails to import. Upgrade shap, or pin numpy<2 and pandas<3.

Written with AI assistance. Measured on 2026-10-24 on Linux with Python 3.11.17: shap 0.44.1, 0.45.0, 0.45.1, 0.46.0 and 0.51.0 with numpy 2.4.6, and shap 0.45.1 with numpy 1.26.4 + pandas 2.3.3, using scikit-learn 1.9.1. The script ran verbatim and output was pasted unedited (paths shortened). Version pins read from PyPI metadata the same day. PlainTables is our extension; shap, NumPy and scikit-learn are free.