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
