Skip to content

context doesn't contain TaskIntance when used in on_*_callback at the DAG level #51402

Closed

Description

@jnstockley

Apache Airflow version

3.0.1

If "Other Airflow 2 version" selected, which one?

No response

What happened?

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

Anything else?

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. added
    kind:bug
    needs-triage
    on Jun 4, 2025
  2. 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
  3. jroachgolf84 commented on Jun 4, 2025

    @jroachgolf84
    Collaborator

    @jnstockley, can you provide a snippet of your DAG-definition? I'm mostly curious in which callbacks you are setting at the DAG-level.

  4. jnstockley commented on Jun 4, 2025

    @jnstockley
    Author

    @jroachgolf84 I tired it with on_success_callback and on_failure_callback and got the same results.
    `

    import json
    
    import pendulum
    
    from airflow.sdk import dag, task
    
    from plugins.notifications.custom import CustomNotifier
    
    
    @dag(
        schedule=None,
        start_date=pendulum.datetime(2021, 1, 1, tz="UTC"),
        catchup=False,
        tags=["example"],
        on_success_callback=CustomNotifier(to="email-address"),
    )
    def tutorial_taskflow_api():`
    
  5. jroachgolf84 commented on Jun 4, 2025

    @jroachgolf84
    Collaborator

    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.

    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

  6. jroachgolf84 commented on Jun 4, 2025

    @jroachgolf84
    Collaborator

    That being said, I think there are items on that list that you shared that would be worth thinking about bringing over...

  7. jnstockley commented on Jun 4, 2025

    @jnstockley
    Author

    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?

  8. added
    priority:low
    and removed
    needs-triage
    on Jun 4, 2025
  9. jroachgolf84 commented on Jun 4, 2025

    @jroachgolf84
    Collaborator

    Are there additional fields that you are interested in bring in? Hypothetically, this would be a very light-lift to implement.

  10. jnstockley commented on Jun 4, 2025

    @jnstockley
    Author

    @jroachgolf84 Ideally I would love either task_instance or ti or at least these fields from task_instance

    • state I'm assuming reason would be the same but I could be wrong
    • try_number
    • max_tries
  11. jroachgolf84 commented on Jun 5, 2025

    @jroachgolf84
    Collaborator

    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?

  12. 4 remaining items

  13. jroachgolf84 commented on Jun 13, 2025

    @jroachgolf84
    Collaborator

    @kiran2706, just wanted to check-in and see how things are going. Were you able to open up a PR for this?

  14. kiran2706 commented on Jun 14, 2025

    @kiran2706
    Contributor

    @jroachgolf84 - It’s on my to-do list for Sunday.

  15. Alexbobaz commented on Jul 11, 2025

    @Alexbobaz

    Is there a timeline for this fix?
    What alternatives are there? Add an on_x_callback on every step in the DAG?

  16. added a commit that references this issue on Jul 24, 2025
    ef80507
  17. added a commit that references this issue on Aug 9, 2025
    b07017b
  18. dlamblin commented on May 19, 2026

    @dlamblin
    Contributor

    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?

    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_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?

  19. kaxil commented on May 19, 2026

    @kaxil
    Member

    @dlamblin mark_success_url was added in #55238 to RuntimeTaskInstance, which is what is received in context["ti"] object:

    @property
    def mark_success_url(self) -> str:
    """URL to mark TI success."""
    return self.log_url

    "task_instance": self,
    "ti": self,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:corekind:bugpriority:low

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions