microsoft/semantic-kernel · error · NotImplementedError

Unsupported BoolOp: {type(op)}

Error message

Unsupported BoolOp: {type(op)}

What it means

The MongoDB filter lambda parser handles boolean operations (`and`, `or`) by translating them to `$and`/`$or`. This NotImplementedError is raised when the lambda contains a BoolOp whose op type is neither `ast.And` nor `ast.Or`. In standard Python the only two BoolOp types are `and` and `or`, so this error is extremely rare under normal usage but serves as a defensive guard for malformed or non-standard AST input.

Source

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

                    case ast.NotEq():
                        return {left: {"$ne": right}}
                    case ast.Gt():
                        return {left: {"$gt": right}}
                    case ast.GtE():
                        return {left: {"$gte": right}}
                    case ast.Lt():
                        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(

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Restructure the filter to use only standard `and` / `or` boolean combinators in the lambda.
  2. Avoid programmatic AST construction for filter predicates; use plain Python lambdas.
  3. Split complex boolean logic into nested `and`/`or` sub-expressions.
Defensive patterns

Strategy: validation

Validate before calling

import ast, inspect
def validate_bool_ops(func):
    tree = ast.parse(inspect.getsource(func))
    for node in ast.walk(tree):
        if isinstance(node, ast.BoolOp) and not isinstance(node.op, (ast.And, ast.Or)):
            raise ValueError(f"Unsupported boolean operator: {type(node.op).__name__}")

Try / catch

try:
    results = await collection.search(filter=my_lambda)
except NotImplementedError as e:
    if "Unsupported BoolOp" in str(e):
        # rewrite using only 'and' / 'or'
        ...

Prevention

When it happens

Trigger: The lambda predicate contains a boolean operation node whose operator is not `and` or `or`. This is essentially unreachable under normal Python lambdas but could surface with AST manipulation, code generation tools, or corrupted lambda objects.

Common situations: Rare in practice. Could arise from metaprogramming or code-generation frameworks that synthesize AST nodes not produced by the standard Python compiler.

Related errors


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