huggingface/smolagents · error · InterpreterError

Cannot delete index/key: {str(e)}

Error message

Cannot delete index/key: {str(e)}

What it means

A `del obj[index]` statement failed with TypeError, KeyError, or IndexError inside the sandbox; the message wraps the original error string.

Source

Thrown at src/smolagents/local_python_executor.py:1411

        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],
    authorized_imports: list[str] = BASE_BUILTIN_MODULES,
):
    """
    Evaluate an abstract syntax tree using the content of the variables stored in a state and only evaluating a given
    set of functions.

    This function will recurse through the nodes of the tree provided.

View on GitHub (pinned to 30bb116109)

Solutions

  1. Check membership/range before del: if key in d: del d[key]; if 0 <= i < len(lst): del lst[i]
  2. Use d.pop(key, None) for dicts to delete-if-exists
  3. Verify the container type supports deletion (list/dict, not tuple/str)

Example fix

# before
del cache[key]
# after
cache.pop(key, None)
Defensive patterns

Strategy: type-guard

Validate before calling

if isinstance(obj, dict) and key in obj:
    del obj[key]

Type guard

def can_delete_at(obj, idx) -> bool:
    if isinstance(obj, dict):
        return idx in obj
    if isinstance(obj, list):
        return isinstance(idx, int) and -len(obj) <= idx < len(obj)
    return False

Try / catch

try:
    evaluate_python(code, ...)
except InterpreterError as e:
    if 'Cannot delete index/key' in str(e):
        # use pop(key, None) / bound-checked del and retry

Prevention

When it happens

Trigger: del d['missing'] (KeyError), del lst[10] (IndexError), del x[0] on an int/None or immutable tuple (TypeError).

Common situations: Agent code removes processed keys and the key is absent or already removed in a loop; deleting from a tuple (immutable); the container is None because an earlier step failed.

Related errors


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