This hack solves it only in the context of this simple example, but not if python_repl added as a tool to an AgentExecutor. At the core, the issue seems to be caused by Python scopes, but my knowledge ...