Skip to content

Conversation

AhnJoonSung
Copy link

Fix duplicate action_step yield and NameError in _run_stream()

Description

This PR fixes two issues in the _run_stream method of the MultiStepAgent class:

  1. When an agent reaches max_steps, the final action_step was being yielded twice - once in the finally block and again after the while loop
  2. When max_steps=0, the code attempted to yield an undefined action_step variable, causing a NameError

Changes

  • Removed the duplicate yield action_step on line 605 in src/smolagents/agents.py
  • The action_step is already properly yielded in the finally block (line 600), so the additional yield was both redundant and problematic

Impact

This fix ensures that:

  • Stream consumers receive each action_step exactly once
  • Edge cases like max_steps=0 are handled gracefully without errors
  • Both CodeAgent and ToolCallingAgent benefit from this fix since they inherit from MultiStepAgent

Fixes #1816

@aymeric-roucher
Copy link
Collaborator

HI @AhnJoonSung ! It's not really clear to me what was problematic before : could you add a test, or add assertions to an existing test, to show what was problematic before and now is solved + avoid regressions in the future?

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.

BUG: Duplicate action_step yield and NameError when max_steps=0

2 participants