ZhangZhihui's Blog  

Short answer:
from airflow.sdk import DAG is better for Airflow 3 (and later).
from airflow import DAG is the old import style for Airflow 2.x.


Why there are two ways

Airflow 3 introduced a new, cleaner, more structured module layout called the Airflow SDK.

Airflow 2.x

from airflow import DAG

This works, but Airflow 2 used a large monolithic package where everything lived under airflow.* in inconsistent locations.

Airflow 3.x

from airflow.sdk import DAG

This is the new recommended import path.


Why airflow.sdk is preferred

airflow.sdk is designed to:

✔ Provide a stable, documented API surface

Airflow 2 had many modules that were not part of the stable API, so code broke when internals changed.

✔ Reduce breaking changes in the future

The SDK creates a clean compatibility layer that is intended to remain stable across Airflow versions.

✔ Enable new features (e.g., improved typing, Task API improvements)

✔ Organize operators, hooks, taskflow, and decorators more cleanly

Examples:

from airflow.sdk import DAG
from airflow.sdk import task, Param
from airflow.sdk.operators.bash import BashOperator

 


Why you should avoid from airflow import DAG in new code

  • It is tied to Airflow’s internal module structure

  • It may break after upgrades

  • It is not part of the future-stable API

  • Airflow 3 deprecates many old import paths

Airflow maintainers are migrating everything toward airflow.sdk.


🧪 Compatibility

Airflow versionairflow import DAGairflow.sdk import DAG
2.7–2.9 ✔ Works ✔ Available (introduced in 2.7)
3.x ⚠ Deprecated ✔ Recommended
Future ❌ May break ✔ Stable

🎯 Which one should you use?

If you are writing new code, use:

from airflow.sdk import DAG

If you are maintaining older Airflow 2 DAGs, and you cannot upgrade yet, you can keep:

from airflow import DAG

—but migration is recommended.

 

posted on 2025-12-10 21:02  ZhangZhihuiAAA  阅读(2)  评论(0)    收藏  举报