{"record":{"id":"9b9fd403a8f4170e","repo":"jax-ml/jax","slug":"value-x-with-dtype-dt-is-not-a-valid-jax-arr","errorCode":null,"errorMessage":"Value '{x}' with dtype {dt} is not a valid JAX array type. Only arrays of numeric types are supported by JAX.","messagePattern":"Value '(.+?)' with dtype (.+?) is not a valid JAX array type\\. Only arrays of numeric types are supported by JAX\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/dtypes.py","lineNumber":1028,"sourceCode":"\n    # Numpy scalar types, e.g., np.int32, np.float32\n    if _issubclass(x, np.generic):\n      dt = np.dtype(x)\n      return _maybe_canonicalize_explicit_dtype(dt, \"dtype\")\n\n  # Python scalar values, e.g., int(3), float(3.14)\n  elif (dt := python_scalar_types_to_dtypes.get(type(x))) is not None:\n    return canonicalize_dtype(dt)\n  # Jax Arrays, literal arrays, and scalars.\n  # We intentionally do not canonicalize these types: once we've formed an x64\n  # value, that is something we respect irrespective of the x64 mode.\n  elif isinstance(x, _types_whose_dtype_should_not_be_canonicalized):\n    return x.dtype\n\n  if isinstance(x, (str, np.dtype)):\n    dt = np.dtype(x)\n    if dt not in _jax_dtype_set and not issubdtype(dt, extended):\n      raise TypeError(f\"Value '{x}' with dtype {dt} is not a valid JAX array \"\n                      \"type. Only arrays of numeric types are supported by JAX.\")\n    return _maybe_canonicalize_explicit_dtype(dt, \"dtype\")\n\n  # If x has a dtype attribute, and it's a valid dtype, use it. This avoids\n  # calling np.result_type on objects that might have a .dtype but are not\n  # standard NumPy array-like, which can lead to warnings in NumPy 2.4+.\n  dt_attr = getattr(x, 'dtype', None)\n  if issubdtype(dt_attr, extended) or isinstance(dt_attr, np.dtype):\n    dt = dt_attr\n  else:\n    try:\n      dt = np.result_type(x)\n    except TypeError as err:\n      raise TypeError(f\"Cannot determine dtype of {x}\") from err\n  if dt not in _jax_dtype_set and not issubdtype(dt, extended):\n    raise TypeError(f\"Value '{x}' with dtype {dt} is not a valid JAX array \"\n                    \"type. Only arrays of numeric types are supported by JAX.\")\n  # TODO(jakevdp): fix return type annotation and remove this ignore.","sourceCodeStart":1010,"sourceCodeEnd":1046,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/dtypes.py#L1010-L1046","documentation":"When dtype() is given a string or np.dtype, it converts via np.dtype(x) and verifies the result is a JAX-supported type (numeric/bool or an extended dtype). Strings that parse to non-numeric NumPy dtypes — like 'str', 'U10', 'datetime64', 'object' — fail this check.","triggerScenarios":"jax.dtypes.dtype('U10'), dtype('S'), dtype('datetime64[ns]'), dtype('object'), or jnp.array(..., dtype='str') style paths that reach this validation.","commonSituations":"Passing a format string meant for something else as a dtype; encoding text data; copying NumPy snippets that use 'datetime64' dtypes; column-type strings from a schema (e.g. 'object' from pandas).","solutions":["Map string data to numeric ids first, then use an integer dtype","Use a supported dtype string: 'float32','int32','complex64','bool', etc.","If you genuinely need extended dtypes (e.g. jax.dtypes.prng_key), use the dtype object, not a string"],"exampleFix":"# before\njax.dtypes.dtype('U10')\n\n# after\nids = np.array([s.encode() for s in strings], dtype=np.int32)  # encode first\njax.dtypes.dtype(ids.dtype)","handlingStrategy":"type-guard","validationCode":"import numpy as np\nif isinstance(x, (str, np.dtype)):\n    assert np.dtype(x).kind in 'biufc', f'non-numeric dtype {np.dtype(x)}'","typeGuard":"def is_numeric_dtype_spec(s: object) -> bool:\n    import numpy as np\n    if not isinstance(s, (str, np.dtype)):\n        return True\n    try:\n        return np.dtype(s).kind in 'biufc'\n    except TypeError:\n        return False","tryCatchPattern":null,"preventionTips":["Encode strings to integer ids before entering JAX code","Validate dtype strings against a whitelist before passing them to dtype()"],"tags":["jax","dtype","string-dtype","validation"],"backgroundTag":"unsupported-dtype","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}