You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache Airflow出现Broken DAG错误:DbtRunLocalOperator属性异常求助

Airflow DbtDAG 出现 Broken DAG 错误:AttributeError: '_BaseOperator__init_kwargs'

问题背景

按照YouTube教程搭建Airflow的dbt-DAG,其他人操作均成功,但本地环境触发Broken DAG错误,无法加载dbt-dag.py的DAG信息。

报错信息

Broken DAG: [/usr/local/airflow/dags/dbt-dag.py]
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/cosmos/operators/base.py", line 361, in __init__
    self.full_refresh = full_refresh
    ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/airflow/models/baseoperator.py", line 1198, in __setattr__
    if key in self.__init_kwargs:
              ^^^^^^^^^^^^^^^^^^
AttributeError: 'DbtRunLocalOperator' object has no attribute '_BaseOperator__init_kwargs'. Did you mean: '_BaseOperator__instantiated'?

本地代码(dbt-dag.py)

import os
from datetime import datetime

from cosmos import DbtDag, ProfileConfig, ProjectConfig, ExecutionConfig
from cosmos.profiles import SnowflakeUserPasswordProfileMapping

profile_config = ProfileConfig(
    profile_name='default',
    target_name='dev',
    profile_mapping=SnowflakeUserPasswordProfileMapping(
        conn_id='snowflake_conn',
        profile_args={'database':'dbt_db','schema':'dbt_schema'},
    )
)

dbt_snowflake_dag = DbtDag(
    project_config=ProjectConfig('/usr/local/airflow/dags/dbt/data_pipeline',),
    operator_args={'install_deps':True},
    profile_config=profile_config,
    execution_config=ExecutionConfig(dbt_executable_path=f"{os.environ['AIRFLOW_HOME']}/dbt_venv/bin/dbt",),
    schedule_interval="@daily",
    start_date=datetime(2023, 9, 10),
    catchup=False,
    dag_id="dbt_dag",
)

解决方案

这个错误的核心是Airflow 2.8+ 与 Astronomer Cosmos 旧版本(<1.5.0)的兼容性冲突:Airflow 2.8重构了BaseOperator的私有属性命名,将__init_kwargs改为新的实现,而旧版Cosmos仍在引用旧属性名。

解决步骤:

  1. 升级Cosmos到兼容版本(推荐):
    安装1.5.0及以上版本的Astronomer Cosmos,该版本已适配Airflow 2.8+:
    pip install astronomer-cosmos>=1.5.0
    
  2. 降级Airflow到兼容版本(备选):
    如果需要保留旧版Cosmos,可将Airflow降级到2.7.x版本:
    pip install apache-airflow==2.7.3
    
  3. 额外验证项:
    • 确认dbt_executable_path指向的dbt可执行文件存在且拥有执行权限
    • 检查Airflow的snowflake_conn连接配置是否完整(包含账号、密码、仓库、区域等必要信息)

内容的提问来源于stack exchange,提问作者Tian Hock Woo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 16:54:55