{"record":{"id":"78d12451e15dc7d0","repo":"microsoft/semantic-kernel","slug":"invert-operation-is-not-supported-78d124","errorCode":null,"errorMessage":"Invert operation is not supported.","messagePattern":"Invert operation is not supported\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/azure_cosmos_db.py","lineNumber":944,"sourceCode":"                            return f\"{left} >= {right}\"\n                        case ast.Lt():\n                            return f\"{left} < {right}\"\n                        case ast.LtE():\n                            return f\"{left} <= {right}\"\n                    raise NotImplementedError(f\"Unsupported operator: {type(op)}\")\n                case ast.BoolOp():\n                    op_str = \"AND\" if isinstance(node.op, ast.And) else \"OR\"\n                    return \"(\" + f\" {op_str} \".join([parse(v) for v in node.values]) + \")\"\n                case ast.UnaryOp():\n                    match node.op:\n                        case ast.Not():\n                            return f\"NOT ({parse(node.operand)})\"\n                        case ast.UAdd():\n                            return f\"+{parse(node.operand)}\"\n                        case ast.USub():\n                            return f\"-{parse(node.operand)}\"\n                        case ast.Invert():\n                            raise NotImplementedError(\"Invert operation is not supported.\")\n                    raise NotImplementedError(f\"Unsupported unary operator: {type(node.op)}\")\n                case ast.Attribute():\n                    # Cosmos DB: c.field_name\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 f\"c.{node.attr}\"\n                case ast.Name():\n                    # Could be a variable or constant; not supported\n                    raise NotImplementedError(\"Constants or variables are not supported, use a value or attribute.\")\n                case ast.Constant():\n                    # Bind strings as query parameters to avoid SQL injection. Numbers and null\n                    # cannot carry injection, so they are inlined.\n                    if isinstance(node.value, str):\n                        name = f\"@filter_p{len(parameters)}\"\n                        parameters.append({\"name\": name, \"value\": node.value})\n                        return name","sourceCodeStart":926,"sourceCodeEnd":962,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/azure_cosmos_db.py#L926-L962","documentation":"The AST translator for unary operators handles Not, UAdd, and USub, but explicitly rejects bitwise inversion (~, ast.Invert) with NotImplementedError. Cosmos SQL has no direct equivalent for Python's bitwise-not on a column, so the translator refuses to render it rather than producing invalid SQL.","triggerScenarios":"A filter lambda contains the '~' operator, e.g. lambda x: ~x.flags or ~x.active. The UnaryOp branch matches ast.Invert at azure_cosmos_db.py:943-944 and raises immediately.","commonSituations":"Copy-pasting a boolean/bitwise expression into a search filter, or attempting to reuse a predicate written for in-memory Python filtering that relies on '~' for bitwise negation.","solutions":["Remove the '~' operator from filter lambdas; express negation with 'not' (ast.Not) or '!=' instead.","Reformulate bitwise logic using supported comparison/logical operators that Cosmos SQL can express.","Keep filter lambdas limited to the operator subset the translator documents (==, !=, <, >, <=, >=, in, not in, and, or, not, +, -)."],"exampleFix":"// before\noptions = VectorSearchOptions(filter=lambda x: ~x.active)\n// after\noptions = VectorSearchOptions(filter=lambda x: not x.active)\n","handlingStrategy":"validation","validationCode":"import ast\ntree = ast.parse(filter_src, mode=\"eval\")\nfor node in ast.walk(tree):\n    if isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Invert):\n        raise ValueError(\"Filter uses unsupported '~' (Invert) operator\")","typeGuard":"def has_no_invert(node: ast.AST) -> bool:\n    return not any(isinstance(n, ast.Invert) for n in ast.walk(node))","tryCatchPattern":null,"preventionTips":["Never use '~' in filter lambdas; use 'not' for logical negation.","Avoid bitwise operators in search filter expressions."],"tags":["azure-cosmos-db","filter","ast","lambda"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}