-
Notifications
You must be signed in to change notification settings - Fork 5.1k
fix: loop iterations to forward output from previous iteration #6868
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
Open
kausmeows
wants to merge
5
commits into
main
Choose a base branch
from
fix/loop-iteration-outputs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+296
−0
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
97e02eb
fix: loop iterations to forward output from previous iteration
kausmeows 98509f2
Merge branch 'main' into fix/loop-iteration-outputs
kausmeows ea55119
chore: add a flag to control the behaviour
kausmeows f5cfd62
Merge branch 'main' into fix/loop-iteration-outputs
kausmeows 68da53b
update
kausmeows File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
cookbook/04_workflows/03_loop_execution/loop_iterative_accumulation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """ | ||
| Loop Iterative Accumulation | ||
| ============================ | ||
|
|
||
| Demonstrates that Loop iterations carry forward the output from the previous iteration. | ||
| Each iteration receives the previous iteration's output via `step_input.get_last_step_content()`, | ||
| enabling iterative processing patterns like accumulation, refinement, and convergence. | ||
|
|
||
| This example increments a numeric value by 10 each iteration, stopping when it reaches 50 or more. | ||
| Starting from 35, the loop should: | ||
| - Iteration 1: 35 -> 45 | ||
| - Iteration 2: 45 -> 55 (>= 50, end condition met) | ||
| """ | ||
|
|
||
| from agno.workflow import Loop, Step, Workflow | ||
| from agno.workflow.types import StepInput, StepOutput | ||
|
|
||
|
|
||
| def increment_executor(step_input: StepInput) -> StepOutput: | ||
| """Increment the previous step's numeric content by 10.""" | ||
| last_content = step_input.get_last_step_content() | ||
| if last_content and last_content.isdigit(): | ||
| new_value = int(last_content) + 10 | ||
| return StepOutput(content=str(new_value)) | ||
| return StepOutput(content="0") | ||
|
|
||
|
|
||
| workflow = Workflow( | ||
| name="Iterative Accumulation Workflow", | ||
| description="Demonstrates loop iterations carrying forward output from previous iterations.", | ||
| steps=[ | ||
| Step( | ||
| name="Initial Value", | ||
| description="Pass through the initial input value.", | ||
| executor=lambda step_input: StepOutput(content=step_input.input), | ||
| ), | ||
| Loop( | ||
| name="Increment Loop", | ||
| description="Increment value by 10 each iteration until it reaches 50.", | ||
| steps=[ | ||
| Step( | ||
| name="Increment Step", | ||
| description="Add 10 to the current value.", | ||
| executor=increment_executor, | ||
| ) | ||
| ], | ||
| end_condition=lambda step_outputs: int(step_outputs[-1].content) >= 50, | ||
| max_iterations=10, | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
| if __name__ == "__main__": | ||
| workflow.print_response("35") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.