Skip to content

Bug Fix: Fix continue semantics of LocalPythonExecutor #1645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 6, 2025

Conversation

adaminsky
Copy link
Contributor

Bug: continue treated as pass within for loops with LocalPythonExecutor

Fix

The proposed fix is to make continue statements break from the current execution of the loop body and continue to the next loop iteration.

Problem Description

The local python interpreter incorrectly handles continue statements within for loops. A continue statement is treated as a pass statement which silently breaks normal Python semantics.

Minimal Bug Reproduction:

from smolagents.local_python_executor import LocalPythonExecutor, BASE_PYTHON_TOOLS

# this program should not print anything
program = """for i in range(5):
  continue
  print(i)"""

executor = LocalPythonExecutor(additional_authorized_imports=[])
executor.send_tools(BASE_PYTHON_TOOLS)

print(executor(program).logs)

Output:

0
1
2
3
4

The output from the above code is incorrect based on the semantics of a Python continue statement which should skip to the next loop iteration rather than execute the rest of the loop body.

Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks! Could you please add a regression test?

Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix and the test!

I just refactored the tests.

@albertvillanova albertvillanova merged commit 2d4a2ef into huggingface:main Aug 6, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants