Dependencies
Manage task dependencies and subtasks to create complex workflows.
In complex workflows, tasks often need to be executed in a specific order. Some tasks may rely on the outputs of others, or there might be a logical sequence that must be followed to achieve the desired outcome. ControlFlow provides several mechanisms to define and manage these task relationships, ensuring that your workflow executes in the correct order and that data flows properly between tasks.
ControlFlow offers two primary ways to establish relationships between tasks: sequential dependencies and subtask relationships. Each method has its own use cases and benefits, allowing you to structure your workflows in the most appropriate way for your specific needs.
Upstream dependencies
Upstream dependencies are the most straightforward way to specify that one task must wait for another to complete before it can begin. This is done using the depends_on
parameter when creating a task.
In this example, analyze_sources
will not start until gather_sources
has completed successfully.
Subtasks
Subtasks create a hierarchical dependency structure. A parent task can not be completed until all of its subtasks have finished. This hierarchical structure enables you to create detailed, step-by-step workflows that an AI agent can follow, ensuring thorough and accurate task completion.
Imperative creation
You can create subtasks imperatively by passing the parent task as an argument when creating a new task:
Context managers
Another way to create subtasks is by using a context manager. This approach allows you to dynamically generate and execute subtasks within the scope of a parent task.
Automatic Execution of Dependencies
A key feature of ControlFlow’s dependency management is that you don’t need to explicitly run dependent tasks. When you run a task, ControlFlow automatically executes all of its dependencies, including:
- Tasks specified in the
depends_on
parameter - Subtasks (for parent tasks)
This means that when you run a flow or task, you only need to run or return the final task(s) in the workflow DAG. ControlFlow will ensure that all necessary upstream tasks and subtasks are executed in the correct order.
For example:
In this example, running write_report
will automatically trigger the execution of analyze_sources
, which in turn will trigger gather_sources
. You don’t need to explicitly run or return gather_sources
or analyze_sources
.
To learn more, see running tasks.