jax-ml/jax · error · TypeError

Currently, string arrays can only be made from NumPy arrays.

Error message

Currently, string arrays can only be made from NumPy arrays. Got:  {type(object)}.

What it means

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.

Source

Thrown at jax/_src/numpy/array_constructors.py:86


def _supports_buffer_protocol(obj):
  try:
    memoryview(obj)
  except TypeError:
    return False
  else:
    return True


def _make_string_array(
    object: np.ndarray,
    dtype: DTypeLike | None = None,
    ndmin: int = 0,
    device: xc.Device | Sharding | None = None,
) -> Array:
  if not isinstance(object, np.ndarray):
    raise TypeError(
        "Currently, string arrays can only be made from NumPy"
        f" arrays. Got:  {type(object)}."
    )
  if dtype is not None and (
      (object.dtype == dtypes.string_dtype) != (dtype == dtypes.string_dtype)
  ):
    raise TypeError(
        f"Cannot make an array with dtype {dtype} from an object with dtype"
        f" {object.dtype}."
    )
  if ndmin > object.ndim:
    raise TypeError(
        f"ndmin {ndmin} cannot be greater than object's ndims"
        f" {object.ndim} for string arrays."
    )

  # Just do a device_put since XLA does not support string as a data type.
  return api.device_put(x=object, device=device)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Convert to NumPy first: jnp.array(np.array(['a','b'])) — dtype is inferred as string
  2. Pass the np.array directly and let dtype be inferred rather than constructing from a list
  3. Keep string data in NumPy/pandas and use integer-encoded categories in JAX computations

Example fix

# before
import jax.numpy as jnp
a = jnp.array(['a', 'b', 'c'])
# after
import numpy as np, jax.numpy as jnp
a = jnp.array(np.array(['a', 'b', 'c']))
Defensive patterns

Strategy: type-guard

Validate before calling

import numpy as np
def to_string_array(x):
    if not isinstance(x, np.ndarray):
        x = np.asarray(x)
    return jnp.array(x)

Type guard

def is_numpy_string_source(obj) -> bool:
    import numpy as np
    return isinstance(obj, np.ndarray)

Prevention

When it happens

Trigger: 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.

Common situations: Trying to hold tokenized text labels or categorical strings in a JAX array, assuming NumPy-like coercion from Python lists.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/3d06d4e459f16199. Report an issue: GitHub.