liveBook · Manning

Taskflow API

As explained in Data Pipelines with Apache Airflow, Second Edition

The Taskflow API is an Apache Airflow interface for defining workflows by expressing task logic and dependencies through Python functions.

The Taskflow API is Apache Airflow’s decorator-based interface for turning Python functions into tasks and connecting them with ordinary function-like syntax. It reduces the boilerplate of PythonOperator definitions and makes data dependencies visible in the DAG. section 6.6 section 6.6.1

Use the API to express common workflow structures:

  • Apply @task to a Python function; return values become XCom-backed inputs for downstream tasks. section 6.6.1
  • Call decorated tasks like functions: model_id = train_model(); deploy_model(model_id) creates the dependency and passes the model ID. section 6.6.1
  • Apply @dag to a function containing task definitions, configure parameters such as dag_id and schedule, and call the decorated function so Airflow discovers the DAG. section 6.6.2
  • Use specialized decorators for supported operators, including task groups, branching, sensors, Bash, Docker, and virtual environments. section 6.6.1

Training and deploying a model

A training task returns a generated model ID, and a deployment task receives that ID directly: @task def train_model(): return model_id; @task def deploy_model(model_id): print(f"Deploying model {model_id}"); model_id = train_model(); deploy_model(model_id). Airflow handles the underlying XCom exchange and dependency. section 6.6.1

Listing 6.29 Defining a DAG with a Taskflow API decorator
import uuid
from airflow.sdk import task, dag

@dag(  #1
    start_date=...,
    schedule=...,
)
def taskflow_api_decorator():  #2

    @task  #3
    def train_model():
        model_id = str(uuid.uuid4())
        return model_id

    @task  #3
    def deploy_model(model_id: str):
        print(f"Deploying model {model_id}")

    model_id = train_model()
    deploy_model(model_id)  #3

taskflow_api_decorator()  #4

Inspect a complete decorator-defined DAG combining configuration, task functions, data passing, and DAG registration. section 6.6.2

How Data Pipelines with Apache Airflow, Second Edition covers it

How deep it goes
Explains decorators, returned-value dependencies, DAG registration, mixed operators, and XCom limitations; stops short of deeper operator implementation.
Examples
Uses a machine-learning train-and-deploy workflow to demonstrate task conversion, model-ID passing, dependency creation, and mixed operators.
What it assumes
Assumes basic Airflow DAGs, task dependencies, Python functions, and XComs; teaches a less cumbersome alternative to explicit PythonOperator wiring.
What you can do after
Build it from scratch: Define Python tasks and complete Airflow DAGs while exposing dependencies through function calls and arguments.
How it connects
Connects Python functions with DAGs, PythonOperator, XComs, TaskGroup, branching, sensors, and other Airflow operators.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Pipelines with Apache Airflow, Second Edition. The section links point to the parts of the book it draws on.