microsoft/semantic-kernel · error · VectorStoreOperationException

Addition in filter expressions is only allowed for numeric v

Error message

Addition in filter expressions is only allowed for numeric values and same-type sequences.

What it means

Thrown by _safe_add (in_memory.py:347-348) when the operands of `+` are not both numeric, not both str, not both list, and not both tuple. Parse-time only checks that a BinOp/Add node is allowed; the actual operand value types are only known at evaluation, so mixed-type or unsupported addition (str+int, dict+dict, list+str) fails here.

Source

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

        if isinstance(operator_node, ast.Is):
            return left is right
        if isinstance(operator_node, ast.IsNot):
            return left is not right
        raise VectorStoreOperationException(
            f"Comparison operator '{type(operator_node).__name__}' is not allowed in filter expressions."
        )

    def _safe_add(self, left: Any, right: Any) -> Any:
        """Safely evaluate addition."""
        if isinstance(left, (int, float)) and isinstance(right, (int, float)):
            return left + right
        if isinstance(left, str) and isinstance(right, str):
            return self._ensure_sequence_result_size(left, right, lambda a, b: a + b)
        if isinstance(left, list) and isinstance(right, list):
            return self._ensure_sequence_result_size(left, right, lambda a, b: a + b)
        if isinstance(left, tuple) and isinstance(right, tuple):
            return self._ensure_sequence_result_size(left, right, lambda a, b: a + b)
        raise VectorStoreOperationException(
            "Addition in filter expressions is only allowed for numeric values and same-type sequences."
        )

    def _safe_mult(self, left: Any, right: Any) -> Any:
        """Safely evaluate multiplication."""
        if isinstance(left, (int, float)) and isinstance(right, (int, float)):
            return left * right
        if isinstance(left, int) and isinstance(right, (str, list, tuple)):
            return self._safe_repeat(right, left)
        if isinstance(right, int) and isinstance(left, (str, list, tuple)):
            return self._safe_repeat(left, right)
        raise VectorStoreOperationException(
            "Multiplication in filter expressions is only allowed for numeric values and bounded sequence repetition."
        )

    def _safe_repeat(self, value: str | list[Any] | tuple[Any, ...], repeat_count: int) -> Any:
        """Safely repeat a sequence."""
        if repeat_count <= 0 or len(value) == 0:

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Make both operands of `+` the same numeric type or the same sequence type (str+str, list+list, tuple+tuple).
  2. Cast operands with the allowlisted builtins int(), float(), str() before adding.
  3. Verify the actual runtime types of the fields used in arithmetic.

Example fix

# before
VectorSearchOptions(filter="lambda x: x.count + x.suffix == 5")  # int + str
# after
VectorSearchOptions(filter="lambda x: x.count + int(x.suffix) == 5")
Defensive patterns

Strategy: try-catch

Try / catch

try:
    results = await collection.search(search_type=SearchType.VECTOR, options=opts)
except VectorStoreOperationException as e:
    logger.warning("filter rejected: %s", e.__cause__ or e)
    results = None

Prevention

When it happens

Trigger: A filter like `lambda x: x.tags + 1` (list + int), `lambda x: x.count + x.name` (int + str), or `lambda x: x.meta + x.other` where the fields are dicts.

Common situations: Assuming filter arithmetic is permissive; field type mismatches between the model and the filter; concatenating a numeric field with a string literal.

Related errors


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