microsoft/semantic-kernel · error · VectorStoreOperationException
Filter expression exceeds the maximum allowed complexity.
Error message
Filter expression exceeds the maximum allowed complexity.
What it means
Thrown by _parse_and_validate_filter (in_memory.py:770-771) when the number of AST nodes in the filter exceeds max_filter_ast_node_count (default 128). It guards against denial-of-service via enormous or deeply nested expressions and is checked during the allowlist walk.
Source
Thrown at python/semantic_kernel/connectors/in_memory.py:771
try:
tree = ast.parse(filter_str, mode="eval")
except SyntaxError as e:
raise VectorStoreOperationException(f"Filter string is not valid Python: {e}") from e
# Only allow lambda expressions at the top level
if not (isinstance(tree, ast.Expression) and isinstance(tree.body, ast.Lambda)):
raise VectorStoreOperationException(
"Filter string must be a lambda expression, e.g. 'lambda x: x.key == 1'"
)
# Get the lambda parameter name(s) to allow them as valid Name nodes
lambda_node = tree.body
lambda_param_names = {arg.arg for arg in lambda_node.args.args}
lambda_param_order = [arg.arg for arg in lambda_node.args.args]
# Walk the AST to validate all nodes against the allowlist
for node_count, node in enumerate(ast.walk(tree), start=1):
if node_count > self.max_filter_ast_node_count:
raise VectorStoreOperationException("Filter expression exceeds the maximum allowed complexity.")
node_type = type(node)
# Check if the node type is allowed
if node_type not in self.allowed_filter_ast_nodes:
raise VectorStoreOperationException(
f"AST node type '{node_type.__name__}' is not allowed in filter expressions."
)
# For Attribute nodes, validate that dangerous dunder attributes are not accessed
if isinstance(node, ast.Attribute) and node.attr in self.blocked_filter_attributes:
raise VectorStoreOperationException(
f"Access to attribute '{node.attr}' is not allowed in filter expressions. "
"This attribute could be used to escape the filter sandbox."
)
# For Name nodes, only allow the lambda parameter
if isinstance(node, ast.Name) and node.id not in lambda_param_names:View on GitHub (pinned to c028a0c7dc)
Solutions
- Simplify the filter expression.
- Split one large filter into a list of filters (options.filter accepts a list, OR-semantics).
- Move large constant data out of the filter and pass it as a callable closure.
- Raise max_filter_ast_node_count if the complexity is legitimate.
Example fix
# before
opts = VectorSearchOptions(filter="lambda x: " + " or ".join(f"x.id == {i}" for i in range(200)))
# after
ids = set(range(200))
opts = VectorSearchOptions(filter=lambda x: x.id in ids) Defensive patterns
Strategy: validation
Validate before calling
import ast
def node_count_within(expr: str, cap: int = 128) -> bool:
tree = ast.parse(expr, mode="eval")
return sum(1 for _ in ast.walk(tree)) <= cap 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
- Avoid generating filters with hundreds of OR clauses.
- Use membership (in) with a closure set instead of long OR chains.
- Split very large filters into a list.
When it happens
Trigger: A filter with deeply nested boolean logic, very long chained comparisons, or huge literal collections that expand the node count past 128.
Common situations: Auto-generating filters from large rule sets; combining many conditions with and/or; lowering the node cap.
Related errors
- Sequence repetition in filter expressions exceeds the maximu
- Collection literals in filter expressions exceed the maximum
- Sequence operations in filter expressions exceed the maximum
- Filter string exceeds the maximum allowed length.
- Attribute '{node.func.attr}' is not callable in filter expre
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/2e3d6393d231a585.
Report an issue: GitHub.