- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Data
Pass Data Between Tasks with XComs in Apache Airflow 3
Airflow tasks run in separate processes, so they can't share variables the way functions in a script do. In this lab, you build a retail order pipeline that passes data between tasks with XComs. You work with XComs through the task instance, then rebuild the same pipeline with the TaskFlow API, where Airflow passes values between tasks for you. You finish by fixing a task that breaks when it tries to return more than one value.
Lab Info
Table of Contents
-
Challenge
Push a value to XCom
Goal: Return a value from a task and confirm Airflow stores it as an XCom.
You are a data engineer at Globomantics. The retail ETL Dag you inherited moves data between tasks with hand-written
xcom_pushandxcom_pullcalls. Over three steps you will return values from tasks, pull them downstream, and finish by chaining the whole pipeline with the TaskFlow API.How to run the Dag. In the Terminal tab, click Run at the bottom right, or type
bash run_dag.sh. Either one registers your latest file and runs the whole Dag in one process. A good run ends withstate=success. Run it after every edit, because Airflow only sees changes when it runs again.Stuck? The
solutions/folder holds the finishedxcom_lab_dag.pyfor every task.Airflow 3 spells it Dag in its own documentation and release notes, so this lab does too.
-
Challenge
Pull a value in a downstream task
Goal: Consume the extract task's XCom value inside a second task using the classic pull API.
A task reads another task's XCom with
ti.xcom_pull(task_ids=...). By default it reads thereturn_valuekey. To read a value that was pushed under another key, passkey=.... If you pull from a task that has not run, you getNoneinstead of an error.Run
bash run_dag.shafter each edit. If you get stuck, the matching file insolutions/shows the finished Dag. -
Challenge
Chain a full extract, transform, load sequence
Goal: Connect three tasks end to end with automatic XCom passing, then fix a task that returns more than one value.
The TaskFlow API removes the manual calls. Decorate a function with
@taskand whatever it returns becomes its XCom. Call one task with the result of another, such astransform(count), and Airflow wires the dependency for you. A decorated function only becomes a task once you call it inside thewith DAG(...)block.A returned tuple is stored as one value.
@task(multiple_outputs=True)with a returned dict stores each key as its own XCom.You convert one function per task, and the Dag stays runnable after each one, so run
bash run_dag.shas you go. What you built: You took an ETL Dag that moved data with hand-writtenxcom_pushandxcom_pullcalls and turned it into a chained TaskFlow pipeline. A task now stores whatever it returns, the next task receives it as an argument, andmultiple_outputs=Truesplits a returned dict into separately addressable XCom keys.Moving this to a deployment. The tasks, the XCom handoffs, and the dependencies carry over unchanged. What changes is the schedule, which becomes a cron string instead of
None, and the file lives in the deployment's Dag bundle where the scheduler parses it.Where to go next
- Return a file path instead of a large value. XCom is meant for small handoffs.
- Replace the single
transformwith dynamic task mapping so each city becomes its own task instance. - Give the Dag a daily schedule and watch Airflow create a run for every day since
start_date.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.