jax-ml/jax · error · ValueError
period must be a scalar; got {period}
Error message
period must be a scalar; got {period} What it means
jnp.interp's period argument (for periodic interpolation) must be a scalar (0-dimensional). Passing an array or any non-scalar with ndim != 0 is rejected because periodic wrapping requires a single period value.
Source
Thrown at jax/_src/numpy/lax_numpy.py:2626
raise ValueError("the only valid string value of `left` is "
f"'extrapolate', but got: {left!r}")
extrapolate_left = True
else:
extrapolate_left = False
if isinstance(right, str):
if right != 'extrapolate':
raise ValueError("the only valid string value of `right` is "
f"'extrapolate', but got: {right!r}")
extrapolate_right = True
else:
extrapolate_right = False
if dtypes.issubdtype(x_arr.dtype, np.complexfloating):
raise ValueError("jnp.interp: complex x values not supported.")
if period is not None:
if np.ndim(period) != 0:
raise ValueError(f"period must be a scalar; got {period}")
period = ufuncs.abs(period)
x_arr = x_arr % period
xp_arr = xp_arr % period
xp_arr, fp_arr = lax.sort_key_val(xp_arr, fp_arr)
xp_arr = concatenate([xp_arr[-1:] - period, xp_arr, xp_arr[:1] + period])
fp_arr = concatenate([fp_arr[-1:], fp_arr, fp_arr[:1]])
i = clip(searchsorted(xp_arr, x_arr, side='right'), 1, len(xp_arr) - 1)
df = fp_arr[i] - fp_arr[i - 1]
dx = xp_arr[i] - xp_arr[i - 1]
delta = x_arr - xp_arr[i - 1]
epsilon = np.spacing(np.finfo(xp_arr.dtype).eps)
dx0 = lax.abs(dx) <= epsilon # Prevent NaN gradients when `dx` is small.
f = where(dx0, fp_arr[i - 1], fp_arr[i - 1] + (delta / where(dx0, 1, dx)) * df)
if not extrapolate_left:
assert not isinstance(left, str)View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Pass a Python float or 0-d value: period=2*np.pi
- If period comes from an array, index it: period=float(period[0]) or use period.item()
- Verify np.ndim(period) == 0 before the call
Example fix
// before jnp.interp(x, xp, fp, period=np.array([6.283185])) // after jnp.interp(x, xp, fp, period=float(np.array([6.283185]).item()))
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
if np.ndim(period) != 0:
period = float(np.asarray(period).reshape(-1)[0]) Type guard
def is_scalar_period(p) -> bool:
return p is None or np.ndim(p) == 0 Prevention
- Always pass period as a Python float
- Assert np.ndim(period) == 0 in tests
When it happens
Trigger: Calling jnp.interp(x, xp, fp, period=jnp.array([2*np.pi])) or period=np.pi*np.ones(3); a 1-element list also triggers it.
Common situations: Wrapping a period in brackets accidentally, reusing a per-point periods array from custom interpolation code, or loading a period from config as a 1-element array.
Related errors
- scan got `length` argument of {} which disagrees with leadin
- conv_general_dilated batch_group_count must divide lhs batch
- conv_general_dilated rhs output feature dimension size must
- conv_general_dilated window and window_strides must have the
- Wrong number of explicit pads for convolution: expected {},
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/5a0e20fec6309a4b.
Report an issue: GitHub.