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

Airflow自定义Operator支持Jinja模板时'ti'未定义问题解决

解决Airflow自定义Operator中Jinja模板'ti'未定义的问题

针对你遇到的自定义UtilETLStartJobRunOperator中Jinja表达式{{ ti.xcom_pull(...) }}报'ti'未定义的问题,可按以下步骤排查修复:

  • 确认template_fields的正确定义
    自定义Operator中要确保template_fields是类级属性,且正确追加而非覆盖父类的字段:

    class UtilETLStartJobRunOperator(UtilETLStartRunBaseOperator):
        template_fields = (*UtilETLStartRunBaseOperator.template_fields, 'util_etl_start_run_uuid')
    

    这样能保证父类的模板字段和自定义字段都能被Airflow的模板引擎处理。

  • 修正Jinja表达式的写法
    任务定义时,Jinja表达式内的task_ids参数不需要用引号包裹变量名,否则会被当成字符串字面量,正确写法:

    UtilETLStartJobRunOperator(
        task_id="current_task",
        util_etl_start_run_uuid="{{ ti.xcom_pull(task_ids='previous_task_id') }}",
        # 其他任务参数
    )
    
  • 确保父类execute方法触发模板渲染
    检查父类UtilETLStartRunBaseOperator的execute方法是否调用了render_template_fields,如果没有,需要在自定义Operator的execute方法中手动触发:

    def execute(self, context):
        # 先渲染所有模板字段
        self.render_template_fields(context)
        # 执行原有业务逻辑
        super().execute(context)
    
  • 弃用provide_context=True
    Airflow 2.x版本已弃用provide_context=True参数,Operator的execute方法默认接收context参数,无需额外设置;若使用1.x版本,确保该参数在任务实例上正确配置,但优先建议升级到2.x版本适配新规范。

内容的提问来源于stack exchange,提问作者Vinicius Silva

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 17:32:44