{"record":{"id":"3d1f3fdad972e1b2","repo":"jax-ml/jax","slug":"cannot-determine-dtype-of-x","errorCode":null,"errorMessage":"Cannot determine dtype of {x}","messagePattern":"Cannot determine dtype of (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/dtypes.py","lineNumber":1042,"sourceCode":"\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.\n  return canonicalize_dtype(dt, allow_extended_dtype=True)  # pyrefly: ignore[bad-return]\n\ndef lattice_result_type(*args: Any) -> tuple[DType, bool]:\n  dtypes, weak_types = zip(*(_dtype_and_weaktype(arg) for arg in args))\n  if len(dtypes) == 1:\n    out_dtype = dtypes[0]\n    out_weak_type = weak_types[0]\n  elif len(set(dtypes)) == 1 and not all(weak_types):\n    # Trivial promotion case. This allows extended dtypes through.\n    out_dtype = dtypes[0]\n    out_weak_type = False\n  elif all(weak_types) and config.numpy_dtype_promotion.value != config.NumpyDtypePromotion.STRICT:\n    # If all inputs are weakly typed, we compute the bound of the strongly-typed\n    # counterparts and apply the weak type at the end. This avoids returning the","sourceCodeStart":1024,"sourceCodeEnd":1060,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/dtypes.py#L1024-L1060","documentation":"dtype(x) fell back to np.result_type(x) to infer the dtype of an array-like, and NumPy itself raised TypeError — meaning x has no inferable dtype (e.g. a ragged list, an arbitrary object, or a sequence of incompatible items).","triggerScenarios":"jax.dtypes.dtype([[1,2],[3]]) (ragged nested list), dtype(some_random_object), dtype of a list whose elements are untyped Python objects; the object had no .dtype attribute and np.result_type failed.","commonSituations":"Passing unstructured Python objects or ragged data into APIs that expect array-likes; user-supplied config values that are sometimes lists of mixed types; None elements inside a list ([1, None]).","solutions":["Normalize the input to a proper ndarray first: np.asarray(data, dtype=np.float32)","Validate/flatten nested structures (or use padding) before inferring dtypes","Catch TypeError around dtype inference and produce a clear user-facing message with the offending value"],"exampleFix":"# before\ndt = jax.dtypes.dtype(maybe_ragged)\n\n# after\nif isinstance(maybe_ragged, list):\n    maybe_ragged = np.asarray(maybe_ragged, dtype=np.float32)\ndt = jax.dtypes.dtype(maybe_ragged)","handlingStrategy":"validation","validationCode":"import numpy as np\ntry:\n    a = np.asarray(x)\nexcept (TypeError, ValueError):\n    a = None  # reject before calling dtype()\nassert a is not None, f'cannot convert {x!r} to array'","typeGuard":"def is_array_like_with_dtype(x) -> bool:\n    import numpy as np\n    try:\n        np.result_type(x)\n        return True\n    except TypeError:\n        return False","tryCatchPattern":"try:\n    dt = jax.dtypes.dtype(x)\nexcept TypeError as e:\n    raise ValueError(f'cannot infer dtype for input {x!r}') from e","preventionTips":["Convert ragged/nested Python data to ndarray explicitly with a dtype","Reject None-containing lists during data cleaning"],"tags":["jax","dtype","array-like","inference-failure"],"backgroundTag":"dtype-inference-failure","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}