{"record":{"id":"45707044882bf4a0","repo":"jax-ml/jax","slug":"unexpected-input-dtype","errorCode":null,"errorMessage":"unexpected input: {dtype=}","messagePattern":"unexpected input: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/dtypes.py","lineNumber":297,"sourceCode":"  complex: default_complex_dtype,\n}\n\ndef itemsize_bits(dtype: DTypeLike) -> int:\n  \"\"\"Number of bits per element for the dtype.\"\"\"\n  # Note: we cannot use dtype.itemsize here because this is\n  # incorrect for sub-byte integer types.\n  if dtype is None:\n    raise ValueError(\"dtype cannot be None.\")\n  if dtype == np.dtype(bool):\n    return 8  # physical bit layout for boolean dtype\n  elif issubdtype(dtype, np.integer):\n    return iinfo(dtype).bits\n  elif issubdtype(dtype, np.floating):\n    return finfo(dtype).bits\n  elif issubdtype(dtype, np.complexfloating):\n    return 2 * finfo(dtype).bits\n  else:\n    raise ValueError(f\"unexpected input: {dtype=}\")\n\n# Trivial vectorspace datatype needed for tangent values of int/bool primals\nfloat0: np.dtype = np.dtype([('float0', np.void, 0)])\n\n_dtype_to_32bit_dtype: dict[DType, DType] = {\n    np.dtype('int64'): np.dtype('int32'),\n    np.dtype('uint64'): np.dtype('uint32'),\n    np.dtype('float64'): np.dtype('float32'),\n    np.dtype('complex128'): np.dtype('complex64'),\n}\n\n# Note: we promote narrow types to float32 here for backward compatibility\n# with earlier approaches. We might consider revisiting this, or perhaps\n# tying the logic more closely to the type promotion lattice.\n_dtype_to_inexact: dict[DType, DType] = {\n    np.dtype(k): np.dtype(v) for k, v in [\n        ('bool', 'float32'),\n        ('uint4', 'float32'), ('int4', 'float32'),","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/dtypes.py#L279-L315","documentation":"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).","triggerScenarios":"itemsize_bits('U10'), itemsize_bits(np.dtype('O')), or passing a non-dtype Python object.","commonSituations":"Dynamic dtype handling where a string dtype or object array sneaks into numeric layout logic (bitcast, viewing, block-mapping checks).","solutions":["Canonicalize first: dtype = np.dtype(dtype); validate with jax.numpy.issubdtype checks before calling","Reject non-numeric dtypes at API boundaries of your code","Convert string/object arrays to numeric before numeric-layout operations"],"exampleFix":"# before\nitemsize_bits('O')\n\n# after\ndtype = np.dtype(x.dtype)\nif dtype.kind not in 'biufc':\n    raise TypeError(f'non-numeric dtype {dtype}')\nitemsize_bits(dtype)","handlingStrategy":"type-guard","validationCode":"d = np.dtype(dtype)\nassert d.kind in 'biufc', f'non-numeric dtype {d}'","typeGuard":"import numpy as np\ndef is_numeric_dtype(d) -> bool:\n    return np.dtype(d).kind in 'biufc'","tryCatchPattern":"try:\n    itemsize_bits(dtype)\nexcept ValueError:\n    dtype = np.float32  # explicit fallback policy","preventionTips":["Canonicalize dtypes with np.dtype early","Filter out object/string arrays before layout computations"],"tags":["jax","dtype","validation"],"backgroundTag":"invalid-dtype","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}