{"record":{"id":"b38954d99a91c287","repo":"microsoft/semantic-kernel","slug":"unsupported-ast-node-type-node","errorCode":null,"errorMessage":"Unsupported AST node: {type(node)}","messagePattern":"Unsupported AST node: (.+?)","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/mongodb.py","lineNumber":511,"sourceCode":"                    case ast.UAdd() | ast.USub() | ast.Invert():\n                        raise NotImplementedError(\"Unary +, -, ~ are not supported in MongoDB filters.\")\n            case ast.Attribute():\n                # Only allow attributes that are in the data model\n                if node.attr not in self.definition.storage_names:\n                    raise VectorStoreOperationException(\n                        f\"Field '{node.attr}' not in data model (storage property names are used).\"\n                    )\n                return node.attr\n            case ast.Name():\n                # Only allow names that are in the data model\n                if node.id not in self.definition.storage_names:\n                    raise VectorStoreOperationException(\n                        f\"Field '{node.id}' not in data model (storage property names are used).\"\n                    )\n                return node.id\n            case ast.Constant():\n                return node.value\n        raise NotImplementedError(f\"Unsupported AST node: {type(node)}\")\n\n    @override\n    def _get_record_from_result(self, result: dict[str, Any]) -> dict[str, Any]:\n        return result\n\n    @override\n    def _get_score_from_result(self, result: dict[str, Any]) -> float | None:\n        return result.get(MONGODB_SCORE_FIELD)\n\n    @override\n    async def __aexit__(self, exc_type, exc_value, traceback) -> None:\n        \"\"\"Exit the context manager.\"\"\"\n        if self.managed_client:\n            await self.mongo_client.close()\n\n    async def __aenter__(self) -> Self:\n        \"\"\"Enter the context manager.\"\"\"\n        await self.mongo_client.aconnect()","sourceCodeStart":493,"sourceCodeEnd":529,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/mongodb.py#L493-L529","documentation":"This is the final fallthrough in the MongoDB lambda parser's match statement. After handling Compare, BoolOp, UnaryOp, Attribute, Name, and Constant nodes, any other AST node type reaches this NotImplementedError. It means the filter lambda contains a Python expression construct that the parser cannot translate into a MongoDB query at all — e.g. function calls, list comprehensions, ternary expressions, subscripts, or arithmetic operations.","triggerScenarios":"A filter lambda uses any unsupported Python expression: function calls (`len(x.field)`), comprehensions (`[v for v in x.items]`), ternary (`a if cond else b`), subscripting (`x.items[0]`), arithmetic (`x.a + x.b`), walrus operators, or lambda definitions inside the filter.","commonSituations":"Developer embeds business logic (function calls, comprehensions, arithmetic) directly in the filter lambda expecting it to be evaluated, not realizing the parser statically walks the AST rather than executing the lambda.","solutions":["Restrict filter lambdas to direct field-vs-value comparisons combined with `and`/`or`/`not`.","Move any computation (function calls, arithmetic, comprehensions) outside the lambda; compute the result and compare the raw field to it.","Replace ternary logic with explicit `and`/`or` boolean combinations of supported comparisons."],"exampleFix":"// before\ncollection.search(filter=lambda x: len(x.tags) > 0)\n// after\n# pre-check or restructure; the parser only supports field-level comparisons\ncollection.search(filter=lambda x: x.tag_count > 0)","handlingStrategy":"validation","validationCode":"SUPPORTED_NODES = (ast.Compare, ast.BoolOp, ast.UnaryOp, ast.Attribute, ast.Name, ast.Constant)\nimport ast, inspect\ndef validate_filter_ast(func):\n    tree = ast.parse(inspect.getsource(func))\n    for node in ast.walk(tree):\n        if not isinstance(node, SUPPORTED_NODES) and not isinstance(node, (ast.Lambda, ast.arg, ast.arguments, ast.Load, ast.cmpop, ast.boolop, ast.unaryop, ast.expr_context)):\n            raise ValueError(f\"Unsupported AST node in filter: {type(node).__name__}\")","typeGuard":null,"tryCatchPattern":"try:\n    results = await collection.search(filter=my_lambda)\nexcept NotImplementedError as e:\n    if \"Unsupported AST node\" in str(e):\n        # remove function calls, comprehensions, arithmetic, ternary from the lambda\n        ...","preventionTips":["Keep filter lambdas to pure field-vs-value comparisons with and/or/not.","Do not embed function calls, list comprehensions, arithmetic, or ternary expressions in filter lambdas.","Remember the parser walks the AST statically — it does not execute the lambda."],"tags":["mongodb","filter","lambda","ast","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}