{"record":{"id":"395c432195cb96dd","repo":"microsoft/semantic-kernel","slug":"ast-node-type-node-type-name-is-not-allowe","errorCode":null,"errorMessage":"AST node type '{node_type.__name__}' is not allowed in filter expressions.","messagePattern":"AST node type '(.+?)' is not allowed in filter expressions\\.","errorType":"exception","errorClass":"VectorStoreOperationException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/in_memory.py","lineNumber":777,"sourceCode":"        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\n            # Check if the node type is allowed\n            if node_type not in self.allowed_filter_ast_nodes:\n                raise VectorStoreOperationException(\n                    f\"AST node type '{node_type.__name__}' is not allowed in filter expressions.\"\n                )\n\n            # For Attribute nodes, validate that dangerous dunder attributes are not accessed\n            if isinstance(node, ast.Attribute) and node.attr in self.blocked_filter_attributes:\n                raise VectorStoreOperationException(\n                    f\"Access to attribute '{node.attr}' is not allowed in filter expressions. \"\n                    \"This attribute could be used to escape the filter sandbox.\"\n                )\n\n            # For Name nodes, only allow the lambda parameter\n            if isinstance(node, ast.Name) and node.id not in lambda_param_names:\n                raise VectorStoreOperationException(\n                    f\"Use of name '{node.id}' is not allowed in filter expressions. \"\n                    f\"Only the lambda parameter(s) ({', '.join(lambda_param_names)}) can be used.\"\n                )\n\n            # For Call nodes, validate that only allowed functions are called","sourceCodeStart":759,"sourceCodeEnd":795,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/in_memory.py#L759-L795","documentation":"Thrown by _parse_and_validate_filter (in_memory.py:776-778) when the filter contains an AST node type not in allowed_filter_ast_nodes. Common offenders: ternary IfExp, comprehensions (ListComp/SetComp/DictComp/GeneratorExp), f-strings (JoinedStr/FormattedValue), Starred, walrus NamedExpr, Await, and Yield.","triggerScenarios":"Using a ternary (`a if c else b`), a comprehension (`[i for i in ...]`), an f-string, starred arguments, or a walrus assignment inside a filter lambda.","commonSituations":"Writing Pythonic one-liners that use comprehensions or ternaries; copy-pasting general expressions; expecting full Python in the sandbox.","solutions":["Rewrite using only allowed constructs: comparisons, boolean ops (and/or/not), calls to allowlisted functions, literals, basic arithmetic, subscript, and attribute access.","Replace a ternary with boolean short-circuit: `(c and a) or b`.","Replace a comprehension with an allowlisted builtin call such as any()/all() over a literal.","Precompute complex values outside the filter and pass them via a callable closure."],"exampleFix":"# before\nVectorSearchOptions(filter=\"lambda x: 'a' if x.f else 'b'\")      # IfExp not allowed\n# after\nVectorSearchOptions(filter=\"lambda x: (x.f and 'a') or 'b'\")        # boolean short-circuit","handlingStrategy":"validation","validationCode":"import ast\n\nSAFE_NODES = {\n    ast.Expression, ast.Lambda, ast.arguments, ast.arg, ast.Compare, ast.BoolOp,\n    ast.UnaryOp, ast.And, ast.Or, ast.Not, ast.Eq, ast.NotEq, ast.Lt, ast.LtE,\n    ast.Gt, ast.GtE, ast.In, ast.NotIn, ast.Is, ast.IsNot, ast.Name, ast.Load,\n    ast.Attribute, ast.Subscript, ast.Slice, ast.Constant, ast.List, ast.Tuple,\n    ast.Set, ast.Dict, ast.BinOp, ast.Add, ast.Sub, ast.Mult, ast.Div, ast.Mod,\n    ast.FloorDiv, ast.Call,\n}\n\ndef preflight_filter(expr: str, *, max_len: int = 2048, max_nodes: int = 128) -> None:\n    if len(expr) > max_len:\n        raise ValueError(\"filter too long\")\n    try:\n        tree = ast.parse(expr, mode=\"eval\")\n    except SyntaxError as e:\n        raise ValueError(f\"invalid python: {e}\") from e\n    if not (isinstance(tree, ast.Expression) and isinstance(tree.body, ast.Lambda)):\n        raise ValueError(\"filter must be a lambda expression\")\n    blocked = {\"__class__\", \"__globals__\", \"__subclasses__\", \"__builtins__\", \"__code__\"}\n    for n in ast.walk(tree):\n        if isinstance(n, ast.Attribute) and n.attr in blocked:\n            raise ValueError(f\"blocked attribute: {n.attr}\")\n        if type(n) not in SAFE_NODES:\n            raise ValueError(f\"disallowed node: {type(n).__name__}\")\n    if sum(1 for _ in ast.walk(tree)) > max_nodes:\n        raise ValueError(\"filter too complex\")\n","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":["Keep filters to comparisons, boolean ops, allowlisted calls, and literals.","Avoid ternaries, comprehensions, f-strings, and walrus in filter lambdas.","Move complex logic into a callable filter when the sandbox is too restrictive."],"tags":["filter","in-memory","security","validation","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}