jax-ml/jax · error · TypeError

ndmin {ndmin} cannot be greater than object's ndims {object.

Error message

ndmin {ndmin} cannot be greater than object's ndims {object.ndim} for string arrays.

What it means

For string arrays, jnp.array's ndmin parameter can only pad dimensions the source NumPy array already has or fewer; it cannot broadcast a string array up to more dimensions because no element-wise string construction path exists.

Source

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

    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:
  """Convert an object to a JAX array.

  JAX implementation of :func:`numpy.array`.

  Args:

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Reshape in NumPy before conversion: np.array(['a']).reshape(1, -1) then jnp.array(...)
  2. Only use ndmin <= object.ndim for string arrays, adding new axes afterwards with jnp.expand_dims

Example fix

# before
import numpy as np, jax.numpy as jnp
a = jnp.array(np.array(['a']), ndmin=2)
# after
import numpy as np, jax.numpy as jnp
a = jnp.array(np.array(['a']).reshape(1, 1))
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
def prep_string_array(np_arr, ndmin):
    while np_arr.ndim < ndmin:
        np_arr = np_arr[np.newaxis, ...]
    return np_arr

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: jnp.array(np.array(['a']), ndmin=2) — ndmin greater than the source array's ndim.

Common situations: Pipeline code that normalizes inputs to at least 2D with ndmin=2 running over scalar/1-D string labels.

Related errors


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