Skip to content

Conversation

@kaxil
Copy link
Member

@kaxil kaxil commented Jul 22, 2025

Add get_previous_dagrun method to RuntimeTaskInstance to replace the removed DagRun.get_previous_dagrun() method from Airflow 2.x. This enables users to access previous DAG run information from task context for conditional logic like notifications on state changes.

Changes:

  • Add GetPreviousDagRun message and PreviousDagRunResult response in comms
  • Add get_previous_dagrun instance method to RuntimeTaskInstance
  • Add /dag-runs/{dag_id}/previous API endpoint with optional state filtering
  • Add get_previous method to DagRunOperations API client
  • Update supervisor to handle GetPreviousDagRun messages

ToDo:

  • Add Cadwyn migration
  • Remove defaults when sending serialized object to buffer

Usage:

def my_task(context):
    ti = context["ti"]
    previous_run = ti.get_previous_dagrun()
    if previous_run:
        # Access previous run metadata
        print(f"Previous run: {previous_run.dag_run.run_id}")

or

@dag
def a_dag():
    @task(task_id="print_the_context")
    def print_context(**kwargs):
        ctx = get_current_context()
        dr = ctx["dag_run"]
        print("Current dag run:")
        pprint(dr)
        print()
        print("Getting prev dag run...")
        gpd = ctx["ti"].get_previous_dagrun()
        print("Previous dag run:")
        pprint(gpd)


    run_this = print_context()
image

Fixes #53628


^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in airflow-core/newsfragments.

@kaxil kaxil requested review from amoghrajesh and ashb as code owners July 22, 2025 22:23
@boring-cyborg boring-cyborg bot added area:API Airflow's REST/HTTP API area:task-sdk labels Jul 22, 2025
@kaxil kaxil force-pushed the add-dagrun-in-context branch 2 times, most recently from d429189 to 4d73a07 Compare July 23, 2025 10:22
@kaxil kaxil requested a review from mobuchowski as a code owner July 23, 2025 10:22
Copy link
Contributor

@amoghrajesh amoghrajesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a comment, looks good otherwise!

@kaxil kaxil force-pushed the add-dagrun-in-context branch from 4d73a07 to 0d625b8 Compare July 23, 2025 12:21
Add `get_previous_dagrun` method to RuntimeTaskInstance to replace the removed
`DagRun.get_previous_dagrun()` method from Airflow 2.x. This enables users to
access previous DAG run information from task context for conditional logic
like notifications on state changes.

Changes:
- Add `GetPreviousDagRun` message and `PreviousDagRunResult` response in comms
- Add `get_previous_dagrun` instance method to `RuntimeTaskInstance`
- Add `/dag-runs/{dag_id}/previous` API endpoint with optional state filtering
- Add `get_previous` method to DagRunOperations API client
- Update supervisor to handle `GetPreviousDagRun` messages

Usage:
```python
def my_task(context):
    ti = context["ti"]
    previous_run = ti.get_previous_dagrun(state="success")
    if previous_run:
        # Access previous run metadata
        print(f"Previous run: {previous_run.dag_run.run_id}")
```

Fixes apache#53628
@kaxil kaxil force-pushed the add-dagrun-in-context branch from f220553 to 3d1789a Compare July 23, 2025 16:26
@kaxil kaxil added this to the Airflow 3.0.4 milestone Jul 23, 2025
@kaxil kaxil merged commit 35d23c3 into apache:main Jul 23, 2025
96 of 100 checks passed
@kaxil kaxil deleted the add-dagrun-in-context branch July 23, 2025 17:14
@ashb ashb modified the milestones: Airflow 3.0.4, Airflow 3.0.5 Aug 8, 2025
amoghrajesh added a commit to astronomer/airflow that referenced this pull request Aug 11, 2025
amoghrajesh added a commit to astronomer/airflow that referenced this pull request Aug 11, 2025
…e#53655)

Co-authored-by: Kaxil Naik <kaxilnaik@gmail.com>

(cherry picked from commit 35d23c3)
kaxil pushed a commit that referenced this pull request Aug 11, 2025
Co-authored-by: Kaxil Naik <kaxilnaik@gmail.com>

(cherry picked from commit 35d23c3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:API Airflow's REST/HTTP API area:task-sdk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DagRun.get_previous_dagrun() was removed

3 participants