microsoft/semantic-kernel · error · NotImplementedError

Invert operation is not supported.

Error message

Invert operation is not supported.

What it means

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.

Source

Thrown at python/semantic_kernel/connectors/azure_cosmos_db.py:944

                            return f"{left} >= {right}"
                        case ast.Lt():
                            return f"{left} < {right}"
                        case ast.LtE():
                            return f"{left} <= {right}"
                    raise NotImplementedError(f"Unsupported operator: {type(op)}")
                case ast.BoolOp():
                    op_str = "AND" if isinstance(node.op, ast.And) else "OR"
                    return "(" + f" {op_str} ".join([parse(v) for v in node.values]) + ")"
                case ast.UnaryOp():
                    match node.op:
                        case ast.Not():
                            return f"NOT ({parse(node.operand)})"
                        case ast.UAdd():
                            return f"+{parse(node.operand)}"
                        case ast.USub():
                            return f"-{parse(node.operand)}"
                        case ast.Invert():
                            raise NotImplementedError("Invert operation is not supported.")
                    raise NotImplementedError(f"Unsupported unary operator: {type(node.op)}")
                case ast.Attribute():
                    # Cosmos DB: c.field_name
                    if node.attr not in self.definition.storage_names:
                        raise VectorStoreOperationException(
                            f"Field '{node.attr}' not in data model (storage property names are used)."
                        )
                    return f"c.{node.attr}"
                case ast.Name():
                    # Could be a variable or constant; not supported
                    raise NotImplementedError("Constants or variables are not supported, use a value or attribute.")
                case ast.Constant():
                    # Bind strings as query parameters to avoid SQL injection. Numbers and null
                    # cannot carry injection, so they are inlined.
                    if isinstance(node.value, str):
                        name = f"@filter_p{len(parameters)}"
                        parameters.append({"name": name, "value": node.value})
                        return name

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Remove the '~' operator from filter lambdas; express negation with 'not' (ast.Not) or '!=' instead.
  2. Reformulate bitwise logic using supported comparison/logical operators that Cosmos SQL can express.
  3. Keep filter lambdas limited to the operator subset the translator documents (==, !=, <, >, <=, >=, in, not in, and, or, not, +, -).

Example fix

// before
options = VectorSearchOptions(filter=lambda x: ~x.active)
// after
options = VectorSearchOptions(filter=lambda x: not x.active)
Defensive patterns

Strategy: validation

Validate before calling

import ast
tree = ast.parse(filter_src, mode="eval")
for node in ast.walk(tree):
    if isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Invert):
        raise ValueError("Filter uses unsupported '~' (Invert) operator")

Type guard

def has_no_invert(node: ast.AST) -> bool:
    return not any(isinstance(n, ast.Invert) for n in ast.walk(node))

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/78d12451e15dc7d0. Report an issue: GitHub.