{"record":{"id":"aacbabdc3b98278c","repo":"huggingface/smolagents","slug":"cannot-delete-name-target-id-name-is-not-defi","errorCode":null,"errorMessage":"Cannot delete name '{target.id}': name is not defined","messagePattern":"Cannot delete name '(.+?)': name is not defined","errorType":"exception","errorClass":"InterpreterError","httpStatus":null,"severity":"error","filePath":"src/smolagents/local_python_executor.py","lineNumber":1403,"sourceCode":"    authorized_imports: list[str],\n) -> None:\n    \"\"\"\n    Evaluate a delete statement (del x, del x[y]).\n\n    Args:\n        delete_node: The AST Delete node to evaluate\n        state: The current state dictionary\n        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],","sourceCodeStart":1385,"sourceCodeEnd":1421,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/local_python_executor.py#L1385-L1421","documentation":"A `del name` statement referenced a variable that does not exist in the interpreter state — the sandboxed equivalent of NameError on del.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Guard with if 'x' in ... — practically, only del names you assigned in the same code block","Use try/except around the del (NameError semantics)","Reassign to None instead of deleting if cleanup is the goal"],"exampleFix":"# before\ndel results  # may not exist\n# after\nresults = None  # or:\ntry:\n    del results\nexcept NameError:\n    pass","handlingStrategy":"try-catch","validationCode":"# in generated code, only delete what this block assigned\nif 'x' in locals():\n    del x","typeGuard":null,"tryCatchPattern":"try:\n    evaluate_python(code, ...)\nexcept InterpreterError as e:\n    if 'name is not defined' in str(e) and 'delete' in str(e):\n        # drop or guard the del statement and retry","preventionTips":["Prefer setting to None over del for cleanup in sandboxed code","Remember executor state does not persist across runs"],"tags":["python-executor","del","name-error","smolagents"],"backgroundTag":"deleting-undefined-variable","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}