From 9414ccfddd92e95a65356064dddf9b6c5d8495c4 Mon Sep 17 00:00:00 2001 From: Harshil Kaneria Date: Tue, 10 Nov 2026 20:20:27 +0530 Subject: [PATCH] Python: fold negative literals in InMemoryCollection string filters InMemoryCollection parses string filters itself and checks them against an AST allowlist that doesn't include USub, so a filter such as 'lambda x: x.price > -5' was rejected. The fix for #14571 folds negative literals in _build_filter, which InMemoryCollection doesn't use. Apply the same _FoldUnaryNumericConstants transform before validation. Unary minus on anything other than a numeric literal is still rejected. --- .../semantic_kernel/connectors/in_memory.py | 5 ++++ .../unit/connectors/memory/test_in_memory.py | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/python/semantic_kernel/connectors/in_memory.py b/python/semantic_kernel/connectors/in_memory.py index a1f23ac200dd..45b6d2f4311e 100644 --- a/python/semantic_kernel/connectors/in_memory.py +++ b/python/semantic_kernel/connectors/in_memory.py @@ -24,6 +24,7 @@ VectorStore, VectorStoreCollection, VectorStoreCollectionDefinition, + _FoldUnaryNumericConstants, ) from semantic_kernel.exceptions import VectorSearchExecutionException, VectorStoreModelValidationError from semantic_kernel.exceptions.vector_store_exceptions import VectorStoreModelException, VectorStoreOperationException @@ -790,6 +791,10 @@ def _parse_and_validate_filter(self, filter_str: str) -> Callable: except SyntaxError as e: raise VectorStoreOperationException(f"Filter string is not valid Python: {e}") from e + # Fold negative literals such as `-5` into constants, as other connectors do (#14571), + # so they pass the allowlist without allowing unary minus in general. + tree = _FoldUnaryNumericConstants().visit(tree) + # Only allow lambda expressions at the top level if not (isinstance(tree, ast.Expression) and isinstance(tree.body, ast.Lambda)): raise VectorStoreOperationException( diff --git a/python/tests/unit/connectors/memory/test_in_memory.py b/python/tests/unit/connectors/memory/test_in_memory.py index bdd1d4b90b7f..a151e65e32a3 100644 --- a/python/tests/unit/connectors/memory/test_in_memory.py +++ b/python/tests/unit/connectors/memory/test_in_memory.py @@ -349,6 +349,29 @@ async def test_multiple_callable_filters_are_combined_with_and(collection): assert list(results) == ["1"] +@mark.parametrize( + "filter_str, expected", + [ + ("lambda x: x.vector[0] > -1", ["1"]), + ("lambda x: x.vector[0] == -2.5", ["2"]), + ("lambda x: -3 < x.vector[0] < -2", ["2"]), + ], +) +async def test_string_filter_with_negative_literal(collection, filter_str, expected): + # Python parses -1 as UnaryOp(USub, Constant(1)), which the allowlist rejects unless it is folded (#14571). + await collection.upsert([ + {"id": "1", "vector": [1, 2, 3, 4, 5]}, + {"id": "2", "vector": [-2.5, 4, 3, 2, 1]}, + ]) + results = collection._get_filtered_records(type("opt", (), {"filter": filter_str})()) + assert list(results) == expected + + +async def test_string_filter_still_rejects_unary_minus_on_a_field(collection): + with raises(VectorStoreOperationException, match="'USub' is not allowed"): + collection._get_filtered_records(type("opt", (), {"filter": "lambda x: -x.vector[0] > 1"})()) + + async def test_invalid_filter_not_lambda(collection): with raises(VectorStoreOperationException, match="must be a lambda expression"): collection._get_filtered_records(type("opt", (), {"filter": "x.id == '1'"})())