[Compatibility Issue] np.trapz removed in NumPy 2.4.0 — use np.trapezoid instead
Author: xyf5432Created Aug 7, 2026Updated Aug 7, 2026
Affected Files
ivy/functional/backends/numpy/elementwise.py:749ivy/functional/backends/numpy/experimental/statistical.py:567
Current Code
# ivy/functional/backends/numpy/elementwise.py:749
def trapz(y, /, *, x=None, dx=1.0, axis=-1, out=None):
return np.trapz(y, x=x, dx=dx, axis=axis)
# ivy/functional/backends/numpy/experimental/statistical.py:567
integral = np.trapz(y, t)Root Cause
numpy.trapz was deprecated in NumPy 2.0 (2023-08-18) and fully removed in NumPy 2.4.0 (2025-12-20). In NumPy >= 2.4.0, any call to np.trapz() raises:
AttributeError: module 'numpy' has no attribute 'trapz'Ivy defines its own trapz API across multiple backends (torch, jax, tensorflow, numpy). The numpy backend directly delegates to np.trapz, which will break.
Impact
- Severity: High — hard crash at runtime on NumPy >= 2.4.0
- The numpy backend
trapz()is called viaivy.trapz()→ backend dispatch →np.trapz() - Also affects the numpy frontend:
ivy.functional.frontends.numpy.trapz()→ivy.trapz()→ numpy backend →np.trapz() - The experimental
igamma_calfunction in statistical.py is also affected
Solution
# ivy/functional/backends/numpy/elementwise.py:749
# Before:
return np.trapz(y, x=x, dx=dx, axis=axis)
# After:
return np.trapezoid(y, x=x, dx=dx, axis=axis)
# ivy/functional/backends/numpy/experimental/statistical.py:567
# Before:
integral = np.trapz(y, t)
# After:
integral = np.trapezoid(y, t)trapezoid has the same signature: trapezoid(y, x=None, dx=1.0, axis=-1).
For NumPy < 2.0 backward compatibility:
try:
from numpy import trapezoid
except ImportError:
from numpy import trapz as trapezoidReferences
- NumPy 2.4.0 release notes — trapz removed
- NumPy 2.0 deprecation: https://numpy.org/doc/stable/reference/generated/numpy.trapz.html
- Similar fixes in other downstream projects: gpytorch#2695, NeuroKit#1155
Source: unifyai/ivy