huggingface/smolagents · error · InterpreterError
{expression.__class__.__name__} is not supported.
Error message
{expression.__class__.__name__} is not supported. What it means
smolagents' local Python executor only implements a whitelist of AST node types; any statement or expression outside that whitelist (e.g. global, nonlocal, async constructs, match statements, walrus in unsupported positions) hits the fallback branch and raises InterpreterError with the node class name. This is a deliberate safety limitation of the sandboxed interpreter used to run agent code actions. The error names the exact unsupported AST class to tell you what construct to rewrite.
Source
Thrown at src/smolagents/local_python_executor.py:1569
elif isinstance(expression, ast.Try):
return evaluate_try(expression, *common_params)
elif isinstance(expression, ast.Raise):
return evaluate_raise(expression, *common_params)
elif isinstance(expression, ast.Assert):
return evaluate_assert(expression, *common_params)
elif isinstance(expression, ast.With):
return evaluate_with(expression, *common_params)
elif isinstance(expression, ast.Set):
return set((evaluate_ast(elt, *common_params) for elt in expression.elts))
elif isinstance(expression, ast.Return):
raise ReturnException(evaluate_ast(expression.value, *common_params) if expression.value else None)
elif isinstance(expression, ast.Pass):
return None
elif isinstance(expression, ast.Delete):
return evaluate_delete(expression, *common_params)
else:
# For now we refuse anything else. Let's add things as we need them.
raise InterpreterError(f"{expression.__class__.__name__} is not supported.")
class FinalAnswerException(BaseException):
"""Exception raised when final_answer is called.
Inherits from BaseException instead of Exception to prevent being caught
by generic `except Exception` clauses in agent-generated code.
"""
def __init__(self, value):
self.value = value
def evaluate_python_code(
code: str,
static_tools: dict[str, Callable] | None = None,
custom_tools: dict[str, Callable] | None = None,
state: dict[str, Any] | None = None,View on GitHub (pinned to 30bb116109)
Solutions
- Rewrite the code action to avoid the unsupported construct named in the message (e.g. replace `match/case` with if/elif, avoid `global`)
- Check the full list of supported node types in local_python_executor.py evaluate_ast and constrain your prompt/tool descriptions accordingly
- Use E2BSandboxExecutor or DockerExecutor instead of the local interpreter for full Python support
- If a common construct is genuinely needed, open a feature request or subclass the interpreter
Example fix
# before
code = "match x:
case 1: y = 'a'
case _: y = 'b'"
evaluate_python_code(code, state=state)
# after
code = "y = 'a' if x == 1 else 'b'"
evaluate_python_code(code, state=state) Defensive patterns
Strategy: validation
Validate before calling
import ast
SUPPORTED = {ast.Assign, ast.AugAssign, ast.For, ast.While, ast.If, ast.FunctionDef, ast.Return, ast.ClassDef, ast.Call, ast.BinOp, ...}
unsupported = [n.__class__.__name__ for n in ast.walk(ast.parse(code)) if not isinstance(n, (ast.expr_context, ast.operator, ast.boolop, ast.unaryop, ast.cmpop)) and not any(issubclass(n.__class__, s) for s in SUPPORTED)]
if unsupported:
code = rewrite_or_reject(code, unsupported) Type guard
def uses_only_supported_nodes(code: str) -> bool:
try:
tree = ast.parse(code)
except SyntaxError:
return False
return all(is_supported(n) for n in ast.walk(tree)) Try / catch
from smolagents.local_python_executor import InterpreterError
try:
executor(code_action)
except InterpreterError as e:
if 'is not supported' in str(e):
code_action = simplify_constructs(code_action) # e.g. match -> if/elif Prevention
- Instruct the LLM in the system prompt to use plain Python (no match, async, global) for code actions
- Prefer if/elif over match/case and avoid global/nonlocal in generated code
- Use E2B or Docker executors when full language support is required
When it happens
Trigger: Calling evaluate_python_code or agent.run with code containing constructs like `global x`, `async def`, `await`, `match ... case`, `assert`, or any node not handled in evaluate_ast's isinstance chain.
Common situations: LLM-generated code actions using modern Python syntax (match statements, async/await) or side-effectful statements (global/nonlocal) inside a CodeAgent with local_python_executor; upgrading Python versions that introduce new AST nodes.
Related errors
- Unsupported comparison operator: {op}
- Deletion of {type(target).__name__} targets is not supported
- Cannot unpack non-dict value in **kwargs: {type(starred_dict
- super() needs at least one argument
- super() argument 1 must be type
AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28).
Data as JSON: /api/errors/158cec7bfbafcd07.
Report an issue: GitHub.