Implementation:Astronomer Astronomer cosmos Get Airflow Task
| Knowledge Sources | |
|---|---|
| Domains | Orchestration, Task_Conversion |
| Last Updated | 2026-02-07 17:00 GMT |
Overview
get_airflow_task converts a Cosmos Task entity into a native Airflow BaseOperator, bridging the Cosmos graph model with the Airflow execution engine.
Description
The get_airflow_task function is the primary translation layer between the Cosmos internal task representation and Apache Airflow's operator model. It accepts a Cosmos Task data class, along with the target DAG and an optional TaskGroup, and returns a fully configured BaseOperator instance that Airflow can schedule and execute.
Internally the function inspects the operator_class attribute on the Task entity, instantiates the corresponding Airflow operator, and wires up any arguments and extra context that were attached to the task during graph construction. When a TaskGroup is provided the resulting operator is registered within that group, enabling hierarchical DAG visualisation in the Airflow UI.
This function sits at line 25 of a compact 55-line module, reflecting its focused responsibility as a thin adapter rather than a complex transformation pipeline.
Usage
Use get_airflow_task whenever you need to materialise a Cosmos DAG graph into runnable Airflow tasks. This is typically called during the DAG rendering phase, after the Cosmos graph of Task and Group entities has been fully constructed. It is essential for any custom rendering strategy that converts Cosmos entities into Airflow-native objects.
Code Reference
Source Location
- Repository: Astronomer_Astronomer_cosmos
- File: cosmos/core/airflow.py
- Lines: 25 onward (module is 55 lines total)
Signature
def get_airflow_task(
task: Task,
dag: DAG,
task_group: TaskGroup | None = None,
) -> BaseOperator:
...
Import
from cosmos.core.airflow import get_airflow_task
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| task | Task | Yes | A Cosmos Task entity containing operator_class, arguments, and extra_context |
| dag | DAG | Yes | The Airflow DAG instance to which the resulting operator will be attached |
| task_group | TaskGroup or None | No | An optional Airflow TaskGroup for nesting the operator within a visual group |
Outputs
| Name | Type | Description |
|---|---|---|
| operator | BaseOperator | A fully configured Airflow operator instance ready for scheduling and execution |
Usage Examples
from airflow import DAG
from cosmos.core.graph.entities import Task
from cosmos.core.airflow import get_airflow_task
with DAG(dag_id="example_cosmos_dag") as dag:
cosmos_task = Task(
id="run_model",
operator_class="cosmos.operators.local.DbtRunLocalOperator",
arguments={"models": "my_model"},
)
airflow_operator = get_airflow_task(task=cosmos_task, dag=dag)
from airflow.utils.task_group import TaskGroup
from cosmos.core.airflow import get_airflow_task
with DAG(dag_id="grouped_dag") as dag:
with TaskGroup(group_id="dbt_models") as tg:
for task_entity in cosmos_group.entities:
get_airflow_task(task=task_entity, dag=dag, task_group=tg)
Related Pages
- Environment:Astronomer_Astronomer_cosmos_Python_Airflow_Runtime
- Astronomer_Astronomer_cosmos_Graph_Entities -- defines the Task data class consumed by this function