{"record":{"id":"3d06d4e459f16199","repo":"jax-ml/jax","slug":"currently-string-arrays-can-only-be-made-from-num","errorCode":null,"errorMessage":"Currently, string arrays can only be made from NumPy arrays. Got:  {type(object)}.","messagePattern":"Currently, string arrays can only be made from NumPy arrays\\. Got:  (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/array_constructors.py","lineNumber":86,"sourceCode":"\n\ndef _supports_buffer_protocol(obj):\n  try:\n    memoryview(obj)\n  except TypeError:\n    return False\n  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)","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/array_constructors.py#L68-L104","documentation":"JAX's experimental string-array support (jnp.array with string dtype) can only wrap existing NumPy string arrays; it cannot build strings from Python lists, generators, or other containers because XLA itself has no string element type — the array is stored via device_put of the NumPy buffer.","triggerScenarios":"Calling jnp.array(['a','b'], dtype=jax.numpy.string_dtype) or jnp.array(np.array(['a'])) with anything but an np.ndarray as object — e.g. a Python list of str.","commonSituations":"Trying to hold tokenized text labels or categorical strings in a JAX array, assuming NumPy-like coercion from Python lists.","solutions":["Convert to NumPy first: jnp.array(np.array(['a','b'])) — dtype is inferred as string","Pass the np.array directly and let dtype be inferred rather than constructing from a list","Keep string data in NumPy/pandas and use integer-encoded categories in JAX computations"],"exampleFix":"# before\nimport jax.numpy as jnp\na = jnp.array(['a', 'b', 'c'])\n# after\nimport numpy as np, jax.numpy as jnp\na = jnp.array(np.array(['a', 'b', 'c']))","handlingStrategy":"type-guard","validationCode":"import numpy as np\ndef to_string_array(x):\n    if not isinstance(x, np.ndarray):\n        x = np.asarray(x)\n    return jnp.array(x)","typeGuard":"def is_numpy_string_source(obj) -> bool:\n    import numpy as np\n    return isinstance(obj, np.ndarray)","tryCatchPattern":null,"preventionTips":["Convert string data with np.array() before entering JAX","Prefer integer-encoded categories for strings in compute paths"],"tags":["jax","string-dtype","type-validation"],"backgroundTag":"unsupported-input-type","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}