microsoft/semantic-kernel · error · VectorStoreOperationException

Collection literals in filter expressions exceed the maximum

Error message

Collection literals in filter expressions exceed the maximum allowed size.

What it means

Thrown by _ensure_literal_collection_size (in_memory.py:389-391) during evaluation when a list/tuple/set/dict literal has more than max_filter_literal_collection_size elements (default 256). Parse-time also enforces this (in_memory.py:814-825), so the evaluator check is defense-in-depth for the case where limits differ or parsing was bypassed.

Source

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

    def _safe_numeric_operation(
        self,
        operator_node: ast.AST,
        left: Any,
        right: Any,
        operation: Callable[[float | int, float | int], Any],
    ) -> Any:
        """Safely evaluate a numeric binary operation."""
        if not isinstance(left, (int, float)) or not isinstance(right, (int, float)):
            raise VectorStoreOperationException(
                f"Operator '{type(operator_node).__name__}' is only allowed for numeric values in filter expressions."
            )
        return operation(left, right)

    def _ensure_literal_collection_size(self, size: int) -> None:
        """Reject excessively large literal collections."""
        if size > self._max_literal_collection_size:
            raise VectorStoreOperationException(
                "Collection literals in filter expressions exceed the maximum allowed size."
            )

    def _ensure_sequence_result_size(
        self,
        left: str | list[Any] | tuple[Any, ...],
        right: str | list[Any] | tuple[Any, ...],
        operation: Callable[[Any, Any], Any],
    ) -> Any:
        """Reject oversized sequence concatenation results."""
        if len(left) + len(right) > self._max_sequence_repeat_size:
            raise VectorStoreOperationException(
                "Sequence operations in filter expressions exceed the maximum allowed size."
            )
        return operation(left, right)

    def _evaluate_optional(self, node: ast.AST | None, context: Mapping[str, Any]) -> Any:
        """Evaluate an optional AST node."""

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Shrink the inline collection to at most max_filter_literal_collection_size elements.
  2. Pass options.filter as a Python callable instead of a string to bypass string parsing limits where appropriate.
  3. Raise max_filter_literal_collection_size on the collection if the large literal is legitimate.
  4. Split one big filter into a list of smaller filters (options.filter accepts a list).

Example fix

# before
VectorSearchOptions(filter=f"lambda x: x.id in {list(range(300))}")  # > 256 elements
# after
allowed = {i for i in range(300)}
VectorSearchOptions(filter=lambda x: x.id in allowed)  # callable bypasses parse limits
Defensive patterns

Strategy: validation

Validate before calling

def collection_within_limit(expr: str, cap: int = 256) -> bool:
    import ast
    tree = ast.parse(expr, mode="eval")
    for n in ast.walk(tree):
        if isinstance(n, (ast.List, ast.Tuple, ast.Set)) and len(n.elts) > cap:
            return False
        if isinstance(n, ast.Dict) and len(n.keys) > cap:
            return False
    return True

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 containing a literal collection larger than 256 elements, e.g. `lambda x: x.id in [1, 2, ..., 300]`, or a relaxed/custom parse path.

Common situations: Pasting a large allowlist/ID set inline into a filter; migrating from a version with different limits.

Related errors


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