Skip to main content

How to Write a Dag with the TaskFlow API in Apache Airflow®

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

In Apache Airflow®, a Dag is a workflow: a set of tasks and the order they run in. With the TaskFlow API, you write it as ordinary Python functions. @task turns a function into a task, and @dag turns a function into the Dag that holds them. Pass one task's result to another and Airflow works out both the order and the data hand-off. This guide writes a two-task Dag on Airflow 3.4.0 and runs it.

Syntax​

Syntax
from airflow.sdk import dag, task


@dag(schedule=None)
def <dag_id>():
@task
def <first_task>():
return <value>

@task
def <second_task>(<parameter>):
...

<second_task>(<first_task>())


<dag_id>()
PartWhat it means
from airflow.sdk import dag, taskThe two decorators, from Airflow 3's Task SDK
@dag(schedule=None)Turns the function below it into a Dag. schedule=None means it only runs when you trigger it
<dag_id>The function's name becomes the Dag's ID in the UI
@taskTurns a function into a task. Its name becomes the task's ID
<second_task>(<first_task>())Calling one task with another's result makes the second run after the first, and passes the value along
<dag_id>()Calls the Dag function once, at the end of the file, so Airflow finds the Dag

Airflow's TaskFlow documentation explains that calling a task function in the Dag doesn't run it there: "you will get an object representing the XCom for the result", which other tasks can take as input. XCom, short for cross-communication, is how Airflow passes small values between tasks.

Prerequisites​

  • Apache Airflow® running, for example with airflow standalone, and its dags folder in your AIRFLOW_HOME.

Step 1: Import the Decorators​

Create a file in the dags folder, dags/taskflow_hello.py, and start it with:

dags/taskflow_hello.py
from airflow.sdk import dag, task

airflow.sdk is the package Dag files import from in Airflow 3.

Step 2: Define the Dag​

dags/taskflow_hello.py
@dag(schedule=None, tags=["demo"])
def taskflow_hello():
...

The Dag's ID is the function name, taskflow_hello. tags adds labels that you can filter by in the UI.

Step 3: Add Two Tasks​

Inside the Dag function:

dags/taskflow_hello.py
@task
def get_name() -> str:
return "Ada"

@task
def say_hello(name: str) -> None:
print(f"Hello, {name}!")

get_name returns a value; say_hello takes one and prints a greeting. The -> str and name: str type hints are optional, but they document what flows between the tasks.

Step 4: Connect Them, and Call the Dag​

Still inside the Dag function, after the two tasks:

dags/taskflow_hello.py
say_hello(get_name())

And at the very end of the file, outside it:

dags/taskflow_hello.py
taskflow_hello()

say_hello(get_name()) reads like a normal function call, but in a Dag it makes get_name run first and hands its return value to say_hello. The last line creates the Dag: the documentation is explicit that declaring a function with @dag isn't enough, "you must also call it at least once".

The complete file:

dags/taskflow_hello.py
from airflow.sdk import dag, task


@dag(schedule=None, tags=["demo"])
def taskflow_hello():
@task
def get_name() -> str:
return "Ada"

@task
def say_hello(name: str) -> None:
print(f"Hello, {name}!")

say_hello(get_name())


taskflow_hello()

The taskflow_hello.py Dag file in an editor window: the import from airflow.sdk, the @dag function with the get_name and say_hello tasks, the call that connects them, and the call that creates the Dag.

Step 5: Find It in the UI​

Airflow's Dag processor checks the dags folder every few seconds; airflow standalone prints the interval when it starts. Open Dags in the UI:

The taskflow_hello Dag in the Apache Airflow® Dags list, tagged demo, with its pause toggle off.

taskflow_hello is listed with its demo tag. New Dags start paused, which the toggle on the right shows.

Step 6: Run It and Look at the Graph​

Trigger the Dag with the ▷ button (How to Trigger a Dag from the Apache Airflow® UI walks through the dialog). Then open the Dag and its Graph view:

The taskflow_hello Dag page in Graph view: get_name and say_hello as two @task boxes joined by a line.

The graph shows the order Airflow worked out from say_hello(get_name()): get_name first, then say_hello. The Dag's page also shows the latest run with a green check: both tasks succeeded.

Step 7: Read the Greeting in the Log​

Click the say_hello task of that run:

The log of the say_hello task instance: Success, and the lines INFO - Hello, Ada! and INFO - Done. Returned value was: None.

Hello, Ada! is the print from say_hello, with the name that get_name returned. Done. Returned value was: None is Airflow noting that say_hello itself returns nothing. How to Read a Task Log in the Apache Airflow® UI explains the rest of the log page.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the last line, taskflow_hello()There's no Dag: the documentation says you must call the functionCall the Dag function once at the end of the file
from airflow.decorators import dag, task, the Airflow 2 pathIt still works in 3.4.0, with a warning: The `airflow.decorators.dag` attribute is deprecated. Please use `'airflow.sdk.dag'`.Import from airflow.sdk
Returning something large from a taskXComs "are only designed for small amounts of data", says Airflow's XCom documentationSave large data to a file or database, and pass its location
Putting the file outside dagsAirflow never sees itSave it in $AIRFLOW_HOME/dags

Troubleshooting​

SymptomLikely causeFix
The Dag doesn't appearThe Dag processor hasn't read the file yet, or the file isn't in dagsWait a few seconds; check the folder
The run doesn't startThe Dag is pausedUnpause it, or let the trigger dialog unpause it
The greeting isn't in the logYou opened get_name's logOpen say_hello

FAQ​

Do I need operators to write a Dag?​

No. TaskFlow tasks are plain Python functions. Operators are the other way of writing tasks, and both can be mixed in one Dag.

Where did "Ada" travel between the tasks?​

Through an XCom: Airflow stored get_name's return value and handed it to say_hello as name.

Conclusion​

@dag and @task from airflow.sdk turned three small functions into a Dag with two tasks, and say_hello(get_name()) set both their order and the hand-off of "Ada". The Graph view showed get_name before say_hello, and say_hello's log printed Hello, Ada!.

Additional Resources​

  • TaskFlow — how TaskFlow passes values and sets dependencies.
  • Dags — the Dag decorator and how Airflow loads Dag files.
  • TaskFlow tutorial — a longer official example.
  • XComs — what XComs are and their size limits.

Apache®, Apache Airflow and Airflow® are either registered trademarks or trademarks of The Apache Software Foundation in the United States and/or other countries. No endorsement by The Apache Software Foundation is implied by the use of these marks.