Airflow Branching, Dynamic Task Mapping and Task Groups by Example

Short answer: use branching when a task should choose one path and skip the other, dynamic task mapping when you need one task for each item in a list that is only known when the DAG runs, and task groups to draw related tasks as one box. Each example below is a small DAG you can run in the free Airflow Lab.

Open the Airflow Lab

Branching: choose a path

A branch task returns the `task_id` of the path to run. The other path is skipped, not failed.

import pendulum
from airflow.sdk import dag, task
from airflow.providers.standard.operators.empty import EmptyOperator


@dag(start_date=pendulum.datetime(2026, 1, 1, tz="UTC"), schedule=None, catchup=False)
def branching():
    @task.branch
    def choose():
        # change the number, run again and watch a different path run
        return "big_order_path" if 120 > 100 else "small_order_path"

    big = EmptyOperator(task_id="big_order_path")
    small = EmptyOperator(task_id="small_order_path")
    done = EmptyOperator(task_id="done", trigger_rule="none_failed_min_one_success")

    choose() >> [big, small] >> done


branching()

**The trigger rule matters.** By default a task runs only when all its upstream tasks succeeded. One of the two paths is skipped, so `done` would be skipped too. `none_failed_min_one_success` means: run if nothing failed and at least one upstream task succeeded.

Dynamic task mapping: one task per item

Sometimes you do not know how many tasks you need until the DAG runs, for example one per file in a folder. `expand()` creates the copies at run time.

import pendulum
from airflow.sdk import dag, task


@dag(start_date=pendulum.datetime(2026, 1, 1, tz="UTC"), schedule=None, catchup=False)
def dynamic_mapping():
    @task
    def list_files():
        return ["orders.csv", "customers.csv", "payments.csv", "products.csv"]

    @task
    def process(file_name):
        print(f"Processing {file_name}")
        return len(file_name)

    @task
    def summarize(sizes):
        print("Processed", len(sizes), "files")

    summarize(process.expand(file_name=list_files()))   # one "process" task per file


dynamic_mapping()

Add a file name to the list and run again: you get one more `process` task, and `summarize` receives all the results. The [official guide to dynamic task mapping](https://airflow.apache.org/docs/apache-airflow/stable/authoring-and-scheduling/dynamic-task-mapping.html) covers the details and the limits.

Branching or mapping?

| You need | Use | |---|---| | One of several different paths | Branching | | The same task repeated for each item | Dynamic task mapping |

Task groups: keep a big DAG readable

import pendulum
from airflow.sdk import DAG, TaskGroup
from airflow.providers.standard.operators.bash import BashOperator

with DAG(dag_id="task_groups", start_date=pendulum.datetime(2026, 1, 1, tz="UTC"), schedule=None, catchup=False) as dag:
    with TaskGroup(group_id="extract") as extract:
        BashOperator(task_id="orders", bash_command="echo extract orders")
        BashOperator(task_id="customers", bash_command="echo extract customers")

A task group only changes how the graph is drawn: the tasks inside one box collapse into a single node you can open. Use groups when a DAG grows past a screenful.

Run all three

In the Airflow Lab, the track **Going further** has these three as lessons 6, 7 and 8. Press Test DAG, change a number or a list, and run again.

Open the Airflow Lab

Related tool pages

Also try

How to use the Airflow Lab: step by step

  1. Start the Airflow Lab
  2. Open Branching and change the numbers
  3. Open Dynamic task mapping and add a file to the list
  4. Open Task groups and look at the graph

When to use the Airflow Lab

  • Learning the Airflow features that come after your first DAG
  • Choosing between branching and mapping
  • Cleaning up a DAG that has grown too big
  • Preparing for an Airflow interview

Example

Input: a DAG that cleans data, runs a Spark job and lists the result

Output: Task states in the lab, the DAG in the Airflow UI, the job in the Spark UI

Frequently asked questions

How does branching work in Airflow?

A task decorated with @task.branch returns the task_id of the path to run. The other paths are skipped. The task after the branches needs a trigger rule such as none_failed_min_one_success so it still runs.

What is dynamic task mapping?

It creates one task instance for each item in a list at run time, using expand(). It is useful when the number of items, such as files, is not known when you write the DAG.

Do task groups change how tasks run?

No. They only group tasks in the graph view so a large DAG is easier to read. The dependencies work the same.

Tip: In the mapping lesson, add one more file name to the list and run again. Seeing one extra task appear is the whole idea.