Error handling

ControlFlow provides mechanisms to handle errors and exceptions that may occur during task execution.

In eager execution mode, if a task fails and raises an exception, you can catch and handle the exception using Python’s standard exception handling techniques, such as try-except blocks.

Here’s an example of error handling in eager mode:

from controlflow import flow, task

@task
def divide_numbers(a: int, b: int) -> float:
    """Divide two numbers."""
    pass

@flow
def my_flow():
    try:
        result = divide_numbers(10, 0)
        print(result)
    except ValueError as e:
        print(f"Error: {str(e)}")

In this example, if the divide_numbers task fails because the agent recognizes that it can’t divide by zero, it will raise a ValueError. The exception is caught in the except block, and an error message is printed.

In lazy execution mode, exceptions are not raised immediately but are propagated through the task DAG. When the flow is executed using the run() method, any exceptions that occurred during task execution will be raised at that point.

Observability and orchestration

ControlFlow integrates with Prefect, a popular workflow orchestration tool, to provide observability and orchestration capabilities for your workflows.

Under the hood, ControlFlow tasks and flows are modeled as Prefect tasks and flows, allowing you to leverage Prefect’s features for monitoring, logging, and orchestration. This integration enables you to track the execution status of tasks, monitor their progress, and access detailed logs and reports.

By default, ControlFlow configures Prefect to use a local SQLite database for storage and a ephemeral Prefect server for orchestration. You can customize the Prefect configuration to use different storage backends and orchestration setups based on your requirements.

To access the Prefect UI and view the status and logs of your workflows, you can start the Prefect server and open the UI in your web browser:

prefect server start

Once the server is running, you can open the Prefect UI by navigating to http://localhost:4200 in your web browser. The UI provides a visual interface to monitor the execution of your workflows, view task statuses, and access logs and reports.