How to Write a Dag with the TaskFlow API in Apache Airflow®
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
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>()
| Part | What it means |
|---|---|
from airflow.sdk import dag, task | The 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 |
@task | Turns 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 itsdagsfolder in yourAIRFLOW_HOME.
Step 1: Import the Decorators
Create a file in the dags folder, dags/taskflow_hello.py, and start it with:
from airflow.sdk import dag, task
airflow.sdk is the package Dag files import from in Airflow 3.
Step 2: Define the Dag
@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:
@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:
say_hello(get_name())
And at the very end of the file, outside it:
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:
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()

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:

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 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:

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
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out the last line, taskflow_hello() | There's no Dag: the documentation says you must call the function | Call the Dag function once at the end of the file |
from airflow.decorators import dag, task, the Airflow 2 path | It 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 task | XComs "are only designed for small amounts of data", says Airflow's XCom documentation | Save large data to a file or database, and pass its location |
Putting the file outside dags | Airflow never sees it | Save it in $AIRFLOW_HOME/dags |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The Dag doesn't appear | The Dag processor hasn't read the file yet, or the file isn't in dags | Wait a few seconds; check the folder |
| The run doesn't start | The Dag is paused | Unpause it, or let the trigger dialog unpause it |
| The greeting isn't in the log | You opened get_name's log | Open 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.
