#2846·smolagents

BUG: LocalPythonExecutor: augmented assignment to an undefined name starts from 0 instead of raising NameError

Author: jayzuccarelliCreated Sep 25, 2026Updated Sep 25, 2026

Problem

In LocalPythonExecutor, name op= value reads the old value with state.get(target.id, 0) (evaluate_augassign.get_current_value, src/smolagents/local_python_executor.py:646-647). If name was never assigned, CPython raises NameError / UnboundLocalError; the executor silently starts from 0 and creates a new variable.

The common trigger is a typo in an accumulator (totla += x) or a counter the model forgot to initialise. The agent gets no error to self-correct from, and the run continues with a wrong number.

Steps to reproduce

python
from smolagents.local_python_executor import LocalPythonExecutor

ex = LocalPythonExecutor([]); ex.send_tools({})
print(ex("total = 0\nfor x in [1,2,3]:\n    totla += x\ntotal, totla").output)  # CPython: NameError
print(ex("def f():\n    n += 1\n    return n\nf()").output)                    # CPython: UnboundLocalError
print(ex("s += 'abc'").output)                                                 # CPython: NameError

Actual behavior and error logs

bash
(0, 6)
1
InterpreterError: Code execution failed at line 's += 'abc'' due to: TypeError: unsupported operand type(s) for +=: 'int' and 'str'

Expected behavior

InterpreterError saying the variable is not defined, matching what evaluate_name raises for a plain read of an undefined name.

Proposed fix: in the ast.Name branch of get_current_value, raise InterpreterError(f"The variable {target.id} is not defined.") when target.id is not in state (or read through evaluate_name so the lookup rules match). Add a test that x += 1 with x undefined raises. None of the existing augassign tests rely on the 0 default.

Environment:

  • OS: Linux (Ubuntu 22.04)
  • Python version: 3.12.13 (also 3.13.13)
  • Package version: smolagents 1.26.0 (PyPI) and main 1.27.0.dev0 @ 227ef5e49, same output on both

Additional context (optional)

Related but different: #2537 (evaluate augassign targets once, keeps the 0 default), #2746 (list += non-list iterable).

Happy to open a PR with a regression test if this approach works for you.


Checklist

  • I have searched the existing issues and have not found a similar bug report.
  • I have provided a minimal, reproducible example.
  • I have provided the full traceback of the error.
  • I have provided my environment details.
  • I am willing to work on this issue and submit a pull request. (optional)