Skip to content

fix: remove duplicate action_step yield and prevent NameError when max_steps=0#2323

Open
gauravSsinha wants to merge 1 commit into
huggingface:mainfrom
substrai:fix/run-stream-duplicate-yield-and-nameerror
Open

fix: remove duplicate action_step yield and prevent NameError when max_steps=0#2323
gauravSsinha wants to merge 1 commit into
huggingface:mainfrom
substrai:fix/run-stream-duplicate-yield-and-nameerror

Conversation

@gauravSsinha
Copy link
Copy Markdown

Description

Fixes two bugs in MultiStepAgent._run_stream():

  1. Duplicate yield: When max_steps is reached, action_step was yielded in the finally block AND again after the while loop. Stream consumers received duplicate step events.

  2. NameError when max_steps=0: If max_steps=0, the while loop never executes, so action_step is never defined. The post-loop yield action_step raises NameError.

Fix

Remove the redundant yield action_step after the while loop. The finally block already yields every action_step on each iteration, and _handle_max_steps_reached() handles the max-steps case without needing to re-yield.

One-line change

         if not returned_final_answer and self.step_number == max_steps + 1:
             final_answer = self._handle_max_steps_reached(task)
-            yield action_step

Fixes #1816

…x_steps=0

Two bugs in _run_stream:

1. Duplicate yield: when max_steps is reached, action_step was yielded
   in the finally block (line 601) AND again on line 605. Consumers of
   the stream received duplicate step events.

2. NameError: if max_steps=0, the while loop never executes so
   action_step is never defined. The post-loop code tried to yield
   this undefined variable.

Fix: remove the redundant 'yield action_step' after the while loop.
The finally block already yields every action_step, and
_handle_max_steps_reached handles the max-steps case without needing
to re-yield the last step.

Fixes huggingface#1816

Signed-off-by: Gaurav Kumar Sinha <gaurav@substrai.dev>
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

1 participant