{"record":{"id":"1d64ce4790706878","repo":"jax-ml/jax","slug":"ndmin-ndmin-cannot-be-greater-than-object-s-ndim","errorCode":null,"errorMessage":"ndmin {ndmin} cannot be greater than object's ndims {object.ndim} for string arrays.","messagePattern":"ndmin (.+?) cannot be greater than object's ndims (.+?) for string arrays\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/array_constructors.py","lineNumber":98,"sourceCode":"    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:\n  \"\"\"Convert an object to a JAX array.\n\n  JAX implementation of :func:`numpy.array`.\n\n  Args:","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/array_constructors.py#L80-L116","documentation":"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.","triggerScenarios":"jnp.array(np.array(['a']), ndmin=2) — ndmin greater than the source array's ndim.","commonSituations":"Pipeline code that normalizes inputs to at least 2D with ndmin=2 running over scalar/1-D string labels.","solutions":["Reshape in NumPy before conversion: np.array(['a']).reshape(1, -1) then jnp.array(...)","Only use ndmin <= object.ndim for string arrays, adding new axes afterwards with jnp.expand_dims"],"exampleFix":"# before\nimport numpy as np, jax.numpy as jnp\na = jnp.array(np.array(['a']), ndmin=2)\n# after\nimport numpy as np, jax.numpy as jnp\na = jnp.array(np.array(['a']).reshape(1, 1))","handlingStrategy":"validation","validationCode":"import numpy as np\ndef prep_string_array(np_arr, ndmin):\n    while np_arr.ndim < ndmin:\n        np_arr = np_arr[np.newaxis, ...]\n    return np_arr","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Expand dims in NumPy before conversion","Only use ndmin <= source.ndim for strings"],"tags":["jax","string-dtype","shape-validation"],"backgroundTag":"invalid-shape-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}