Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Astronomer Astronomer cosmos Get Airflow Task

From Leeroopedia
Revision as of 14:30, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Astronomer_Astronomer_cosmos_Get_Airflow_Task.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

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

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment