0
0
Apache Airflowdevops~5 mins

XCom with return values in Apache Airflow - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run tasks in Airflow, sometimes you want one task to send a small piece of information to another task. XComs let tasks share these little messages easily by returning values.
When a task needs to send a result like a file path or a number to another task.
When you want to pass a status or flag from one task to the next without using external storage.
When you want to chain tasks that depend on each other's output.
When you want to keep your workflow dynamic by using data generated during execution.
When you want to avoid hardcoding values and instead use live results from previous tasks.
Config File - xcom_return_example_dag.py
xcom_return_example_dag.py
from airflow import DAG
from airflow.decorators import task
from airflow.utils.dates import days_ago

with DAG(dag_id='xcom_return_example', start_date=days_ago(1), schedule_interval=None, catchup=False) as dag:

    @task
    def generate_number():
        return 42

    @task
    def print_number(number):
        print(f"Received number: {number}")

    number = generate_number()
    print_number(number)

This DAG defines two tasks using the @task decorator.

generate_number returns the number 42, which Airflow automatically stores as an XCom.

print_number receives the returned value as an argument and prints it.

This shows how returning a value from a task automatically pushes it as an XCom, and downstream tasks can receive it by using the returned variable.

Commands
List all available DAGs to confirm the new DAG is recognized by Airflow.
Terminal
airflow dags list
Expected OutputExpected
dag_id | filepath xcom_return_example | /path/to/airflow/dags/xcom_return_example_dag.py
Trigger the DAG run manually to execute the tasks and generate XComs.
Terminal
airflow dags trigger xcom_return_example
Expected OutputExpected
Created <DagRun xcom_return_example @ 2024-06-01 12:00:00+00:00: manual__2024-06-01T12:00:00+00:00, externally triggered: True>
View logs of the generate_number task to see the returned value and confirm it ran successfully.
Terminal
airflow tasks logs xcom_return_example generate_number --run-id manual__2024-06-01T12:00:00+00:00
Expected OutputExpected
[2024-06-01 12:00:01,000] INFO - Task returned value: 42 [2024-06-01 12:00:01,001] INFO - Task succeeded
--run-id - Specify the exact DAG run to view logs for
View logs of the print_number task to see the received XCom value printed.
Terminal
airflow tasks logs xcom_return_example print_number --run-id manual__2024-06-01T12:00:00+00:00
Expected OutputExpected
Received number: 42 [2024-06-01 12:00:02,000] INFO - Task succeeded
--run-id - Specify the exact DAG run to view logs for
Key Concept

If you remember nothing else, remember: returning a value from a task automatically pushes it as an XCom that downstream tasks can receive as input.

Common Mistakes
Not returning the value from the task function and trying to push XCom manually.
Without a return statement, Airflow does not automatically create the XCom, so downstream tasks get no data.
Use a return statement in the task function to send the value as an XCom automatically.
Trying to access XComs directly without passing the returned value as a parameter.
Direct XCom pulls require extra code and are more complex; passing returned values is simpler and cleaner.
Use the returned value from the upstream task as a parameter in the downstream task function.
Summary
Define tasks with @task decorator and return values to automatically create XComs.
Trigger the DAG and check task logs to see returned values and their usage.
Pass the returned value from one task as an argument to another to share data simply.