Skip to content

Restore get_previous_dagrun functionality for task context - #53655

Merged
kaxil merged 1 commit into
apache:mainfrom
astronomer:add-dagrun-in-context
Jul 23, 2025
Merged

Restore get_previous_dagrun functionality for task context#53655
kaxil merged 1 commit into
apache:mainfrom
astronomer:add-dagrun-in-context

Conversation

@kaxil

@kaxilkaxil commented Jul 22, 2025

Copy link
Copy Markdown
Member

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:

defmy_task(context):
ti=context["ti"]
previous_run=ti.get_previous_dagrun()
ifprevious_run:
# Access previous run metadataprint(f"Previous run: {previous_run.dag_run.run_id}")

or

@dagdefa_dag():
@task(task_id="print_the_context")defprint_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.

@boring-cyborgboring-cyborgBot added area:API Airflow's REST/HTTP API area:task-sdk labels Jul 22, 2025
@kaxilkaxil mentioned this pull request Jul 22, 2025
2 tasks
@kaxil
kaxilforce-pushed the add-dagrun-in-context branch 2 times, most recently from d429189 to 4d73a07CompareJuly 23, 2025 10:22
@kaxil
kaxil requested a review from mobuchowski as a code ownerJuly 23, 2025 10:22

@amoghrajeshamoghrajesh left a comment

Copy link
Copy Markdown
Contributor

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!

Comment threadairflow-core/src/airflow/api_fastapi/execution_api/versions/v2025_08_10.py Outdated
@kaxil
kaxilforce-pushed the add-dagrun-in-context branch from 4d73a07 to 0d625b8CompareJuly 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}")
```
Fixesapache#53628
@kaxil
kaxilforce-pushed the add-dagrun-in-context branch from f220553 to 3d1789aCompareJuly 23, 2025 16:26
@kaxilkaxil added this to the Airflow 3.0.4 milestone Jul 23, 2025
@kaxil
kaxil merged commit 35d23c3 into apache:mainJul 23, 2025
96 of 100 checks passed
@kaxil
kaxil deleted the add-dagrun-in-context branch July 23, 2025 17:14
@ashbashb modified the milestones: Airflow 3.0.4, Airflow 3.0.5Aug 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 freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:APIAirflow's REST/HTTP APIarea:task-sdk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DagRun.get_previous_dagrun() was removed

3 participants

@kaxil@amoghrajesh@ashb