Apache Airflow Task SDK
The Apache Airflow Task SDK provides Python-native interfaces for defining Directed Acyclic Graphs (DAGs), executing tasks in isolated subprocesses, and interacting with Airflow resources at runtime. Its primary goal is to decouple DAG authoring from Airflow internals, offering a forward-compatible and stable interface across Airflow versions. The library is currently at version 1.2.0 and receives frequent updates, aligning with the release cadence of Apache Airflow 3.x.
Warnings
- breaking Direct metadata database access from task code is now restricted in Airflow 3.x. Tasks can no longer directly import and use Airflow database sessions or models. All runtime interactions must go through a dedicated Task Execution API via the Task SDK.
- breaking All DAGs must update their imports to refer to `airflow.sdk` instead of using internal Airflow modules directly. Deprecated legacy import paths (e.g., `airflow.models.dag.DAG`, `airflow.decorator.task`) will be removed in future Airflow versions.
- breaking SubDAGs are replaced by TaskGroups, Assets, and Data Aware Scheduling in Airflow 3.x. Existing SubDAGs will need to be refactored.
- deprecated SLAs (Service Level Agreements) are deprecated and replaced with Deadline Alerts in Airflow 3.x.
- gotcha The `catchup_by_default` DAG parameter is now `False` by default in Airflow 3.x. This can lead to unexpected behavior if DAGs were implicitly relying on catchup being enabled.
- gotcha When passing context to tasks, the traditional `def my_task(**context)` signature is being superseded. While still functional, the recommended approach in the Task SDK is to explicitly retrieve context using `airflow.sdk.get_current_context()`.
Install
-
pip install apache-airflow-task-sdk
Imports
- dag
from airflow.sdk import dag
- task
from airflow.sdk import task
- DAG
from airflow.sdk import DAG
- BaseOperator
from airflow.sdk import BaseOperator
- Connection
from airflow.sdk import Connection
- Variable
from airflow.sdk import Variable
- Param
from airflow.sdk import Param
- XComArg
from airflow.sdk import XComArg
- get_current_context
from airflow.sdk import get_current_context
Quickstart
import pendulum
from airflow.sdk import dag, task
@task
def my_task():
print("Hello from a Task SDK task!")
@dag(
schedule=None,
start_date=pendulum.datetime(2023, 1, 1, tz="UTC"),
catchup=False,
tags=["example"],
)
def example_simplest_dag():
my_task()
example_dag_instance = example_simplest_dag()