huggingface/smolagents · warning · InterpreterError

Unary operation {expression.op.__class__.__name__} is not su

Error message

Unary operation {expression.op.__class__.__name__} is not supported.

What it means

evaluate_unaryop supports only USub (-), UAdd (+), Not (not) and Invert (~). Any other unary operator AST node raises InterpreterError naming the unsupported op class. In practice CPython only produces those four, so this is a defensive exhaustiveness branch.

Source

Thrown at src/smolagents/local_python_executor.py:413

def evaluate_unaryop(
    expression: ast.UnaryOp,
    state: dict[str, Any],
    static_tools: dict[str, Callable],
    custom_tools: dict[str, Callable],
    authorized_imports: list[str],
) -> Any:
    operand = evaluate_ast(expression.operand, state, static_tools, custom_tools, authorized_imports)
    if isinstance(expression.op, ast.USub):
        return -operand
    elif isinstance(expression.op, ast.UAdd):
        return operand
    elif isinstance(expression.op, ast.Not):
        return not operand
    elif isinstance(expression.op, ast.Invert):
        return ~operand
    else:
        raise InterpreterError(f"Unary operation {expression.op.__class__.__name__} is not supported.")


def evaluate_lambda(
    lambda_expression: ast.Lambda,
    state: dict[str, Any],
    static_tools: dict[str, Callable],
    custom_tools: dict[str, Callable],
    authorized_imports: list[str],
) -> Callable:
    args = [arg.arg for arg in lambda_expression.args.args]

    def lambda_func(*values: Any) -> Any:
        new_state = state.copy()
        for arg, value in zip(args, values):
            new_state[arg] = value
        return evaluate_ast(
            lambda_expression.body,
            new_state,

View on GitHub (pinned to 30bb116109)

Solutions

  1. Stick to the four standard unary ops (-, +, not, ~) in executed code
  2. Upgrade smolagents if a newer Python version added an operator and the executor was updated
  3. If feeding ASTs programmatically, normalize to supported ops before evaluation
Defensive patterns

Strategy: validation

Validate before calling

import ast
from smolagents.local_python_executor import evaluate_ast
for node in ast.walk(ast.parse(code)):
    if isinstance(node, ast.UnaryOp) and not isinstance(node.op, (ast.USub, ast.UAdd, ast.Not, ast.Invert)):
        raise ValueError('unsupported unary operator in AST')

Try / catch

from smolagents.local_python_executor import InterpreterError
try:
    evaluate_python(code)
except InterpreterError as e:
    if 'Unary operation' in str(e):
        raise NotImplementedError('rewrite the expression using -/+/not/~') from e

Prevention

When it happens

Trigger: Practically unreachable with standard Python source; would only fire if a new unary operator were added to the grammar or an AST is fed programmatically with an exotic ast.UnaryOp op node.

Common situations: Version skew: a future/alternate Python grammar emits a unary op node the executor doesn't recognize; hand-crafted AST passed to evaluate_ast directly instead of source code.

Related errors


AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28). Data as JSON: /api/errors/6f5caf2710099a26. Report an issue: GitHub.