{"record":{"id":"b8d15a8b49f7bca5","repo":"huggingface/smolagents","slug":"cannot-delete-index-key-str-e","errorCode":null,"errorMessage":"Cannot delete index/key: {str(e)}","messagePattern":"Cannot delete index/key: (.+?)","errorType":"exception","errorClass":"InterpreterError","httpStatus":null,"severity":"error","filePath":"src/smolagents/local_python_executor.py","lineNumber":1411,"sourceCode":"        static_tools: Dictionary of static tools\n        custom_tools: Dictionary of custom tools\n        authorized_imports: List of authorized imports\n    \"\"\"\n    for target in delete_node.targets:\n        if isinstance(target, ast.Name):\n            # Handle simple variable deletion (del x)\n            if target.id in state:\n                del state[target.id]\n            else:\n                raise InterpreterError(f\"Cannot delete name '{target.id}': name is not defined\")\n        elif isinstance(target, ast.Subscript):\n            # Handle index/key deletion (del x[y])\n            obj = evaluate_ast(target.value, state, static_tools, custom_tools, authorized_imports)\n            index = evaluate_ast(target.slice, state, static_tools, custom_tools, authorized_imports)\n            try:\n                del obj[index]\n            except (TypeError, KeyError, IndexError) as e:\n                raise InterpreterError(f\"Cannot delete index/key: {str(e)}\")\n        else:\n            raise InterpreterError(f\"Deletion of {type(target).__name__} targets is not supported\")\n\n\n@safer_eval\ndef evaluate_ast(\n    expression: ast.AST,\n    state: dict[str, Any],\n    static_tools: dict[str, Callable],\n    custom_tools: dict[str, Callable],\n    authorized_imports: list[str] = BASE_BUILTIN_MODULES,\n):\n    \"\"\"\n    Evaluate an abstract syntax tree using the content of the variables stored in a state and only evaluating a given\n    set of functions.\n\n    This function will recurse through the nodes of the tree provided.\n","sourceCodeStart":1393,"sourceCodeEnd":1429,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/local_python_executor.py#L1393-L1429","documentation":"A `del obj[index]` statement failed with TypeError, KeyError, or IndexError inside the sandbox; the message wraps the original error string.","triggerScenarios":"del d['missing'] (KeyError), del lst[10] (IndexError), del x[0] on an int/None or immutable tuple (TypeError).","commonSituations":"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.","solutions":["Check membership/range before del: if key in d: del d[key]; if 0 <= i < len(lst): del lst[i]","Use d.pop(key, None) for dicts to delete-if-exists","Verify the container type supports deletion (list/dict, not tuple/str)"],"exampleFix":"# before\ndel cache[key]\n# after\ncache.pop(key, None)","handlingStrategy":"type-guard","validationCode":"if isinstance(obj, dict) and key in obj:\n    del obj[key]","typeGuard":"def can_delete_at(obj, idx) -> bool:\n    if isinstance(obj, dict):\n        return idx in obj\n    if isinstance(obj, list):\n        return isinstance(idx, int) and -len(obj) <= idx < len(obj)\n    return False","tryCatchPattern":"try:\n    evaluate_python(code, ...)\nexcept InterpreterError as e:\n    if 'Cannot delete index/key' in str(e):\n        # use pop(key, None) / bound-checked del and retry","preventionTips":["Use d.pop(key, None) for delete-if-exists","Bounds-check list indices before del"],"tags":["python-executor","del","subscript","key-error","smolagents"],"backgroundTag":"container-subscript-out-of-range-or-key","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}