---
title: "Get an XCom value in the Airflow on_failure_callback function"
description: "How to get the task instance in the on_failure_callback to get access to XCom"
author: "Bartosz Mikulski"
author_bio: "Principal AI Engineer & MLOps Architect. I bridge the gap between \"it works in a notebook\" and \"it works for 200 million users.\""
author_url: https://mikulskibartosz.name
author_linkedin: https://www.linkedin.com/in/mikulskibartosz/
author_github: https://github.com/mikulskibartosz
canonical_url: https://mikulskibartosz.name/xcom-value-in-airflow-on-failure-callback
---

When we want to get an XCom variable in the on_failure_callback, we will face a nasty bug. Using the `ti` key to retrieve a value from the context gives us a None value.

```python
# This does not work
context.get("ti").xcom_pull(key="test")
```

It happens because in the on_failure_callback the task instance is passed to the function using the `task_instance` key. Therefore to get a value from XCom, we must execute this code:

```python
task: TaskInstance = context.get('task_instance')
task.xcom_pull(key="test")
```

Alternatively, we can import XCom and access it directly:

```python
from airflow.models import XCom
XCom.get_one(execution_date = context.get('execution_date'), key='test')
```

