{"record":{"id":"13318b886e7387fb","repo":"microsoft/semantic-kernel","slug":"filter-string-is-not-valid-python-e","errorCode":null,"errorMessage":"Filter string is not valid Python: {e}","messagePattern":"Filter string is not valid Python: (.+?)","errorType":"exception","errorClass":"VectorStoreOperationException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/in_memory.py","lineNumber":756,"sourceCode":"            for filter in callable_filters:\n                if self._run_filter(filter, record):\n                    filtered_records[key] = record\n        return filtered_records\n\n    def _parse_and_validate_filter(self, filter_str: str) -> Callable:\n        \"\"\"Parse and validate a string filter as a lambda expression, then return the callable.\n\n        Uses an allowlist approach - only explicitly permitted AST node types and function names\n        are allowed. This can be customized by overriding `allowed_filter_ast_nodes` and\n        `allowed_filter_functions` class attributes.\n        \"\"\"\n        if len(filter_str) > self.max_filter_source_length:\n            raise VectorStoreOperationException(\"Filter string exceeds the maximum allowed length.\")\n\n        try:\n            tree = ast.parse(filter_str, mode=\"eval\")\n        except SyntaxError as e:\n            raise VectorStoreOperationException(f\"Filter string is not valid Python: {e}\") from e\n\n        # Only allow lambda expressions at the top level\n        if not (isinstance(tree, ast.Expression) and isinstance(tree.body, ast.Lambda)):\n            raise VectorStoreOperationException(\n                \"Filter string must be a lambda expression, e.g. 'lambda x: x.key == 1'\"\n            )\n\n        # Get the lambda parameter name(s) to allow them as valid Name nodes\n        lambda_node = tree.body\n        lambda_param_names = {arg.arg for arg in lambda_node.args.args}\n        lambda_param_order = [arg.arg for arg in lambda_node.args.args]\n        # Walk the AST to validate all nodes against the allowlist\n        for node_count, node in enumerate(ast.walk(tree), start=1):\n            if node_count > self.max_filter_ast_node_count:\n                raise VectorStoreOperationException(\"Filter expression exceeds the maximum allowed complexity.\")\n\n            node_type = type(node)\n","sourceCodeStart":738,"sourceCodeEnd":774,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/in_memory.py#L738-L774","documentation":"Thrown by _parse_and_validate_filter (in_memory.py:755-756) when ast.parse(filter_str, mode='eval') raises SyntaxError. The filter string is not syntactically valid Python.","triggerScenarios":"A lambda with a syntax error: unbalanced parentheses, a trailing operator, an incomplete expression, stray characters, or invalid keyword usage.","commonSituations":"Hand-writing filter strings; templating filters with f-strings that produce malformed output; truncation of a filter in transit.","solutions":["Fix the Python syntax of the lambda.","Test the lambda body in a Python REPL or with ast.parse before passing it.","If generating filters dynamically, compile/parse them during construction to fail fast."],"exampleFix":"# before\nVectorSearchOptions(filter=\"lambda x: x.id ==\")   # incomplete expression\n# after\nVectorSearchOptions(filter=\"lambda x: x.id == 1\")","handlingStrategy":"validation","validationCode":"import ast\ndef is_valid_python(expr: str) -> bool:\n    try:\n        ast.parse(expr, mode=\"eval\")\n        return True\n    except SyntaxError:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    results = await collection.search(search_type=SearchType.VECTOR, options=opts)\nexcept VectorStoreOperationException as e:\n    logger.warning(\"filter rejected: %s\", e.__cause__ or e)\n    results = None","preventionTips":["Parse-test filter strings before sending them to search.","Avoid hand-concatenating filter fragments; build with care."],"tags":["filter","in-memory","syntax","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}