jax-ml/jax · error · ValueError

dtype cannot be None.

Error message

dtype cannot be None.

What it means

Guard inside dtypes.itemsize_bits (via the shared dtype-normalization path): None was passed where a dtype-like object is required. NumPy would silently coerce None to float64, so the helper rejects it explicitly before computing the bit width.

Solutions

  1. Populate the dtype before the call (resolve defaults: `dtype = dtype or jnp.float32` if that is intended)
  2. Add an assert dtype is not None upstream to fail fast with context
  3. 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

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


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/087aa58363dedcf5. Report an issue: GitHub.

Appendix: 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)