Enhancement: Support Dynamic dag_id Resolution in TriggerDagRunOperator for Clickable Links
Body
Currently, when using TriggerDagRunOperator with a dynamically determined dag_id (e.g., from XCom or templated fields), the Airflow web UI cannot generate a proper link to the triggered DAG at parse time. This results in a broken or invalid link (e.g., {{ task_instance.xcom_pull(...) }}) or a “DAG not found” error, even though the triggered DAG actually runs successfully.
Use Case / Motivation
We have pipelines that need to trigger different DAGs dynamically, depending on runtime conditions (e.g., data or metadata that arrives in real-time).
While the operator itself correctly triggers the desired DAG, the UI link to the triggered DAG is not usable.
Users rely on the UI link to quickly navigate to the triggered DAG’s run details, so a broken link complicates monitoring and debugging.
Proposed Solution
Allow the Airflow UI to update or determine the dag_id link after the task is executed, possibly by reading from XCom or storing the resolved dag_id in the metadata DB.
Provide a mechanism (e.g., an extra link or a new operator parameter) to supply the final dag_id to the UI once it’s known at runtime.
Alternatively, enhance TriggerDagRunOperator to set the correct link in the UI if dag_id is determined during the operator’s execution.
def decide_which_dag(**kwargs):
# Some dynamic logic here
return "some_dynamic_dag_id"
trigger_dynamic = TriggerDagRunOperator(
task_id="trigger_dynamic_dag",
trigger_dag_id="{{ python_callable_returned_id }}", # or from XCom
wait_for_completion=True,
dag=dag,
)
in the above scenario, the DAG is triggered correctly, but the link displayed in the UI is not valid.
Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.
Can I work on this?
Can I work on this?
Sure! Feel free to work on it. Let me know if you need any help or clarification!
Hello is this still being worked on, if it is available can I work on this ?
Hello is this still being worked on, if it is available can I work on this ?
Yes!
Hi @pauloenrique071 , I already explored and reproduced the issue and interested in working on this, so can I proceed?
Hi @pauloenrique071 , I already explored and reproduced the issue and interested in working on this, so can I proceed?
Sure, feel free!
Hey, just wanted to check -- is this still being worked on?
@pauloenrique071 Could you please review my PR when you get chance? Thanks!
Hi @potiuk @pauloenrique071,
I'd like to pick this up and submit a fresh, minimal PR.
I reviewed the previous iteration and review feedback on #56973. My proposed implementation keeps the scope strictly focused on resolving the dynamic DAG link:
- Airflow 3 Execution Time (
task_runner.py): Pushtrigger_dag_idalongsidetrigger_run_idin_handle_trigger_dag_runonly after a successful trigger response. This ensures failure-mode state stays consistent across both XComs and prevents ghost values. - Operator Link (
trigger_dagrun.py): InTriggerDagRunLink.get_link(), checkXCom.get_value(ti_key=ti_key, key=XCOM_DAG_ID)using an explicitis Noneidentity check, falling back tooperator.trigger_dag_idif absent. Also pushXCOM_DAG_IDin_trigger_dag_af_2for Airflow 2 compatibility. - Tests: Update
test_trigger_dagrun.pyandtest_task_runner.pywhile strictly preserving ordered call assertions (assert_has_calls) rather than loose matching.
I will open a focused PR shortly!