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_callback
field to theCrew
class. 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_callback
field toCrew
class with proper type annotations and validation_execute_tasks
method to support dynamic task selection via callbackReview & Testing Checklist for Human
task_ordering_callback
and verify they execute correctly in both Sequential and Hierarchical processes_execute_tasks
method was changed from a for loop to a while loop with complex state tracking - ensure no edge cases or infinite loopsexecuted_task_indices
tracking)Notes
uv.lock
file preventing pytest executionNote
Adds
task_ordering_callback
toCrew
and updates task execution to dynamically select the next task with validation, fallbacks, and tests.task_ordering_callback
field toCrew
to dynamically choose the next task (accepts index,Task
, orNone
)._execute_tasks
to a loop tracking executed indices, invoking callback, handling async/sync, and preventing repeats.model_validator
to verify callback signature (exactly 3 params) and callability.examples/dynamic_task_ordering_example.py
showing priority- and condition-based ordering.tests/test_dynamic_task_ordering.py
covering reverse/priority ordering, returningTask
vs 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.