{"record":{"id":"42efc08aeb4dcfec","repo":"jax-ml/jax","slug":"cannot-make-an-array-with-dtype-dtype-from-an-ob","errorCode":null,"errorMessage":"Cannot make an array with dtype {dtype} from an object with dtype {object.dtype}.","messagePattern":"Cannot make an array with dtype (.+?) from an object with dtype (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/array_constructors.py","lineNumber":93,"sourceCode":"  else:\n    return True\n\n\ndef _make_string_array(\n    object: np.ndarray,\n    dtype: DTypeLike | None = None,\n    ndmin: int = 0,\n    device: xc.Device | Sharding | None = None,\n) -> Array:\n  if not isinstance(object, np.ndarray):\n    raise TypeError(\n        \"Currently, string arrays can only be made from NumPy\"\n        f\" arrays. Got:  {type(object)}.\"\n    )\n  if dtype is not None and (\n      (object.dtype == dtypes.string_dtype) != (dtype == dtypes.string_dtype)\n  ):\n    raise TypeError(\n        f\"Cannot make an array with dtype {dtype} from an object with dtype\"\n        f\" {object.dtype}.\"\n    )\n  if ndmin > object.ndim:\n    raise TypeError(\n        f\"ndmin {ndmin} cannot be greater than object's ndims\"\n        f\" {object.ndim} for string arrays.\"\n    )\n\n  # Just do a device_put since XLA does not support string as a data type.\n  return api.device_put(x=object, device=device)\n\n\n@export\ndef array(object: Any, dtype: DTypeLike | None = None, *args, copy: bool = True,\n          order: str | None = \"K\", ndmin: int = 0,\n          device: xc.Device | Sharding | None = None,\n          out_sharding: NamedSharding | P | None = None) -> Array:","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/array_constructors.py#L75-L111","documentation":"When creating a string array via _make_string_array, JAX requires the requested dtype and the source NumPy array's dtype to agree in string-ness: you cannot request string dtype from a numeric array, or a numeric dtype from a string array.","triggerScenarios":"jnp.array(np.array(['a','b']), dtype=jnp.float32) or jnp.array(np.array([1,2]), dtype=jax.numpy.string_dtype) — a mismatch between object's string/numeric dtype and the requested dtype.","commonSituations":"Generic loading code that always passes dtype=float32, hitting a column of strings; or explicitly tagging numeric data with string_dtype by mistake.","solutions":["Match dtypes: drop the dtype argument and let it be inferred, or convert in NumPy first (np.asarray(...).astype(...))","For numeric output from string input, parse/convert via NumPy before calling jnp.array"],"exampleFix":"# before\nimport numpy as np, jax.numpy as jnp\na = jnp.array(np.array(['1', '2']), dtype=jnp.float32)\n# after\nimport numpy as np, jax.numpy as jnp\na = jnp.array(np.array(['1', '2']).astype(np.float32))","handlingStrategy":"validation","validationCode":"import jax.numpy as jnp, numpy as np\ndef string_or_none_dtype(np_arr, dtype):\n    if dtype is None:\n        return None\n    same = (np_arr.dtype == object or np_arr.dtype.kind in 'US') == (dtype == jnp.string_dtype)\n    return dtype if same else None  # None lets inference work","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Don't pass explicit dtypes over string arrays","Do numeric casting in NumPy before jnp.array"],"tags":["jax","string-dtype","dtype-mismatch"],"backgroundTag":"dtype-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}