Repack: Airflow Xcom Example

from airflow import DAG from airflow.operators.python import PythonOperator from datetime import datetime def push_function(ti): # Pushing a specific key-value pair ti.xcom_push(key='model_accuracy', value=0.95) # Returning a value also pushes it to the 'return_value' key automatically return "Process Complete" def pull_function(ti): # Pulling from a specific task and key accuracy = ti.xcom_pull(task_ids='sender_task', key='model_accuracy') status = ti.xcom_pull(task_ids='sender_task', key='return_value') print(f"Model accuracy was accuracy with status: status") with DAG('xcom_traditional_example', start_date=datetime(2024, 1, 1), schedule=None) as dag: t1 = PythonOperator(task_id='sender_task', python_callable=push_function) t2 = PythonOperator(task_id='receiver_task', python_callable=pull_function) t1 >> t2 Use code with caution. Copied to clipboard

# Define the DAG with DAG( 'xcom_example_dag', default_args=default_args, schedule_interval='@daily', catchup=False, ) as dag:

Need a full working DAG? Reply “XCOM” and I’ll DM you a template. ✈️ airflow xcom example

One of the most common questions when building DAGs is: 👉 "How do I pass data from one task to another?"

You have a DAG that:

If you are using traditional operators (like PythonOperator or BashOperator ), you may need to use the ti (Task Instance) object to manually push and pull.

def pull_func(ti): value = ti.xcom_pull(task_ids='push_task', key='result') print(value) from airflow import DAG from airflow

from airflow.operators.python import PythonOperator

3/4 ⚠️ Warning: XCom is NOT for large data (no CSVs, no models). Keep it under 48KB. Use S3/GCS for big files. ✈️ One of the most common questions when

extract = PythonOperator( task_id='extract_order_task', python_callable=extract_order )