microsoft/semantic-kernel · error · VectorStoreOperationException
Filter expected {len(lambda_param_order)} argument(s), but r
Error message
Filter expected {len(lambda_param_order)} argument(s), but received {len(args)}. What it means
Thrown by the filter_callable closure returned from _parse_and_validate_filter (in_memory.py:845) when it is invoked with a number of arguments that does not match the lambda's declared parameter count (lambda_param_order). Internally _run_filter calls the filter with exactly one argument (the wrapped record), so a string lambda declaring more than one parameter will mismatch.
Source
Thrown at python/semantic_kernel/connectors/in_memory.py:845
if (
isinstance(node, ast.Constant)
and isinstance(node.value, str)
and len(node.value) > self.max_filter_literal_collection_size
):
raise VectorStoreOperationException(
"String literals in filter expressions exceed the maximum allowed size."
)
evaluator = _SafeFilterEvaluator(
direct_call_functions=self.direct_filter_functions,
blocked_attributes=self.blocked_filter_attributes,
max_literal_collection_size=self.max_filter_literal_collection_size,
max_sequence_repeat_size=self.max_filter_sequence_repeat_size,
)
def filter_callable(*args: Any) -> Any:
if len(args) != len(lambda_param_order):
raise VectorStoreOperationException(
f"Filter expected {len(lambda_param_order)} argument(s), but received {len(args)}."
)
context = {
name: ReadOnlyAttributeDict._wrap_value(value)
for name, value in zip(lambda_param_order, args, strict=True)
}
return evaluator.evaluate(lambda_node.body, context)
return filter_callable
def _run_filter(self, filter: Callable, record: AttributeDict[TAKey, TAValue]) -> bool:
"""Run the filter on the record, supporting attribute access."""
try:
return filter(ReadOnlyAttributeDict(record))
except Exception as e:
raise VectorStoreOperationException(f"Error running filter: {e}") from e
@overrideView on GitHub (pinned to c028a0c7dc)
Solutions
- Use a single-parameter lambda: "lambda x: x.age > 18".
- Inline any second value as a literal in the expression.
- If you genuinely need multi-arg semantics, pass a Python callable and arrange the call site yourself (note _run_filter still passes one record).
- Keep string filters to the documented one-parameter shape.
Example fix
# before filter = "lambda x, threshold: x.age > threshold" # after filter = "lambda x: x.age > 18"
Defensive patterns
Strategy: validation
Validate before calling
import ast
def lambda_param_count(filter_str: str) -> int:
tree = ast.parse(filter_str, mode="eval")
assert isinstance(tree.body, ast.Lambda)
return len(tree.body.args.args)
# the in-memory evaluator only supplies the record, so require exactly 1:
assert lambda_param_count(filter_str) == 1 Type guard
def is_single_param_lambda(filter_str: str) -> bool:
try:
tree = ast.parse(filter_str, mode="eval")
except SyntaxError:
return False
return (
isinstance(tree, ast.Expression)
and isinstance(tree.body, ast.Lambda)
and len(tree.body.args.args) == 1
) Try / catch
try:
opts = VectorSearchOptions(filter=filter_str)
except VectorStoreOperationException as ex:
if "argument(s), but received" in str(ex):
# rewrite to a single-param lambda with the second value inlined
filter_str = filter_str.replace("lambda x, y:", "lambda x:").replace(", y", "") Prevention
- Always write string filters as a single-parameter lambda.
- Inline any second value as a literal rather than a parameter.
- Validate the param count before passing to search.
- Remember _run_filter only ever passes the one record.
When it happens
Trigger: A string filter lambda with multiple parameters, e.g. "lambda x, y: x.age > y", passed via VectorSearchOptions.filter. The evaluator only ever supplies the single record, so len(args)=1 != len(lambda_param_order)=2 at in_memory.py:844.
Common situations: Writing a two-argument lambda expecting an extra context parameter; copy-pasting a lambda signature from elsewhere; misunderstanding that the filter receives only the record.
Related errors
- Unsupported operator: {type(op)}
- Invert operation is not supported.
- Constants or variables are not supported, use a value or att
- Unsupported constant type: {type(node.value)}
- Unsupported operator: {type(op)}
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/58c4dca023c7365e.
Report an issue: GitHub.