XCom: passing data between tasks
Tasks sometimes need a small piece of information from an earlier task: a row count, a file path, a batch id. XCom passes it. This lesson shows how, how TaskFlow makes it automatic, and why it must stay small.
Pipelines in production · Airflow · Silver · Control flow, lesson 2 of 3
On this page
Read first: Operators and TaskFlow (skip if you know it)
In plain words first
Workersworker: The process that actually runs a task instance, on whatever machine the executor sends it to. Learn more → on different shifts leave each other sticky notes: "batch 42 loaded, 10,000 rows". They do not leave the whole delivery on the desk.
XCom is the sticky note between taskstask: One step of a DAG, such as running a query or calling an API. Each run of a task for one date is a task instance. Learn more →. The data itself belongs in storage; XCom carries its path or a count.
Small messages between tasks
XCom ("cross-communication") lets a task leave a small value for later tasks of the same run: a row count, a file path, a batch id. A task's return value is pushed as the XCom return_value; TaskFlowTaskFlow: Airflow's decorator style: @task turns a Python function into a task, and return values pass between tasks automatically. Learn more → wires it into the next function's arguments. XComs are stored by Airflow (in the metadata databasemetadata database: The database where Airflow records every DAG run, task instance, state and setting. Learn more → by default), serialized as JSON, and kept with the run's history.
import pendulum from airflow.sdk import dag, task @dag( schedule="@daily", start_date=pendulum.datetime(2026, 3, 1, tz="UTC"), catchup=True, ) def release_digest(): @task def export_releases(ds=None): path = f"s3://music-lake/exports/releases/dt={ds}/" # the data is written here return {"path": path, "rows": 120 if ds == "2026-03-01" else 87} @task def check(export): if export["rows"] == 0: raise ValueError("empty export") return export["rows"] @task def digest(rows, export): return f"{rows} new releases in {export['path']}" export = export_releases() digest(check(export), export) release_digest()
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 · deployed Mar 1, 00:00 UTC · grid as of Mar 2, 01:00 UTC · @daily (CronTriggerTimetable)
| task \ run | Mar 1 00:00 | Mar 2 00:00 |
|---|---|---|
| export_releases | ||
| check | ||
| digest |
success· the bar on top is the run's state; a number is how many tries
Return value of digest, run by run
Mar 1, 00:00 '120 new releases in s3://music-lake/exports/releases/dt=2026-03-01/' Mar 2, 00:00 '87 new releases in s3://music-lake/exports/releases/dt=2026-03-02/'
Pulling from classic operators
BashOperator( task_id="announce", bash_command="announce --rows {{ ti.xcom_pull(task_ids='check') }}", ) @task def summary(ti=None): rows = ti.xcom_pull(task_ids="check") # the return_value of check files = ti.xcom_pull(task_ids="export_releases", key="files") ...
ti.xcom_pull(task_ids=...) reads another task's return value (or another key pushed with ti.xcom_push). It only creates a value dependency, not an ordering one: make sure the producer is upstream, or you may pull nothing.
What not to send
- No datasets: a DataFrame or a list of a million rows ends up serialized into the metadata database, slowing every page that shows the run. Write the data to storage and pass its path or table name.
- Only JSON-friendly values (strings, numbers, lists, dicts, dates): Airflow 3 serializes XComs, and arbitrary Python objects fail.
- If large results really must travel, configure an object storage XCom backend so values above a size threshold are written to S3 or GCS and only a reference is stored.
Several values from one task
@task(multiple_outputs=True) (or a return type hint of dict) pushes each key of the returned dict as its own XCom, so downstream tasks can take export["path"] as a separate input. Without it the dict is one value, as in the example above, which works just as well when the consumers want the whole thing.
Common mistakes
- Returning a DataFrame or a big list — It is stored in the metadata database, or fails to serialize.
- xcom_pull from a task that is not upstream — It may run first and pull nothing.
- Using XCom as a cache between runs — XComs belong to one run; use a table or Variable for state across runs.
- Returning non-JSON objects (a client, a cursor) — Serialization fails and the task fails.
Key takeaways
- XCom carries small values between tasks of one run.
- TaskFlow passes return values as arguments; classic operators use ti.xcom_pull.
- Pass paths and table names, never datasets.
- multiple_outputs splits a returned dict into separate XComs.
Interview prep
THE QUESTION
"Task A produces a 2 GB result that task B needs. How do you pass it?"
Avoid: "pass the DataFrame between tasks with XCom". XComs are small values in Airflow's database, not a data transport.
Follow-ups · answer out loud, then open
Follow-up"How do you read another task's result in a BashOperator?"
Scenario"The Airflow UI got slow and the metadata database is huge. What do you check first?"
Trap"xcom_pull also makes the producer run first."
Check yourself
3 questionsWhere are XComs stored by default?
Show the answer
In Airflow's metadata database. Unless an object storage backend is configured.
What should a task return when its output is a large table?
Show the answer
The path or table name where it wrote it. Pass a reference; keep the data in storage.
What does multiple_outputs=True do?
Show the answer
Pushes each key of a returned dict as its own XCom. Each key becomes a separate value downstream tasks can use.
Practise this
Problems that use this, graded on hidden tests
Keep going
Up next · Control flow · lesson 3 of 3 · 5 min readSensors and deferrable operators
Wait for a file, a partition or another DAG without blocking a worker slot.
Related lessons
Operators and TaskFlowAirflow · 4 min read
Branching and short-circuitingAirflow · 4 min read
Dynamic task mapping
Previous: Branching and short-circuiting