huggingface/smolagents · error · InterpreterError

Cannot delete name '{target.id}': name is not defined

Error message

Cannot delete name '{target.id}': name is not defined

What it means

A `del name` statement referenced a variable that does not exist in the interpreter state — the sandboxed equivalent of NameError on del.

Source

Thrown at src/smolagents/local_python_executor.py:1403

    authorized_imports: list[str],
) -> None:
    """
    Evaluate a delete statement (del x, del x[y]).

    Args:
        delete_node: The AST Delete node to evaluate
        state: The current state dictionary
        static_tools: Dictionary of static tools
        custom_tools: Dictionary of custom tools
        authorized_imports: List of authorized imports
    """
    for target in delete_node.targets:
        if isinstance(target, ast.Name):
            # Handle simple variable deletion (del x)
            if target.id in state:
                del state[target.id]
            else:
                raise InterpreterError(f"Cannot delete name '{target.id}': name is not defined")
        elif isinstance(target, ast.Subscript):
            # Handle index/key deletion (del x[y])
            obj = evaluate_ast(target.value, state, static_tools, custom_tools, authorized_imports)
            index = evaluate_ast(target.slice, state, static_tools, custom_tools, authorized_imports)
            try:
                del obj[index]
            except (TypeError, KeyError, IndexError) as e:
                raise InterpreterError(f"Cannot delete index/key: {str(e)}")
        else:
            raise InterpreterError(f"Deletion of {type(target).__name__} targets is not supported")


@safer_eval
def evaluate_ast(
    expression: ast.AST,
    state: dict[str, Any],
    static_tools: dict[str, Callable],
    custom_tools: dict[str, Callable],

View on GitHub (pinned to 30bb116109)

Solutions

  1. Guard with if 'x' in ... — practically, only del names you assigned in the same code block
  2. Use try/except around the del (NameError semantics)
  3. Reassign to None instead of deleting if cleanup is the goal

Example fix

# before
del results  # may not exist
# after
results = None  # or:
try:
    del results
except NameError:
    pass
Defensive patterns

Strategy: try-catch

Validate before calling

# in generated code, only delete what this block assigned
if 'x' in locals():
    del x

Try / catch

try:
    evaluate_python(code, ...)
except InterpreterError as e:
    if 'name is not defined' in str(e) and 'delete' in str(e):
        # drop or guard the del statement and retry

Prevention

When it happens

Trigger: del x when x was never assigned in this run, was already deleted, or lives only in static_tools/custom_tools (which del cannot touch).

Common situations: Agent code deletes loop variables after a loop that never ran; state does not persist between executor runs so deleting something defined in a previous block fails; attempting to del a tool name.

Related errors


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