jax-ml/jax · error · ValueError

unexpected input: {dtype=}

Error message

unexpected input: {dtype=}

What it means

itemsize_bits fell through every dtype category (bool, integer, floating, complex) — the input is not a recognizable numeric dtype (e.g. a string dtype, object dtype, void/float0-adjacent, or an arbitrary object whose __eq__ matches nothing).

Source

Thrown at jax/_src/dtypes.py:297

  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'),
    np.dtype('complex128'): np.dtype('complex64'),
}

# Note: we promote narrow types to float32 here for backward compatibility
# with earlier approaches. We might consider revisiting this, or perhaps
# tying the logic more closely to the type promotion lattice.
_dtype_to_inexact: dict[DType, DType] = {
    np.dtype(k): np.dtype(v) for k, v in [
        ('bool', 'float32'),
        ('uint4', 'float32'), ('int4', 'float32'),

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Canonicalize first: dtype = np.dtype(dtype); validate with jax.numpy.issubdtype checks before calling
  2. Reject non-numeric dtypes at API boundaries of your code
  3. Convert string/object arrays to numeric before numeric-layout operations

Example fix

# before
itemsize_bits('O')

# after
dtype = np.dtype(x.dtype)
if dtype.kind not in 'biufc':
    raise TypeError(f'non-numeric dtype {dtype}')
itemsize_bits(dtype)
Defensive patterns

Strategy: type-guard

Validate before calling

d = np.dtype(dtype)
assert d.kind in 'biufc', f'non-numeric dtype {d}'

Type guard

import numpy as np
def is_numeric_dtype(d) -> bool:
    return np.dtype(d).kind in 'biufc'

Try / catch

try:
    itemsize_bits(dtype)
except ValueError:
    dtype = np.float32  # explicit fallback policy

Prevention

When it happens

Trigger: itemsize_bits('U10'), itemsize_bits(np.dtype('O')), or passing a non-dtype Python object.

Common situations: Dynamic dtype handling where a string dtype or object array sneaks into numeric layout logic (bitcast, viewing, block-mapping checks).

Related errors


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