jax-ml/jax · error · TypeError
Cannot make an array with dtype {dtype} from an object with
Error message
Cannot make an array with dtype {dtype} from an object with dtype {object.dtype}. What it means
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.
Source
Thrown at jax/_src/numpy/array_constructors.py:93
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)
@export
def array(object: Any, dtype: DTypeLike | None = None, *args, copy: bool = True,
order: str | None = "K", ndmin: int = 0,
device: xc.Device | Sharding | None = None,
out_sharding: NamedSharding | P | None = None) -> Array:View on GitHub (pinned to 1e1c6a8fc0)
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
Example fix
# before import numpy as np, jax.numpy as jnp a = jnp.array(np.array(['1', '2']), dtype=jnp.float32) # after import numpy as np, jax.numpy as jnp a = jnp.array(np.array(['1', '2']).astype(np.float32))
Defensive patterns
Strategy: validation
Validate before calling
import jax.numpy as jnp, numpy as np
def string_or_none_dtype(np_arr, dtype):
if dtype is None:
return None
same = (np_arr.dtype == object or np_arr.dtype.kind in 'US') == (dtype == jnp.string_dtype)
return dtype if same else None # None lets inference work Type guard
null
Try / catch
null
Prevention
- Don't pass explicit dtypes over string arrays
- Do numeric casting in NumPy before jnp.array
When it happens
Trigger: 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.
Common situations: Generic loading code that always passes dtype=float32, hitting a column of strings; or explicitly tagging numeric data with string_dtype by mistake.
Related errors
- Incorrect output dtype for return value #{i}: Expected: {out
- Value '{x}' with dtype {dt} is not a valid JAX array type. O
- {} function carry input and carry output must have equal typ
- lower and upper arguments to fori_loop must have equal types
- the first argument to pow must have an inexact dtype (float
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/42efc08aeb4dcfec.
Report an issue: GitHub.