microsoft/semantic-kernel · error · NotImplementedError

Unary +, -, ~ are not supported in MongoDB filters.

Error message

Unary +, -, ~ are not supported in MongoDB filters.

What it means

The MongoDB filter lambda parser handles unary operations but only supports logical `not` (translated to `$not`). Unary plus (`+x`), unary minus (`-x`), and bitwise invert (`~x`) are explicitly rejected because MongoDB filter documents have no meaningful representation for numeric sign-flipping or bitwise negation in a query predicate. The NotImplementedError is raised immediately when such a unary op is encountered.

Source

Thrown at python/semantic_kernel/connectors/mongodb.py:494

                        return {left: {"$lt": right}}
                    case ast.LtE():
                        return {left: {"$lte": right}}
                raise NotImplementedError(f"Unsupported operator: {type(op)}")
            case ast.BoolOp():
                op = node.op  # type: ignore
                values = [self._lambda_parser(v) for v in node.values]
                if isinstance(op, ast.And):
                    return {"$and": values}
                if isinstance(op, ast.Or):
                    return {"$or": values}
                raise NotImplementedError(f"Unsupported BoolOp: {type(op)}")
            case ast.UnaryOp():
                match node.op:
                    case ast.Not():
                        operand = self._lambda_parser(node.operand)
                        return {"$not": operand}
                    case ast.UAdd() | ast.USub() | ast.Invert():
                        raise NotImplementedError("Unary +, -, ~ are not supported in MongoDB filters.")
            case ast.Attribute():
                # Only allow attributes that are in the data model
                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 node.attr
            case ast.Name():
                # Only allow names that are in the data model
                if node.id not in self.definition.storage_names:
                    raise VectorStoreOperationException(
                        f"Field '{node.id}' not in data model (storage property names are used)."
                    )
                return node.id
            case ast.Constant():
                return node.value
        raise NotImplementedError(f"Unsupported AST node: {type(node)}")

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Move the sign/arithmetical transformation outside the filter; pre-compute the comparison value and compare the raw field against it, e.g. instead of `-x.amount > -10` use `x.amount < 10`.
  2. Avoid bitwise operations in filter lambdas entirely; filter flags in application code after the query.
  3. Restructure so the lambda only compares model fields directly against constant or computed values.

Example fix

// before
collection.search(filter=lambda x: -x.score > threshold)
// after
collection.search(filter=lambda x: x.score < -threshold)
Defensive patterns

Strategy: validation

Validate before calling

import ast, inspect
def has_unsupported_unary(func):
    tree = ast.parse(inspect.getsource(func))
    for node in ast.walk(tree):
        if isinstance(node, ast.UnaryOp) and isinstance(node.op, (ast.UAdd, ast.USub, ast.Invert)):
            return True
    return False

Try / catch

try:
    results = await collection.search(filter=my_lambda)
except NotImplementedError as e:
    if "Unary" in str(e):
        # move sign-flip / bitwise logic outside the lambda
        ...

Prevention

When it happens

Trigger: Writing a filter lambda that applies unary minus, plus, or invert to an operand, e.g. `lambda x: -x.amount > 0` or `lambda x: ~x.flags`.

Common situations: Developer tries to negate a numeric field inline in the filter predicate, or uses bitwise complement on a flags field. Common when porting filter logic from application code that performs arithmetic on field values before comparing.

Related errors


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