{"record":{"id":"2648a2249f26c20b","repo":"huggingface/smolagents","slug":"the-variable-name-id-is-not-defined","errorCode":null,"errorMessage":"The variable `{name.id}` is not defined.","messagePattern":"The variable `(.+?)` is not defined\\.","errorType":"exception","errorClass":"InterpreterError","httpStatus":null,"severity":"error","filePath":"src/smolagents/local_python_executor.py","lineNumber":959,"sourceCode":"def evaluate_name(\n    name: ast.Name,\n    state: dict[str, Any],\n    static_tools: dict[str, Callable],\n    custom_tools: dict[str, Callable],\n    authorized_imports: list[str],\n) -> Any:\n    if name.id in state:\n        return state[name.id]\n    elif name.id in static_tools:\n        return safer_func(static_tools[name.id], static_tools=static_tools, authorized_imports=authorized_imports)\n    elif name.id in custom_tools:\n        return custom_tools[name.id]\n    elif name.id in ERRORS:\n        return ERRORS[name.id]\n    close_matches = difflib.get_close_matches(name.id, list(state.keys()))\n    if len(close_matches) > 0:\n        return state[close_matches[0]]\n    raise InterpreterError(f\"The variable `{name.id}` is not defined.\")\n\n\ndef evaluate_condition(\n    condition: ast.Compare,\n    state: dict[str, Any],\n    static_tools: dict[str, Callable],\n    custom_tools: dict[str, Callable],\n    authorized_imports: list[str],\n) -> bool | object:\n    result = True\n    left = evaluate_ast(condition.left, state, static_tools, custom_tools, authorized_imports)\n    for i, (op, comparator) in enumerate(zip(condition.ops, condition.comparators)):\n        op = type(op)\n        right = evaluate_ast(comparator, state, static_tools, custom_tools, authorized_imports)\n        if op == ast.Eq:\n            current_result = left == right\n        elif op == ast.NotEq:\n            current_result = left != right","sourceCodeStart":941,"sourceCodeEnd":977,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/local_python_executor.py#L941-L977","documentation":"The sandboxed interpreter could not resolve a Name node: the identifier is not in local state, not a static/custom tool, and not in the ERRORS mapping (built-in exception classes). Note it first tries difflib close matches against state before failing — but only returns a close match if one exists.","triggerScenarios":"Using an undefined variable, a NameError in normal Python; referencing a tool by the wrong name; using an exception class not in the interpreter's ERRORS whitelist (e.g. some third-party exception).","commonSituations":"Agent code references a variable defined in a different script run (state doesn't persist between executor runs); typo in variable name where no close match exists; expecting builtins like __import__ or exception classes not whitelisted.","solutions":["Define the variable before use in the same code block (state is per-run)","Check spelling of tools and variables; the close-match fallback only works for near-identical names","For exception classes, only use those exposed by the interpreter's ERRORS mapping (standard builtins)"],"exampleFix":"# before\ntotal = price + quantty  # typo\n# after\ntotal = price + quantity","handlingStrategy":"validation","validationCode":"# generated code: check before use\nif 'target_var' not in dir_var_names:  # or track defined names explicitly\n    target_var = compute_default()","typeGuard":null,"tryCatchPattern":"try:\n    evaluate_python(code, ...)\nexcept InterpreterError as e:\n    if 'is not defined' in str(e):\n        # ask the model/agent to define the variable in this block, retry","preventionTips":["Define every variable in the same executed block; executor state does not persist across runs","Double-check spelling; close-match fallback only covers near-identical names","Only reference whitelisted exception classes"],"tags":["python-executor","name-error","undefined-variable","smolagents"],"backgroundTag":"undefined-variable-nameerror","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}