{"record":{"id":"9d2bb5737b5e50d7","repo":"jax-ml/jax","slug":"unexpected-input-type-for-array-type-object","errorCode":null,"errorMessage":"Unexpected input type for array: {type(object)}","messagePattern":"Unexpected input type for array: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/array_constructors.py","lineNumber":337,"sourceCode":"    if object:\n      arrs = (array(elt, dtype=dtype, copy=False) for elt in object)\n      arrays_out = [lax.expand_dims(arr, [0]) for arr in arrs]\n      # lax.concatenate can be slow to compile for wide concatenations, so form a\n      # tree of concatenations as a workaround especially for op-by-op mode.\n      # (https://github.com/jax-ml/jax/issues/653).\n      k = 16\n      while len(arrays_out) > k:\n        arrays_out = [lax.concatenate(arrays_out[i:i+k], 0)\n                      for i in range(0, len(arrays_out), k)]\n      out = lax.concatenate(arrays_out, 0)\n    else:\n      out = np.array([], dtype=dtype)\n  elif _supports_buffer_protocol(object):\n    object = memoryview(object)\n    # TODO(jakevdp): update this once we support NumPy 2.0 semantics for the copy arg.\n    out = np.array(object) if copy else np.asarray(object)\n  else:\n    raise TypeError(f\"Unexpected input type for array: {type(object)}\")\n  out_array: Array = lax._convert_element_type(\n      out, dtype, weak_type=weak_type, sharding=sharding)\n  if ndmin > np.ndim(out_array):\n    out_array = lax.expand_dims(out_array, range(ndmin - np.ndim(out_array)))\n  return out_array\n\n\ndef _get_platform(\n    device_or_sharding: xc.Device | Sharding | None | str) -> str:\n  \"\"\"Get device_or_sharding platform or look up config.default_device.value.\"\"\"\n  if isinstance(device_or_sharding, xc.Device):\n    return device_or_sharding.platform\n  elif isinstance(device_or_sharding, Sharding):\n    return list(device_or_sharding.device_set)[0].platform\n  elif isinstance(device_or_sharding, str):\n    return device_or_sharding\n  elif device_or_sharding is None:\n    if config.default_device.value is None:","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/array_constructors.py#L319-L355","documentation":"jnp.array's final fallback: after checking Tracer, np.ndarray, scalar types, lists/tuples (flattened earlier), and buffer-protocol objects, the input type is not convertible to an array and raises TypeError with the offending type name.","triggerScenarios":"jnp.array(some_arbitrary_object) where object is e.g. a dict, custom class without __jax_array__/__buffer__, file handle, or string under a non-string dtype path.","commonSituations":"Passing a Python object wrapper (e.g. a dataclass or config object) instead of its .array field; assuming arbitrary iterables like sets or dicts convert like they roughly do in NumPy.","solutions":["Extract the numeric data first (e.g. obj.array, np.asarray(obj))","Implement __jax_array__ on your custom class so jnp.array can consume it","For sets/dicts/iterables, convert to a list of leaves first"],"exampleFix":"# before\na = jnp.array(my_dataclass)\n# after\na = jnp.array(my_dataclass.values)  # or implement __jax_array__ on the class","handlingStrategy":"type-guard","validationCode":"def convert_or_fail(obj):\n    import numpy as np\n    return obj if hasattr(obj, '__jax_array__') else np.asarray(obj)","typeGuard":"def is_jax_convertible(obj) -> bool:\n    import numpy as np\n    return hasattr(obj, '__jax_array__') or isinstance(obj, (np.ndarray, list, tuple, int, float, bool, complex, bytes, memoryview))","tryCatchPattern":"null","preventionTips":["Implement __jax_array__ on wrapper classes","Extract numeric fields from dataclasses explicitly"],"tags":["jax","type-validation","conversion"],"backgroundTag":"unsupported-input-type","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}