Airflow Task Groups

A task group bundles several related tasks under one name in the UI. Large DAGs with dozens of tasks turn into a messy picture. Task groups fold related steps into one clean block that you open or close with a click.

The Folder Picture

A desktop with two hundred loose files feels chaotic. Folders named Invoices, Photos, and Reports bring order. A task group acts as a folder for tasks. The work stays the same, and the view gets tidier.

Without groups:
 a1 → a2 → a3 → b1 → b2 → c1 → c2 → c3 → d1 → d2 ...

With groups:
 [ Extract ] ──► [ Transform ] ──► [ Load ]
    (3 tasks)       (2 tasks)        (3 tasks)

Create a Task Group

from datetime import datetime
from airflow import DAG
from airflow.operators.empty import EmptyOperator
from airflow.utils.task_group import TaskGroup

with DAG("grouped_pipeline", start_date=datetime(2024, 1, 1),
         schedule="@daily", catchup=False) as dag:

    start = EmptyOperator(task_id="start")

    with TaskGroup("extract") as extract:
        EmptyOperator(task_id="from_api")
        EmptyOperator(task_id="from_database")
        EmptyOperator(task_id="from_files")

    with TaskGroup("transform") as transform:
        clean = EmptyOperator(task_id="clean")
        merge = EmptyOperator(task_id="merge")
        clean >> merge

    end = EmptyOperator(task_id="end")

    start >> extract >> transform >> end

The line start >> extract >> transform >> end connects whole groups. Every task inside a group follows the same link.

How Groups Look in the UI

The Graph view shows each group as a single box. Click the box to expand it and see the inner tasks. Click again to collapse it. The Grid view indents grouped tasks under the group name.

Task IDs Inside Groups

Airflow adds the group name in front of each task id. The task from_api inside the extract group becomes extract.from_api. This prefix keeps ids unique, so two groups can each hold a task named clean.

GroupTask Written in CodeReal Task ID
extractfrom_apiextract.from_api
transformcleantransform.clean
loadcleanload.clean

Use the real id when you pull an XCom or run a CLI command. Set prefix_group_id=False on the group to turn the prefix off.

Nested Groups

with TaskGroup("warehouse") as warehouse:
    with TaskGroup("staging"):
        EmptyOperator(task_id="load_raw")
    with TaskGroup("marts"):
        EmptyOperator(task_id="build_sales")

A group inside a group builds a layered structure. The id of the inner task reads warehouse.staging.load_raw.

The Decorator Style

With the TaskFlow API, the @task_group decorator turns a function into a group.

from airflow.decorators import task, task_group

@task_group
def prepare_data():
    @task
    def download():
        return "file.csv"

    @task
    def validate(name):
        print(f"Checking {name}")

    validate(download())

Groups Created by a Loop

for region in ["north", "south", "east"]:
    with TaskGroup(f"sales_{region}"):
        EmptyOperator(task_id="extract")
        EmptyOperator(task_id="report")

Loops produce one identical block per region. The names stay unique through the group prefix.

Task Groups and SubDAGs

Older versions offered SubDAGs for the same purpose. SubDAGs create a separate DAG, which causes slot locks and extra overhead. Airflow removed them in version 3. Task groups only change the display and add no scheduling cost, so they replace SubDAGs in every new project.

Group Settings

SettingEffect
tooltipHover text in the Graph view
prefix_group_idTurns the id prefix on or off
default_argsApplies settings to every task in the group
ui_colorBackground color of the group box

Key Takeaways

  • Task groups organize the view and never change how tasks run.
  • Group names become prefixes in task ids.
  • Groups nest and work with loops and the TaskFlow decorator.
  • Task groups replace SubDAGs.