feat: Add dynamic task ordering capability to Sequential and Hierarchical processes #3621
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.
feat: Add dynamic task ordering capability to Sequential and Hierarchical processes
Summary
Implements dynamic task ordering for CrewAI's Sequential and Hierarchical processes by adding a
task_ordering_callbackfield to theCrewclass. This allows users to provide a callback function that dynamically determines the next task to execute based on runtime conditions, completed task outputs, and current execution context.Key Changes:
task_ordering_callbackfield toCrewclass with proper type annotations and validation_execute_tasksmethod to support dynamic task selection via callbackReview & Testing Checklist for Human
task_ordering_callbackand verify they execute correctly in both Sequential and Hierarchical processes_execute_tasksmethod was changed from a for loop to a while loop with complex state tracking - ensure no edge cases or infinite loopsexecuted_task_indicestracking)Notes
uv.lockfile preventing pytest executionNote
Adds
task_ordering_callbacktoCrewand updates task execution to dynamically select the next task with validation, fallbacks, and tests.task_ordering_callbackfield toCrewto dynamically choose the next task (accepts index,Task, orNone)._execute_tasksto a loop tracking executed indices, invoking callback, handling async/sync, and preventing repeats.model_validatorto verify callback signature (exactly 3 params) and callability.examples/dynamic_task_ordering_example.pyshowing priority- and condition-based ordering.tests/test_dynamic_task_ordering.pycovering reverse/priority ordering, returningTaskvs index, invalid indices/types, exceptions, default behavior, and infinite-loop prevention.Written by Cursor Bugbot for commit 1de7dcd. This will update automatically on new commits. Configure here.