jax-ml/jax · error · TypeError

Array.__contains__: unsupported operand type {type(other)}.

Error message

Array.__contains__: unsupported operand type {type(other)}.

What it means

jax.Array.__contains__ (the `in` operator) explicitly rejects None and str operands before converting the query to an array. JAX departs from NumPy here because NumPy's `in` semantics have oddities (numpy/numpy#21933); it requires the array to be 1D and the query to be a scalar. Passing a string or None almost always indicates a bug in caller code, so JAX fails fast.

Source

Thrown at jax/_src/numpy/array_methods.py:214

def _conjugate(self: Array) -> Array:
  """Return the complex conjugate of the array.

  Refer to :func:`jax.numpy.conjugate` for the full documentation.
  """
  return ufuncs.conjugate(self)

def _contains(self: Array, other: ArrayLike) -> Array:
  """Implements __contains__ for JAX arrays.

  This is used by the Python ``in`` operator.
  """
  # Note: we deliberately depart from NumPy's behavior here, which includes
  # some oddities (https://github.com/numpy/numpy/issues/21933). Namely, we
  # require `self` to be a 1D array, and require `other` to be a scalar.'

  # Explicitly check for string and None types, as these were common bugs.
  if other is None or isinstance(other, str):
    raise TypeError(f"Array.__contains__: unsupported operand type {type(other)}.")
  query = util.ensure_arraylike('Array.__contains__', other)
  if self.ndim != 1:
    raise ValueError("Array.__contains__: search array must be one-dimensional,"
                     f" got arr.shape={self.shape}.")
  if query.ndim != 0:
    raise ValueError("Array.__contains__: query value must be a scalar,"
                     f" got {query.shape=}")
  return reductions.any(self == query)

def _copy(self: Array) -> Array:
  """Return a copy of the array.

  Refer to :func:`jax.numpy.copy` for the full documentation.
  """
  return lax_numpy.copy(self)

def _cumprod(self: Array, axis: int | None = None,
             dtype: DTypeLike | None = None, out: None = None) -> Array:

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Replace the membership test with an explicit check, e.g. `if other is None: ...` before `other in arr`
  2. For string membership use a Python/NumPy structure (list/set) or jnp arrays of numeric codes instead
  3. Convert your data to numeric dtype before using `in` on a jax array

Example fix

// before
if key in jax_arr:  # key may be None or a str

# after
if key is not None and not isinstance(key, str) and key in jax_arr:
Defensive patterns

Strategy: type-guard

Validate before calling

def safe_contains(arr, x):
    if x is None or isinstance(x, str):
        return False
    return bool(x in arr)

Type guard

def is_valid_query(x) -> bool:
    return x is not None and not isinstance(x, str)

Prevention

When it happens

Trigger: Calling `x in jnp_array` or `jnp_array.__contains__(other)` where `other` is a Python `str` or `None`, e.g. checking `None in arr` or `'foo' in arr` where arr is a jax.Array.

Common situations: Data pipelines where an optional value (possibly None) is tested for membership before null-handling; mixing object/string columns with numeric jax arrays; copy-pasted NumPy code that used strings for object-dtype arrays.

Related errors


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