Airflow Branching

Branching lets a DAG choose one path out of several at run time. A task looks at some condition, picks the path to follow, and skips the rest. Real pipelines need this when the work depends on the data, the day of the week, or the result of a check.

The Road Fork Picture

You drive to a fork in the road. A sign says "Highway open: take the highway. Highway closed: take the village road." You read the sign and choose one road. The other road stays unused. A branch task is that sign.

                    ┌──► heavy_load  (weekday)
 check_day ──►      │
 (branch)           └──► light_load  (weekend)

 Only one path runs. The other path turns pink (skipped).

BranchPythonOperator

The branch function returns the task id, or a list of task ids, that should run next. Airflow skips every other direct child.

from datetime import datetime
from airflow import DAG
from airflow.operators.python import BranchPythonOperator
from airflow.operators.empty import EmptyOperator


def choose_path(**context):
    day = context["logical_date"].weekday()   # Monday = 0
    if day < 5:
        return "heavy_load"
    return "light_load"


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

    branch = BranchPythonOperator(task_id="check_day", python_callable=choose_path)
    heavy  = EmptyOperator(task_id="heavy_load")
    light  = EmptyOperator(task_id="light_load")

    branch >> [heavy, light]

The Decorator Style

from airflow.decorators import task

@task.branch
def choose_path(**context):
    if context["logical_date"].weekday() < 5:
        return "heavy_load"
    return "light_load"

The decorator does the same job with less code.

Joining the Paths Again

Branches often reunite for a shared final step. The skipped path causes a problem here. A join task with the default rule sees a skipped parent and skips itself too. Set the trigger rule to fix it.

join = EmptyOperator(
    task_id="join",
    trigger_rule="none_failed_min_one_success",
)

branch >> [heavy, light] >> join
 check_day ──┬──► heavy_load ──┐
             │                 ├──► join (runs)
             └──► light_load ──┘
                   (skipped)

Choosing More Than One Path

Return a list of ids to run several paths together.

def choose_reports(**context):
    paths = ["daily_report"]
    if context["logical_date"].day == 1:
        paths.append("monthly_report")
    return paths

On the first day of a month, both reports run. Other days run only the daily report.

ShortCircuitOperator

The ShortCircuitOperator handles a simpler question: should the rest of the pipeline run at all? Its function returns True or False. A False result skips every downstream task.

from airflow.operators.python import ShortCircuitOperator

def is_data_ready():
    return check_row_count() > 0

gate = ShortCircuitOperator(task_id="gate", python_callable=is_data_ready)

gate >> process >> publish
 gate returns True   →  process ► publish run
 gate returns False  →  process and publish skip

Choosing the Right Tool

NeedTool
Pick one path out of severalBranchPythonOperator or @task.branch
Run or skip the whole remaining flowShortCircuitOperator
Rejoin after a branchTrigger rule none_failed_min_one_success
Placeholder for an empty pathEmptyOperator

Empty Branch Paths

A branch sometimes needs a "do nothing" option. Add an EmptyOperator as that path, so the branch always returns a valid task id.

Common Mistakes

  • Returning a task id that does not exist, which makes the branch task fail.
  • Forgetting the trigger rule on the join task.
  • Putting heavy database work inside the branch function. Keep the decision small and fast.
  • Returning a task that is not a direct child of the branch task.

Key Takeaways

  • A branch task returns the id of the path to follow.
  • Skipped paths turn pink and cause no errors.
  • A join after a branch needs a relaxed trigger rule.
  • ShortCircuitOperator stops or allows the entire downstream flow.