jax-ml/jax · error · TypeError

Unexpected input type for array: {type(object)}

Error message

Unexpected input type for array: {type(object)}

What it means

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.

Source

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

    if object:
      arrs = (array(elt, dtype=dtype, copy=False) for elt in object)
      arrays_out = [lax.expand_dims(arr, [0]) for arr in arrs]
      # lax.concatenate can be slow to compile for wide concatenations, so form a
      # tree of concatenations as a workaround especially for op-by-op mode.
      # (https://github.com/jax-ml/jax/issues/653).
      k = 16
      while len(arrays_out) > k:
        arrays_out = [lax.concatenate(arrays_out[i:i+k], 0)
                      for i in range(0, len(arrays_out), k)]
      out = lax.concatenate(arrays_out, 0)
    else:
      out = np.array([], dtype=dtype)
  elif _supports_buffer_protocol(object):
    object = memoryview(object)
    # TODO(jakevdp): update this once we support NumPy 2.0 semantics for the copy arg.
    out = np.array(object) if copy else np.asarray(object)
  else:
    raise TypeError(f"Unexpected input type for array: {type(object)}")
  out_array: Array = lax._convert_element_type(
      out, dtype, weak_type=weak_type, sharding=sharding)
  if ndmin > np.ndim(out_array):
    out_array = lax.expand_dims(out_array, range(ndmin - np.ndim(out_array)))
  return out_array


def _get_platform(
    device_or_sharding: xc.Device | Sharding | None | str) -> str:
  """Get device_or_sharding platform or look up config.default_device.value."""
  if isinstance(device_or_sharding, xc.Device):
    return device_or_sharding.platform
  elif isinstance(device_or_sharding, Sharding):
    return list(device_or_sharding.device_set)[0].platform
  elif isinstance(device_or_sharding, str):
    return device_or_sharding
  elif device_or_sharding is None:
    if config.default_device.value is None:

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Extract the numeric data first (e.g. obj.array, np.asarray(obj))
  2. Implement __jax_array__ on your custom class so jnp.array can consume it
  3. For sets/dicts/iterables, convert to a list of leaves first

Example fix

# before
a = jnp.array(my_dataclass)
# after
a = jnp.array(my_dataclass.values)  # or implement __jax_array__ on the class
Defensive patterns

Strategy: type-guard

Validate before calling

def convert_or_fail(obj):
    import numpy as np
    return obj if hasattr(obj, '__jax_array__') else np.asarray(obj)

Type guard

def is_jax_convertible(obj) -> bool:
    import numpy as np
    return hasattr(obj, '__jax_array__') or isinstance(obj, (np.ndarray, list, tuple, int, float, bool, complex, bytes, memoryview))

Try / catch

null

Prevention

When it happens

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

Common situations: 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.

Related errors


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