You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
context doesn't contain TaskIntance when used in on_*_callback at the DAG level #51402
When using the on_*_callback at the dag level, the context that gets passed to the notify function only contains these keys
dag
run_id
reason
When it's called at a task level these keys are present
dag
inlets
map_index_template
outlets
run_id
task
task_instance
ti
outlet_events
inlet_events
macros
params
var
conn
dag_run
triggering_asset_events
task_instance_key_str
task_reschedule_count
prev_start_date_success
prev_end_date_success
logical_date
ds
ds_nodash
ts
ts_nodash
ts_nodash_with_tz
data_interval_end
data_interval_start
prev_data_interval_start_success
prev_data_interval_end_success
templates_dict
What you think should happen instead?
The keys should be the same when used at a DAG level and a Task level, and should populated similarly, i.e., for task related keys should use the last task that completed in a DAG
How to reproduce
Create a custom notifier to be able to see the contents of context. Use the on_*_callback parameter at the DAG at task level using the TaskFlowAPI, not sure if this matters or not.
Operating System
Ubuntu
Versions of Apache Airflow Providers
No response
Deployment
Other Docker-based deployment
Deployment details
Custom docker container using apache/airflow:3.0.2rc1 as the base
changed the title [-]`context` doesn't contain `TaskIntance` when used in `on_success_callback` at the `DAG` level[/-][+]`context` doesn't contain `TaskIntance` when used in `on_*_callback` at the `DAG` level[/+]on Jun 4, 2025
This is the expected behavior - on_success_callback and on_failure_callback are applied when a DAG enters a "Success" or "Failed" terminal state, not when a Task does. This means that no TaskInstance-level information would be available here.
That makes sense but it also seems like something that changed between V2 and V3. Is there a way to get the V2 behavior in V3, i.e., a different parameter to use at the DAG level?
I think the hard thing here is that we don't have information about the Task that is causing this DAG to move into a terminal state (whether that is success or failure) in the object itself. Was this functionality something that you had working in a certain version of Airflow 2.X?
Hi, I know it's closed, as are its similar issues, but is there some work around to get the on_failure_handler to be able to use the log_url of the task instance or the email_on_failure=True to work with the default email body that includes the mark_success_url?
TLDR: Where can I get a list of what is or is not on task_instance at that time?
I also tried to work my way through what's going on here. The TaskRunner in task.sdk is using a model of TaskInstance that is not the one from Airflow core but rather imported from _generated And I can't quite see where log_url and mark_success_url are dropped from those, but they are. Then the default email handler body gets an exception when rendering with mark_success_url which at least is logged, while my on_failure_handler that uses the log_url is failing silently without leaving a trace in the logs. I think I can fix the logging to occur, and remove the references, but… Where can I get a list of what is or is not on task_instance at that time?
Activity
[-]`context` doesn't contain `TaskIntance` when used in `on_success_callback` at the `DAG` level[/-][+]`context` doesn't contain `TaskIntance` when used in `on_*_callback` at the `DAG` level[/+]jroachgolf84 commentedon Jun 4, 2025
@jnstockley, can you provide a snippet of your DAG-definition? I'm mostly curious in which callbacks you are setting at the DAG-level.
jnstockley commentedon Jun 4, 2025
@jroachgolf84 I tired it with
on_success_callbackandon_failure_callbackand got the same results.`
jroachgolf84 commentedon Jun 4, 2025
This is the expected behavior -
on_success_callbackandon_failure_callbackare applied when a DAG enters a "Success" or "Failed" terminal state, not when a Task does. This means that noTaskInstance-level information would be available here.The docs are a bit mis-leading, and I've got an open PR to address this: https://github.com/apache/airflow/pull/51150/files
jroachgolf84 commentedon Jun 4, 2025
That being said, I think there are items on that list that you shared that would be worth thinking about bringing over...
jnstockley commentedon Jun 4, 2025
That makes sense but it also seems like something that changed between V2 and V3. Is there a way to get the V2 behavior in V3, i.e., a different parameter to use at the DAG level?
jroachgolf84 commentedon Jun 4, 2025
@jnstockley, this is where this context is set.
https://github.com/apache/airflow/blob/main/airflow-core/src/airflow/models/dagrun.py#L1327-L1333
It appears that this change was introduced last month by this PR: https://github.com/apache/airflow/pull/50300/files.
jroachgolf84 commentedon Jun 4, 2025
Are there additional fields that you are interested in bring in? Hypothetically, this would be a very light-lift to implement.
jnstockley commentedon Jun 4, 2025
@jroachgolf84 Ideally I would love either
task_instanceortior at least these fields fromtask_instancestateI'm assumingreasonwould be the same but I could be wrongjroachgolf84 commentedon Jun 5, 2025
I think the hard thing here is that we don't have information about the Task that is causing this DAG to move into a terminal state (whether that is success or failure) in the object itself. Was this functionality something that you had working in a certain version of Airflow 2.X?
4 remaining items
jroachgolf84 commentedon Jun 13, 2025
@kiran2706, just wanted to check-in and see how things are going. Were you able to open up a PR for this?
kiran2706 commentedon Jun 14, 2025
@jroachgolf84 - It’s on my to-do list for Sunday.
Alexbobaz commentedon Jul 11, 2025
Is there a timeline for this fix?
What alternatives are there? Add an on_x_callback on every step in the DAG?
Restore proper DAG callback execution context (apache#53684)
Restore proper DAG callback execution context (#53684)
dlamblin commentedon May 19, 2026
Hi, I know it's closed, as are its similar issues, but is there some work around to get the
on_failure_handlerto be able to use thelog_urlof the task instance or theemail_on_failure=Trueto work with the default email body that includes themark_success_url?TLDR: Where can I get a list of what is or is not on task_instance at that time?
Right now AWS MWAA only provides 3.0.6 of the 3 familiy. And I see signs in this search that this affects 3.0 and 3.1, I would love to see a link to the fixes in 3.x that address it. https://github.com/apache/airflow/issues?q=sort%3Aupdated-desc%20is%3Aissue%20mark_success_url
I also tried to work my way through what's going on here. The TaskRunner in task.sdk is using a model of TaskInstance that is not the one from Airflow core but rather imported from _generated And I can't quite see where
log_urlandmark_success_urlare dropped from those, but they are. Then the default email handler body gets an exception when rendering withmark_success_urlwhich at least is logged, while myon_failure_handlerthat uses thelog_urlis failing silently without leaving a trace in the logs. I think I can fix the logging to occur, and remove the references, but… Where can I get a list of what is or is not on task_instance at that time?kaxil commentedon May 19, 2026
@dlamblin
mark_success_urlwas added in #55238 toRuntimeTaskInstance, which is what is received incontext["ti"]object:airflow/task-sdk/src/airflow/sdk/execution_time/task_runner.py
Lines 716 to 719 in b81e335
airflow/task-sdk/src/airflow/sdk/execution_time/task_runner.py
Lines 244 to 245 in b81e335