The Airflow context is a dictionary containing information about a running DAG and its Airflow environment that can be accessed from a task. One of the most common values to retrieve from the Airflow context is the ti / task_instance keyword, which allows you to access attributes and methods of the taskinstance object.
Other common reasons to access the Airflow context are:
Use this document to learn about the data stored in the Airflow context and how to access it.
To get the most out of this guide, you should have an understanding of:
The Airflow context is available in all Airflow tasks. You can access information from the context using the following methods:
**context argument to the function used in a @task decorated task or PythonOperator.context argument to a @asset decorated function. See Assets and data-aware scheduling for more information.kwarg in the .execute method of any traditional or custom operator.You cannot access the Airflow context dictionary outside of an Airflow task.
@task decorator or PythonOperatorTo access the Airflow context in a @task decorated task or PythonOperator task, you need to add a **context argument to your task function. This will make the context available as a dictionary in your task.
The following code snippets show how to print out the full context dictionary from a task:
Many elements of the Airflow context can be accessed by using Jinja templating. You can get the list of all parameters that allow templates for any operator by printing out its .template_fields attribute.
For example, you can access a DAG run’s logical date in the format YYYY-MM-DD by using the template {{ ds }} in the bash_command parameter of the BashOperator.
It is also common to use Jinja templating to access XCom values in the parameter of a traditional task. In the code snippet below, the first task return_greeting will push the string “Hello” to XCom, and the second task greet_friend will use a Jinja template to pull that value from the ti (task instance) object of the Airflow context and print Hello friend! :) into the logs.
Find an up to date list of all available templates in the Airflow documentation. Learn more about using XComs to pass data between Airflow tasks in Pass data between tasks.
In a traditional operator, the Airflow context is always passed to the .execute method using the context keyword argument. If you write a custom operator, you have to include a context kwarg in the execute method as shown in the following custom operator example.
This section gives an overview of the most commonly used keys in the Airflow context dictionary. To see an up-to-date list of all keys and their types, view the Airflow source code.
The ti or task_instance key contains the TaskInstance object. The most commonly used attributes are .xcom_pull and .xcom_push, which allow you to push and pull XComs.
The following DAG shows an example of using context["ti"].xcom_push(...) and context["ti"].xcom_pull(...) to explicitly pass data between tasks.
The downstream_task will print the following information to the logs:
One of the most common reasons to access the Airflow context in your tasks is to retrieve information about the scheduling of their DAG. A common pattern is to use the timestamp of the logical date in names of files written from a DAG to create a unique file for each DAG run.
The task below creates a new text file in the include folder for each DAG run with the timestamp in the filename in the format YYYY-MM-DDTHH:MM:SS+00:00. Refer to Templates reference for an up to date list of time related keys in the context, and Jinja templating for more information on how to pass these values to templateable parameters of traditional operators.
The dag_run key contains the DAG run object. A commonly used attribute of the DAG run object is run_type, which indicates how the DAG was triggered.
The params key contains a dictionary of all DAG- and task-level params that were passed to a specific task instance. Individual params can be accessed using their respective key.
Learn more about params in the Airflow params guide.
The var key contains all Airflow variables of your Airflow instance. Airflow variables are key-value pairs that are commonly used to store instance-level information that rarely changes.
Your dag run type, i.e. scheduled vs asset-triggered can determine which timestamp keys are available in the context. The following code snippet contains a task that prints out the full list of context keys available, as well as all keys relating to scheduling timestamps.
Note that if your DAG is triggered by an asset or if you created a manual / API triggered run and set the logical date explicitly to None, the following keys will be missing from the context dictionary and trying to access them will raise a KeyError:
logical_datedsds_nodashtsts_nodashdata_interval_startdata_interval_endprevious_data_interval_start_successprevious_data_interval_end_success