Skip to content
Silver 4 min read · Control flow 1 practice problem ↓

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

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.

Python · dags/release_digest.py · counts and a path travel; the data stays put
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 \ runMar 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

Python · xcom_pull in a template and in Python
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?"

Not through XCom: XComs live in the metadata database by default and are meant for small values. A writes the result to object storage or a table, partitioned by the run's date so reruns overwrite it, and returns the path or table name; B takes that reference as its argument (TaskFlow) or pulls it with xcom_pull. If the platform passes larger values routinely, configure an object storage XCom backend, but the data still lives in storage, not the database.

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?"
With a template: {{ ti.xcom_pull(task_ids='producer') }} in bash_command, which is a template field. Make sure the producer is upstream of the BashOperator.
Scenario"The Airflow UI got slow and the metadata database is huge. What do you check first?"
XCom: tasks returning big values (lists of records, DataFrames converted to JSON) bloat the database and every page that loads a run. Change them to return references, configure an object storage XCom backend if needed, and clean up old XCom rows with airflow db clean.
Trap"xcom_pull also makes the producer run first."
It does not create an ordering dependency. TaskFlow arguments do; with xcom_pull you must set the dependency yourself.

Check yourself

3 questions

Where 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 read
Sensors and deferrable operators
Wait for a file, a partition or another DAG without blocking a worker slot.

Related lessons

Previous: Branching and short-circuiting

Primary sources: XComs · TaskFlow