{"record":{"id":"79bbbc2199d86810","repo":"jax-ml/jax","slug":"array-contains-unsupported-operand-type-type","errorCode":null,"errorMessage":"Array.__contains__: unsupported operand type {type(other)}.","messagePattern":"Array\\.__contains__: unsupported operand type (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/array_methods.py","lineNumber":214,"sourceCode":"def _conjugate(self: Array) -> Array:\n  \"\"\"Return the complex conjugate of the array.\n\n  Refer to :func:`jax.numpy.conjugate` for the full documentation.\n  \"\"\"\n  return ufuncs.conjugate(self)\n\ndef _contains(self: Array, other: ArrayLike) -> Array:\n  \"\"\"Implements __contains__ for JAX arrays.\n\n  This is used by the Python ``in`` operator.\n  \"\"\"\n  # Note: we deliberately depart from NumPy's behavior here, which includes\n  # some oddities (https://github.com/numpy/numpy/issues/21933). Namely, we\n  # require `self` to be a 1D array, and require `other` to be a scalar.'\n\n  # Explicitly check for string and None types, as these were common bugs.\n  if other is None or isinstance(other, str):\n    raise TypeError(f\"Array.__contains__: unsupported operand type {type(other)}.\")\n  query = util.ensure_arraylike('Array.__contains__', other)\n  if self.ndim != 1:\n    raise ValueError(\"Array.__contains__: search array must be one-dimensional,\"\n                     f\" got arr.shape={self.shape}.\")\n  if query.ndim != 0:\n    raise ValueError(\"Array.__contains__: query value must be a scalar,\"\n                     f\" got {query.shape=}\")\n  return reductions.any(self == query)\n\ndef _copy(self: Array) -> Array:\n  \"\"\"Return a copy of the array.\n\n  Refer to :func:`jax.numpy.copy` for the full documentation.\n  \"\"\"\n  return lax_numpy.copy(self)\n\ndef _cumprod(self: Array, axis: int | None = None,\n             dtype: DTypeLike | None = None, out: None = None) -> Array:","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/array_methods.py#L196-L232","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Replace the membership test with an explicit check, e.g. `if other is None: ...` before `other in arr`","For string membership use a Python/NumPy structure (list/set) or jnp arrays of numeric codes instead","Convert your data to numeric dtype before using `in` on a jax array"],"exampleFix":"// before\nif key in jax_arr:  # key may be None or a str\n\n# after\nif key is not None and not isinstance(key, str) and key in jax_arr:","handlingStrategy":"type-guard","validationCode":"def safe_contains(arr, x):\n    if x is None or isinstance(x, str):\n        return False\n    return bool(x in arr)","typeGuard":"def is_valid_query(x) -> bool:\n    return x is not None and not isinstance(x, str)","tryCatchPattern":null,"preventionTips":["Never use `in` with possibly-None optionals; null-check first","Keep string data out of arrays passed to `in` on jax arrays"],"tags":["jax","membership","typeerror","contains"],"backgroundTag":"unsupported-operand-type","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}