{"record":{"id":"0bbb9fe312fbdd18","repo":"microsoft/semantic-kernel","slug":"invert-operation-is-not-supported","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_ai_search.py","lineNumber":711,"sourceCode":"                        return f\"{left} gt {right}\"\n                    case ast.GtE():\n                        return f\"{left} ge {right}\"\n                    case ast.Lt():\n                        return f\"{left} lt {right}\"\n                    case ast.LtE():\n                        return f\"{left} le {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([self._lambda_parser(v) for v in node.values]) + \")\"\n            case ast.UnaryOp():\n                match node.op:\n                    case ast.UAdd():\n                        return f\"+{self._lambda_parser(node.operand)}\"\n                    case ast.USub():\n                        return f\"-{self._lambda_parser(node.operand)}\"\n                    case ast.Invert():\n                        raise NotImplementedError(\"Invert operation is not supported.\")\n                    case ast.Not():\n                        return f\"not {self._lambda_parser(node.operand)}\"\n            case ast.Attribute():\n                # Support nested property chains\n                return _parse_attribute_chain(node)\n            case ast.Name():\n                raise NotImplementedError(\"Constants are not supported, make sure to use a value or a attribute.\")\n            case ast.Constant():\n                value = node.value\n                if isinstance(value, str):\n                    return \"'\" + value.replace(\"'\", \"''\") + \"'\"\n                if isinstance(value, bytes):\n                    return \"'\" + value.decode(\"utf-8\").replace(\"'\", \"''\") + \"'\"\n                if isinstance(value, bool):\n                    return str(value).lower()\n                if value is None:\n                    return \"null\"\n                if isinstance(value, (int, float)):","sourceCodeStart":693,"sourceCodeEnd":729,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/azure_ai_search.py#L693-L729","documentation":"Raised in the ast.UnaryOp branch of _lambda_parser when the unary operator is ast.Invert (the Python bitwise NOT, ~). Azure AI Search's OData filter expression language has no bitwise-invert equivalent, so the parser explicitly rejects it with NotImplementedError rather than producing a meaningless query.","triggerScenarios":"Using the ~ operator in a filter lambda, e.g. lambda x: ~x.flag == -2, or ~ applied to a field/operand anywhere in the filter expression.","commonSituations":"Accidentally using ~ (bitwise not) when ~ was meant on a non-filter object; copy-pasting numeric/bitmask logic into a vector-store filter.","solutions":["Remove the ~ operator from filter lambdas; OData filters do not support bitwise inversion.","If you need bitmask semantics, precompute the inverted value and compare with == / != against the field.","Use 'not' (logical) instead of '~' (bitwise) if you intended logical negation of a boolean."],"exampleFix":"// before\noptions.filter = lambda x: ~x.flag == -2\n\n// after\noptions.filter = lambda x: x.flag == 1   # precomputed value, no ~","handlingStrategy":"validation","validationCode":"import ast\n\ndef filter_has_no_invert(filter_lambda) -> None:\n    tree = ast.parse(ast.getsource(filter_lambda), mode=\"exec\")\n    for node in ast.walk(tree):\n        assert not (isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Invert)), \\\n            \"Bitwise invert (~) is not supported in OData filters\"\n\nfilter_has_no_invert(opts.filter)","typeGuard":null,"tryCatchPattern":"try:\n    res = await collection.search(values=q, options=opts)\nexcept NotImplementedError as e:\n    if \"Invert operation is not supported\" in str(e):\n        opts.filter = lambda x: x.flag == 1  # precomputed, no ~\n        res = await collection.search(values=q, options=opts)\n    raise","preventionTips":["Never use ~ in filter lambdas; OData has no bitwise invert.","Use 'not' for logical negation, and precompute inverted numeric values.","Lint filters for ast.Invert before passing them to search."],"tags":["filter","odata","lambda-parser","azure-ai-search"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}