keras-team/keras · error · ValueError
Argument `constant_values` can only be provided when `mode…
Error message
Argument `constant_values` can only be provided when `mode == 'constant'`. Received: mode={mode} What it means
Guard inside the NumPy backend's `pad`: NumPy's `constant_values` fill argument only applies when padding with mode 'constant'. If a user supplies `constant_values` together with any other mode ('reflect', 'edge', 'minimum', etc.), the combination is rejected rather than silently ignoring the fill value. Referenced by the custom-train-step JAX guide's model code paths, so it commonly surfaces when padding is configured from generic helper code that always sets a fill value.
Solutions
- Drop the `constant_values` argument when using a non-'constant' pad mode
- Keep mode='constant' if a specific fill value is required
- Only pass constant_values conditionally: kwargs['constant_values'] = v if mode == 'constant' else None
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/numpy/numpy.py:1270 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/fca270aa43064a1e.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/numpy/numpy.py:1270
def ones_like(x, dtype=None):
return np.ones_like(x, dtype=dtype)
def outer(x1, x2):
x1 = convert_to_tensor(x1)
x2 = convert_to_tensor(x2)
dtype = dtypes.result_type(x1.dtype, x2.dtype)
x1 = x1.astype(dtype)
x2 = x2.astype(dtype)
return np.outer(x1, x2)
def pad(x, pad_width, mode="constant", constant_values=None):
kwargs = {}
if constant_values is not None:
if mode != "constant":
raise ValueError(
"Argument `constant_values` can only be "
"provided when `mode == 'constant'`. "
f"Received: mode={mode}"
)
kwargs["constant_values"] = constant_values
return np.pad(x, pad_width, mode=mode, **kwargs)
def percentile(x, q, axis=None, method="linear", keepdims=False):
x = convert_to_tensor(x)
q = convert_to_tensor(q)
ori_dtype = standardize_dtype(x.dtype)
if ori_dtype == "bool":
x = x.astype(config.floatx())
dtype = dtypes.result_type(x.dtype, float)
return np.percentile(
x, q, axis=axis, method=method, keepdims=keepdims
).astype(dtype)View on GitHub (pinned to 7a34a03db6)