-
Notifications
You must be signed in to change notification settings - Fork 5.2k
feat: Add dynamic task ordering capability to Sequential and Hierarchical processes #3621
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e1c2c08
feat: Add dynamic task ordering capability to Sequential and Hierarch…
devin-ai-integration[bot] c467c96
fix: Remove whitespace from blank lines in example file
devin-ai-integration[bot] ed95f47
fix: Resolve critical bugs identified by Cursor Bugbot
devin-ai-integration[bot] 1de7dcd
fix: Prevent callback validation ValueError from being suppressed
devin-ai-integration[bot] 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
There are no files selected for viewing
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,113 @@ | ||
""" | ||
Example demonstrating dynamic task ordering in CrewAI. | ||
|
||
This example shows how to use the task_ordering_callback to dynamically | ||
determine the execution order of tasks based on runtime conditions. | ||
""" | ||
|
||
from crewai import Agent, Crew, Task | ||
from crewai.process import Process | ||
|
||
|
||
def priority_based_ordering(all_tasks, completed_outputs, current_index): | ||
""" | ||
Order tasks by priority (lower number = higher priority). | ||
|
||
Args: | ||
all_tasks: List of all tasks in the crew | ||
completed_outputs: List of TaskOutput objects for completed tasks | ||
current_index: Current task index (for default ordering) | ||
|
||
Returns: | ||
int: Index of next task to execute | ||
Task: Task object to execute next | ||
None: Use default ordering | ||
""" | ||
completed_tasks = {id(task) for task in all_tasks if task.output is not None} | ||
|
||
remaining_tasks = [ | ||
(i, task) for i, task in enumerate(all_tasks) | ||
if id(task) not in completed_tasks | ||
] | ||
|
||
if not remaining_tasks: | ||
return None | ||
|
||
remaining_tasks.sort(key=lambda x: getattr(x[1], 'priority', 999)) | ||
|
||
return remaining_tasks[0][0] | ||
|
||
|
||
def conditional_ordering(all_tasks, completed_outputs, current_index): | ||
""" | ||
Order tasks based on previous task outputs. | ||
|
||
This example shows how to make task ordering decisions based on | ||
the results of previously completed tasks. | ||
""" | ||
if len(completed_outputs) == 0: | ||
return 0 | ||
|
||
last_output = completed_outputs[-1] | ||
|
||
if "urgent" in last_output.raw.lower(): | ||
completed_tasks = {id(task) for task in all_tasks if task.output is not None} | ||
for i, task in enumerate(all_tasks): | ||
if (hasattr(task, 'priority') and task.priority == 1 and | ||
id(task) not in completed_tasks): | ||
return i | ||
|
||
return None | ||
|
||
|
||
researcher = Agent( | ||
role="Research Analyst", | ||
goal="Gather and analyze information", | ||
backstory="Expert at finding and synthesizing information" | ||
) | ||
|
||
writer = Agent( | ||
role="Content Writer", | ||
goal="Create compelling content", | ||
backstory="Skilled at crafting engaging narratives" | ||
) | ||
|
||
reviewer = Agent( | ||
role="Quality Reviewer", | ||
goal="Ensure content quality", | ||
backstory="Meticulous attention to detail" | ||
) | ||
|
||
research_task = Task( | ||
description="Research the latest trends in AI", | ||
expected_output="Comprehensive research report", | ||
agent=researcher | ||
) | ||
research_task.priority = 2 | ||
|
||
urgent_task = Task( | ||
description="Write urgent press release", | ||
expected_output="Press release draft", | ||
agent=writer | ||
) | ||
urgent_task.priority = 1 | ||
|
||
review_task = Task( | ||
description="Review and edit content", | ||
expected_output="Polished final content", | ||
agent=reviewer | ||
) | ||
review_task.priority = 3 | ||
|
||
crew = Crew( | ||
agents=[researcher, writer, reviewer], | ||
tasks=[research_task, urgent_task, review_task], | ||
process=Process.sequential, | ||
task_ordering_callback=priority_based_ordering, | ||
verbose=True | ||
) | ||
|
||
if __name__ == "__main__": | ||
print("Starting crew with dynamic task ordering...") | ||
result = crew.kickoff() | ||
print(f"Completed {len(result.tasks_output)} tasks") |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Callback Validation Bypass
The
validate_task_ordering_callback
method'stry-except
block is too broad. It catches theValueError
raised when thetask_ordering_callback
has an incorrect number of parameters, allowing invalid callbacks to pass validation silently. This prevents proper signature checks and could lead to runtime errors.