jax-ml/jax · error · ValueError
dtype cannot be None.
Error message
dtype cannot be None.
What it means
jax.dtypes.itemsize_bits requires a concrete dtype; passing None (typically a dtype field that was never populated) is rejected because there is no meaningful bit width.
Source
Thrown at jax/_src/dtypes.py:287
elif issubdtype(obj, extended):
return obj # pyrefly: ignore[bad-return]
elif isinstance(obj, type) and (f := _DEFAULT_TYPEMAP.get(obj)) is not None:
obj = f()
return np.dtype(obj, align=align, copy=copy)
_DEFAULT_TYPEMAP: dict[type, Callable[[], np.dtype]] = {
bool: lambda: np.dtype(bool),
int: default_int_dtype,
float: default_float_dtype,
complex: default_complex_dtype,
}
def itemsize_bits(dtype: DTypeLike) -> int:
"""Number of bits per element for the dtype."""
# Note: we cannot use dtype.itemsize here because this is
# incorrect for sub-byte integer types.
if dtype is None:
raise ValueError("dtype cannot be None.")
if dtype == np.dtype(bool):
return 8 # physical bit layout for boolean dtype
elif issubdtype(dtype, np.integer):
return iinfo(dtype).bits
elif issubdtype(dtype, np.floating):
return finfo(dtype).bits
elif issubdtype(dtype, np.complexfloating):
return 2 * finfo(dtype).bits
else:
raise ValueError(f"unexpected input: {dtype=}")
# Trivial vectorspace datatype needed for tangent values of int/bool primals
float0: np.dtype = np.dtype([('float0', np.void, 0)])
_dtype_to_32bit_dtype: dict[DType, DType] = {
np.dtype('int64'): np.dtype('int32'),
np.dtype('uint64'): np.dtype('uint32'),
np.dtype('float64'): np.dtype('float32'),View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Populate the dtype before the call (resolve defaults: `dtype = dtype or jnp.float32` if that is intended)
- Add an assert dtype is not None upstream to fail fast with context
- Fix the caller that dropped the dtype argument
Example fix
# before itemsize_bits(x.dtype) # x.dtype is None # after assert x.dtype is not None itemsize_bits(x.dtype)
Defensive patterns
Strategy: validation
Validate before calling
if dtype is None:
raise ValueError('dtype required')
jax.dtypes.itemsize_bits(dtype) Prevention
- Assert dtype is not None right after parsing inputs
- Never default dtype arguments to None for numeric layout APIs
When it happens
Trigger: Calling itemsize_bits(None) or threading a None dtype from config/shape-rule code (e.g. bitcast_convert_type with unset dtype) into this helper.
Common situations: Optional dtype parameters defaulting to None; partially-initialized dtypes in custom primitives or sharding rules calling _bitcast paths.
Related errors
- Expected a string or dtype-like object; got {dtype=}
- unexpected input: {dtype=}
- Unrecognized {kind=} expected one of {list(_dtype_kinds.keys
- Dtype {dtype} is not a valid JAX array type. Only arrays of
- Value '{x}' with dtype {dt} is not a valid JAX array type. O
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/087aa58363dedcf5.
Report an issue: GitHub.