microsoft/semantic-kernel · error · VectorStoreOperationException

Method 'contains' expects exactly one argument.

Error message

Method 'contains' expects exactly one argument.

What it means

The contains method is special-cased in _eval_Call to perform 'args[0] in target'. It must be called with exactly one argument; zero or more than one raises VectorStoreOperationException.

Source

Thrown at python/semantic_kernel/connectors/in_memory.py:291

    def _eval_Call(self, node: ast.Call, context: Mapping[str, Any]) -> Any:
        """Evaluate a function or method call."""
        args = [self.evaluate(arg, context) for arg in node.args]

        if isinstance(node.func, ast.Name):
            try:
                func = self._direct_call_functions[node.func.id]
            except KeyError as e:
                raise VectorStoreOperationException(
                    f"Function '{node.func.id}' is only supported as a method call in filter expressions."
                ) from e
            return func(*args)

        if isinstance(node.func, ast.Attribute):
            target = self.evaluate(node.func.value, context)
            if node.func.attr == "contains":
                if len(args) != 1:
                    raise VectorStoreOperationException("Method 'contains' expects exactly one argument.")
                return args[0] in target

            try:
                func = getattr(target, node.func.attr)
            except AttributeError as e:
                raise VectorStoreOperationException(
                    f"Method '{node.func.attr}' is not available in filter expressions."
                ) from e

            if not callable(func):
                raise VectorStoreOperationException(
                    f"Attribute '{node.func.attr}' is not callable in filter expressions."
                )
            return func(*args)

        raise VectorStoreOperationException(
            f"Call target node type '{type(node.func).__name__}' is not allowed in filter expressions."
        )

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Call contains with exactly one argument: lambda x: x.tags.contains('a').
  2. Prefer the Python 'in' operator: lambda x: 'a' in x.tags.
  3. For multi-value membership, chain: lambda x: 'a' in x.tags and 'b' in x.tags.

Example fix

# before
VectorSearchOptions(filter=lambda x: x.tags.contains('a', 'b'))  # -> [1318]

# after
VectorSearchOptions(filter=lambda x: 'a' in x.tags and 'b' in x.tags)
Defensive patterns

Strategy: validation

Validate before calling

import ast

def contains_has_one_arg(filter_str: str) -> None:
    for node in ast.walk(ast.parse(filter_str, mode='eval')):
        if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) and node.func.attr == 'contains':
            if len(node.args) != 1:
                raise ValueError("contains() expects exactly one argument; use 'a in x.f' for membership")

Try / catch

from semantic_kernel.exceptions import VectorStoreOperationException
try:
    await collection.search(vector=[...], options=opts)
except VectorStoreOperationException as ex:
    if "expects exactly one argument" in str(ex):
        opts.filter = "lambda x: 'a' in x.tags"
        await collection.search(vector=[...], options=opts)
    else:
        raise

Prevention

When it happens

Trigger: Calling contains with the wrong arity, e.g. lambda x: x.tags.contains('a', 'b') or lambda x: x.tags.contains().

Common situations: Misunderstanding contains as a multi-argument matcher; copy-paste from a different API; expecting SQL-style IN(list).

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/5d91bae1f69f84ae. Report an issue: GitHub.