# XComs (Cross-communication)

***

### XComs

In Airflow, tasks are isolated processes, meaning they don't naturally share memory or variables. **XComs** (short for "cross-communications") act as a small storage system that allows tasks to exchange messages or data.

#### The Core Concept: Push and Pull

XComs work like a post office: one task "pushes" a value into the Airflow database, and another task "pulls" it out.

* **Pushing**: By default, any value returned by a Python function in an Airflow task is automatically pushed to XComs.
* **Pulling:** Downstream tasks can retrieve these values using the task ID and the key (which defaults to `return_value`).

***

#### XComs in the TaskFlow API (Recommended)

With the modern `@task` decorators you've been learning, XComs are almost invisible because Airflow handles them like standard Python function arguments.

```python
@task
def get_taxi_count():
    return 42  # Automatically pushed to XCom

@task
def process_count(count):
    # Airflow automatically pulls the return value of the previous task
    print(f"Processing {count} taxis.")

# The flow defines the XCom exchange
process_count(get_taxi_count())
```

***

#### Traditional XComs (Manual Push/Pull)

If you are using traditional operators like the `PythonOperator`, you often interact with XComs through the **Task Instance** (`ti`) object provided in the context dictionary.

| **Action**  | **Code Example**                                             |
| ----------- | ------------------------------------------------------------ |
| Manual Push | `ti.xcom_push(key="model_accuracy", value=0.95)`             |
| Manual Pull | `ti.xcom_pull(task_ids="train_model", key="model_accuracy")` |

***

#### XComs in Jinja Templates

You can also pull XCom values directly into your SQL queries or Bash commands using templates.

```python
# Pulling a value into a SQL query
query = "SELECT * FROM rides WHERE count > {{ ti.xcom_pull(task_ids='get_count') }}"
```

***

#### Important Constraints: The "Small Data" Rule

XComs are stored in the Airflow Metadata Database (Postgres, MySQL, etc.). Because of this, you must follow these rules:

* **Size Limits**: Do not use XComs to pass large datasets (like a 1GB CSV or a massive Pandas DataFrame). This will bloat your database and slow down Airflow.
* **Alternative:** For large data, write the file to S3, GCS, or a local path, and use XComs to pass only the **file path** (a string).
* **Serialization:** Data must be JSON-serializable (strings, ints, lists, dicts) unless you have configured a custom XCom backend.

***

#### Visualizing XComs in the UI

If a task fails because of a data issue, you can check exactly what was passed:

1. Go to the **Grid View.**
2. Click on the specific task run.
3. Click the **XCom** tab in the details panel. This shows you a table of every key and value that task produced.

***
