jax-ml/jax · error · TypeError
Expected kind to be a dtype, string, or tuple; got {kind=}
Error message
Expected kind to be a dtype, string, or tuple; got {kind=} What it means
jax.numpy.isdtype(dtype, kind) requires `kind` to be a dtype, a string category (like 'real','numeric','signed integer'), or a (possibly nested) tuple of those. This TypeError is raised when `kind` is some other object (e.g. a list, an int, a class instance) and NumPy's np.dtype(kind) also raised TypeError. It mirrors the semantics of the NumPy NEP 49 isdtype extension.
Source
Thrown at jax/_src/dtypes.py:675
True or False
"""
the_dtype = np.dtype(dtype)
kind_tuple: tuple[str | DTypeLike, ...] = (
kind if isinstance(kind, tuple) else (kind,)
)
options: set[DType] = set()
for kind in kind_tuple:
if isinstance(kind, str) and kind in _dtype_kinds:
options.update(_dtype_kinds[kind])
continue
try:
_dtype = np.dtype(kind)
except TypeError as e:
if isinstance(kind, str):
raise ValueError(
f"Unrecognized {kind=} expected one of {list(_dtype_kinds.keys())}, "
"or a compatible input for jnp.dtype()")
raise TypeError(
f"Expected kind to be a dtype, string, or tuple; got {kind=}"
) from e
options.add(_dtype)
return the_dtype in options
def _jax_type(dtype: DType, weak_type: bool) -> JAXType:
"""Return the jax type for a dtype and weak type."""
if weak_type:
if dtype == bool:
return dtype
if dtype in _custom_float_dtypes:
return float
return type(dtype.type(0).item())
return dtype
def _dtype_and_weaktype(value: Any) -> tuple[DType, bool]:
"""Return a (dtype, weak_type) tuple for the given input."""View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Convert the kind argument to a tuple: jnp.isdtype(dt, tuple(kinds)) if it is a list
- Use one of the recognized kind strings: 'bool','signed integer','unsigned integer','integral','real floating','complex floating','numeric','real'
- Pass a concrete dtype (e.g. np.dtype('float32') or jnp.float32) as kind
Example fix
# before
jnp.isdtype(x.dtype, kind=['real', 'complex'])
# after
jnp.isdtype(x.dtype, kind=('real', 'complex')) Defensive patterns
Strategy: validation
Validate before calling
def valid_kind(k):
from jax._src import dtypes
allowed = {'bool','signed integer','unsigned integer','integral',
'real floating','complex floating','numeric','real'}
if isinstance(k, tuple):
return all(valid_kind(x) for x in k)
return isinstance(k, str) and k in allowed or hasattr(k, 'itemsize') or k in dtypes._dtype_kinds
kind = tuple(kind) if isinstance(kind, list) else kind Type guard
def is_isdtype_kind(k: object) -> bool:
if isinstance(k, tuple):
return all(is_isdtype_kind(x) for x in k)
if isinstance(k, str):
return k in {'bool','signed integer','unsigned integer','integral',
'real floating','complex floating','numeric','real'}
return isinstance(k, np.dtype) or k in (bool, int, float, complex) Prevention
- Always pass kind as a tuple, never a list
- Validate kind against the allowed category strings before calling isdtype
When it happens
Trigger: Calling jnp.isdtype(x.dtype, kind=["real","complex"]) (list instead of tuple), passing a dtype class object like kind=int32-class where np.dtype fails, or passing an arbitrary Python object as kind. Strings that fail np.dtype parsing raise ValueError (the sibling branch); this TypeError is specifically for non-str non-dtype objects.
Common situations: Porting NumPy code where a list was used for kind; building kind dynamically from user input that arrives as a list; passing a numpy scalar instance or a shape tuple by mistake.
Understand the failure class
Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.
Related errors
- dtype must be specified.
- out_dtype should be an integer type; got {out_dtype}
- out_dtype must be integer typed; got {out_dtype=}
- primal and tangent arguments to jax.jvp do not match; dtypes
- unexpected JAX type (e.g. shape/dtype) for gradient ref pass
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/46829c1514a494a2.
Report an issue: GitHub.