jax-ml/jax · error · ValueError
cov: dtype must be a subclass of float or complex; got {dtyp
Error message
cov: dtype must be a subclass of float or complex; got {dtype=} What it means
jnp.cov accepts an optional dtype parameter to control the result dtype, but it must be an inexact (float or complex) dtype since covariance is computed via floating-point averaging. If dtype is provided and not a subclass of np.inexact, a ValueError with the offending dtype is raised.
Source
Thrown at jax/_src/numpy/lax_numpy.py:9188
>>> x = jax.random.normal(key, shape=(3, 100))
>>> with jnp.printoptions(precision=2):
... print(jnp.cov(x))
[[0.9 0.03 0.1 ]
[0.03 1. 0.01]
[0.1 0.01 0.85]]
"""
if y is not None:
m, y = util.promote_args_inexact("cov", m, y)
if y.ndim > 2:
raise ValueError("y has more than 2 dimensions")
else:
m, = util.promote_args_inexact("cov", m)
if m.ndim > 2:
raise ValueError("m has more than 2 dimensions") # same as numpy error
if dtype is not None and not dtypes.issubdtype(dtype, np.inexact):
raise ValueError(f"cov: dtype must be a subclass of float or complex; got {dtype=}")
X = atleast_2d(m)
if not rowvar and m.ndim != 1:
X = X.T
if X.shape[0] == 0:
return array([]).reshape(0, 0)
if y is not None:
y_arr = atleast_2d(y)
if not rowvar and y_arr.shape[0] != 1:
y_arr = y_arr.T
X = concatenate((X, y_arr), axis=0)
if X.shape[1] == 0:
cov_shape = () if X.shape[0] == 1 else (X.shape[0], X.shape[0])
return array_creation.full(cov_shape, np.nan, dtype=X.dtype)
if ddof is None:
ddof = 1 if bias == 0 else 0View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Omit dtype to use the default float promotion
- Use a float dtype: dtype=jnp.float32 or jnp.float64
- Validate dtype with jnp.issubdtype(dtype, jnp.inexact) before passing
Example fix
// before jnp.cov(m, dtype=jnp.int32) # ValueError // after jnp.cov(m, dtype=jnp.float32)
Defensive patterns
Strategy: type-guard
Validate before calling
if dtype is not None:
assert jnp.issubdtype(dtype, jnp.inexact), 'cov dtype must be float/complex'
jnp.cov(m, dtype=dtype) Type guard
def is_inexact_dtype(d) -> bool:
return d is None or jnp.issubdtype(d, jnp.inexact) Prevention
- Only pass float/complex dtypes to cov
- Default dtype is fine in most cases
- Validate injected dtype variables
When it happens
Trigger: jnp.cov(m, dtype=jnp.int32) or dtype=float-with-integer-numpy-type like np.dtype(int64); any integer or boolean dtype request.
Common situations: Users assuming dtype works like astype on arbitrary arrays; passing a config-driven dtype variable that may be int in some code paths.
Related errors
- fweights must be integer.
- {} does not accept dtype {}. Accepted dtypes are subtypes of
- {name} does not accept dtype {dtype_to_string(aval.dtype)}.
- {} does not accept dtype {} at position {}. Accepted dtypes
- Input type is incompatible with `preferred_element_type`. Th
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/5836579496f92f59.
Report an issue: GitHub.