Skip to content
Bronze 5 min read · Basics 1 practice problem ↓

A DAG file, line by line

In Airflow, a pipeline is a Python file that describes tasks and the order between them. This lesson writes a small one, explains each part, and shows what the scheduler does with it.

Pipelines in production · Airflow · Bronze · Orchestration basics, lesson 2 of 3

Read first: What Airflow is (and is not) (skip if you know it)

In plain words first

A recipe card lists steps and their order: you cannot ice the cake before baking it, but you can whip the cream while it bakes. It does not cook anything; it describes the cooking.

A DAG file is that recipe card for a pipeline: tasks, and arrows saying what must finish before what. Airflow reads the card and does the cooking on schedule.

A small DAG

A DAG (directed acyclic graph) is a pipeline: tasks and the order between them, with no loops. In Airflow it is a Python file in the dags folder. Here is a nightly chart pipeline for a music app, and what the schedulerscheduler: The Airflow process that reads DAG files, creates runs when they are due, and queues the tasks whose upstream tasks are done. Learn more → does with it over two nights when the aggregation fails on the second one.

Python · dags/charts_daily.py · three tasks in a chain
import pendulum
from airflow.sdk import DAG
from airflow.providers.standard.operators.bash import BashOperator

with DAG(
    dag_id="charts_daily",                      # unique across the deployment
    schedule="0 2 * * *",                       # every day at 02:00 UTC
    start_date=pendulum.datetime(2026, 3, 1, tz="UTC"),
):
    ingest = BashOperator(
        task_id="ingest_plays", bash_command="ingest_plays --day {{ ds }}"
    )
    aggregate = BashOperator(
        task_id="aggregate", bash_command="build_charts --day {{ ds }}"
    )
    publish = BashOperator(
        task_id="publish", bash_command="publish_charts --day {{ ds }}"
    )

    ingest >> aggregate >> publish             # the order: left runs before right

What the scheduler does · deployed Mar 2, 09:00 UTC · grid as of Mar 3, 03:00 UTC · failing: aggregate in the 2026-03-03 run · 0 2 * * * (CronTriggerTimetable)

task \ runMar 2
02:00
Mar 3
02:00
ingest_plays
aggregate
publish

successfailedupstream failed· the bar on top is the run's state; a number is how many tries

Deployed on 2 March at 09:00, the DAG gets one run for that morning's 02:00 slot straight away (the most recent schedule; more on that in Catchupcatchup: Whether Airflow creates runs for every past period between start_date and now when a DAG is switched on. Learn more →), then the 3 March run. On 3 March aggregate fails, so publish never runs: it ends upstream_failed, and the run is failed.

The parts

PartIn the fileNotes
DAGwith DAG(dag_id=..., schedule=..., start_date=...)dag_id must be unique; start_date is fixed, never "now"
TaskAn operatoroperator: A ready-made kind of task: BashOperator runs a command, PythonOperator a function. You make a task by giving an operator its arguments. Learn more → instance with a task_idUnique within the DAG
Dependencya >> b, [a, b] >> c, chain(a, b, c)Lists fan out and fan in

The @dag decorator

Python · The same DAG with @dag
from airflow.sdk import dag

@dag(schedule="0 2 * * *", start_date=pendulum.datetime(2026, 3, 1, tz="UTC"))
def charts_daily():                              # the function name is the dag_id
    ingest = BashOperator(
        task_id="ingest_plays", bash_command="ingest_plays --day {{ ds }}"
    )
    ...

charts_daily()                                    # without this call there is no DAG

Both forms are equivalent. With @dag, the function body only builds the DAG when the function is called, so the call at the bottom is required: a file with a decorated function and no call defines nothing, and the DAG silently never appears.

The file runs all the time

The DAG processor imports every file in the dags folder again and again (by default every 30 seconds) to pick up changes. Everything at the top level of the file runs on every parse, not when the pipeline runs. That has consequences:

  • No queries, API calls or Variable.get at the top level: they run thousands of times a day and slow down parsing for every DAG.
  • No datetime.now() as a start date: the start date would move on every parse, and runs may never be created.
  • Heavy imports (pandas, ML libraries) belong inside the task function, where they load only when the task runs.
  • A syntax error or an exception at import time makes the whole file a "Broken DAG": none of its DAGs can be scheduled until it is fixed.

Common mistakes

  • A @dag function that is never called — No DAG is created and no error is shown.
  • start_date=datetime.now() — The start keeps moving; runs may never be scheduled.
  • Database or API calls at the top level of the file — They run on every parse, all day long.
  • Reusing a dag_id in two files — Only one of the DAGs wins, and which one is unpredictable.

Key takeaways

  • A DAG file defines tasks and the order between them; it does not run them.
  • with DAG(...) and @dag are equivalent; a @dag function must be called.
  • Top-level code runs on every parse: keep it light and deterministic.
  • When a task fails, the tasks after it end upstream_failed.

Interview prep

THE QUESTION

"Why is it a bad idea to query a database at the top level of a DAG file?"

The DAG processor re-imports every DAG file continuously, every 30 seconds by default, to pick up changes. Top-level code runs on every parse, so the query runs thousands of times a day whether or not the pipeline runs, slows parsing for all DAGs, and can make the file fail to import if the database is down. Code that needs data at run time belongs inside a task; configuration that must shape the DAG should come from a file deployed with it.

Avoid: putting an API call or a database query at the top of the DAG file "to build the task list". It runs on every parse, not once per run.

Follow-ups · answer out loud, then open

Follow-up"What is the difference between a DAG, a DAG run, a task and a task instance?"
The DAG is the pipeline definition; a DAG run is one execution of it for a logical date. A task is a step in the definition; a task instance is that step inside one DAG run, with its own state, tries and logs.
Scenario"A teammate's DAG imports pandas and TensorFlow at the top of the file, and parsing of all DAGs has slowed down. What do you suggest?"
Move heavy imports inside the task functions that need them, so they load only when those tasks run, not on every parse. Check parse times in the DAG processor's statistics, and keep top-level code to building the DAG.
Trap"A cycle in the dependencies just makes the task run twice."
A DAG must be acyclic: a cycle makes the file fail to import with a cycle error, and none of its DAGs are scheduled.

Check yourself

3 questions

A file has a function decorated with @dag but no call to it. What happens?

Show the answer

No DAG is created. The decorator builds the DAG only when the function is called.

How often does top-level code in a DAG file run?

Show the answer

On every parse, every 30 seconds by default. The DAG processor re-imports files continuously.

What does [a, b] >> c mean?

Show the answer

c runs after both a and b. A list on the left fans in: c waits for both.

Practise this

Problems that use this, graded on hidden tests

Keep going

Up next · Orchestration basics · lesson 3 of 3 · 5 min read
Running Airflow 3 on your laptop
airflow standalone, the dags folder, the UI, and testing a DAG with airflow dags test.

Related lessons

Previous: What Airflow is (and is not)

Primary sources: DAGs · Best practices: top-level code