Imperative API
The imperative API uses class instantiation to create tasks and flows explicitly. This approach offers more fine-grained control over task and flow properties.Task
class, allowing explicit specification of properties like result_type
, user_access
, and context
.
The imperative API uses lazy execution by default. This means tasks and flows are not run until they are explicitly invoked, which can result in better performance. For more information on execution modes, see the lazy execution pattern.
Functional API
The functional API uses decorators to transform Python functions into ControlFlow tasks and flows. This approach is more concise and often more intuitive, especially for those familiar with Python decorators.The functional API uses eager execution by default. This means tasks and flows are executed immediately when called. For more information on execution modes, see the lazy execution pattern.
Combining APIs
ControlFlow allows you to mix and match the functional and imperative APIs. This flexibility enables you to choose the most appropriate style for each task or flow based on your specific requirements.@flow
decorator for overall workflow structure with the flexibility of Task
for individual task definition.
Which API Should I Use?
tldr; Use the functional API for flows and start with the imperative API for tasks.
@flow
decorator for defining workflows. This provides a simple, intuitive way to structure your workflow as a function with clear inputs and outputs.
For tasks, we recommend most users start with imperative Task
objects. This approach allows for more dynamic task creation and fine-grained control over task properties. It also lets your workflow benefit from lazy execution optimizations, which can enhance performance.
However, the functional API is a great choice for simple tasks where you want to quickly define a task with minimal boilerplate.