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
Previously, in 2.9.2, I used the dag_run available in the task context and called get_previous_dagrun on it, but from what I understand in Airflow 3 the context holds a stripped down version of the dag_run, which no longer has the get_previous_dagrun method.
Instead, in Airflow 3.0.2 the call fails with: AttributeError: 'DagRun' object has no attribute 'get_previous_dagrun'
What you think should happen instead?
I think there should be a get_previous_dagrun method as before. The reason I used this in 2.9.2 was to get the state of the previous DagRun and based on the current and previous state decided whether to send a notification or not (e.g.: if a DAG fails, we might only care to get a notification on the first failure or when it recovers, not in between)
How to reproduce
For reference, the code that works in 2.9.2 looks like this: crt_dag_run = context["dag_run"] previous_dag_run = crt_dag_run.get_previous_dagrun(dag_run=crt_dag_run)
In v3, the DagRun that I get is of type airflow.sdk.api.datamodels._generated.DagRun which doesn't contain the get_previous_dagrun method.
I am not sure if this is "easy" or whether we want to do much with it. There is a number of methods that have been deliberately removed from "context" objects because they were never intended to be a "public interface" - they were used by people "accidentally" but when we finalized spit of the workers from database - direct DB access (that was needed by this method) is gone and our general recommendation is to use REST API (which was intended for such usage since the beginning of Airlfow 2 - so for about 4 years now).
So I am quite sure we are not going to "fix it" in the sense that this method is not coming back.
I think however that we might think to make some kind of easier replacements for some "common" methods that people used to use - there are a few methods (including get_previous_dag_run) that keeps on repeating and for me this is a signal that we might need to do something about it - either to have such methods in Task SDK directly or making it easier to use REST API from within the tasks.
I will mark it as "affects 3.0" and likely we should discuss it @ashb@kaxil@amoghrajesh -> tagging those who worked most on the task-sdk.
Ah alright , got it ! I can migrate the logic to use the REST API instead of relying on get_previous_dagrun(). What do u say? If there are any specifics lmk :). Ill try my best to incorporate them
Ah alright , got it ! I can migrate the logic to use the REST API instead of relying on get_previous_dagrun(). What do u say? If there are any specifics lmk :). Ill try my best to incorporate them
Well. it requires the user to change it - nothing in Airflow code can be done to change it. User neds to change their code in the Dag currently to use REST API.
The reason I used this in 2.9.2 was to get the state of the previous DagRun and based on the current and previous state decided whether to send a notification or not (e.g.: if a DAG fails, we might only care to get a notification on the first failure or when it recovers, not in between)
@cristian-fatu There is a context["ti"].get_dagrun_state() state method already available -- which you could use in AF 3.0
It was certainly an "unintentional or not thought out" breaking of the python interface from 2 to 3. We should see if we can fix it nicely, but make not guarantees that we will be able to nicely.
Activity
nrnavaneet commentedon Jul 22, 2025
Would like to work on this :)
potiuk commentedon Jul 22, 2025
I am not sure if this is "easy" or whether we want to do much with it. There is a number of methods that have been deliberately removed from "context" objects because they were never intended to be a "public interface" - they were used by people "accidentally" but when we finalized spit of the workers from database - direct DB access (that was needed by this method) is gone and our general recommendation is to use REST API (which was intended for such usage since the beginning of Airlfow 2 - so for about 4 years now).
So I am quite sure we are not going to "fix it" in the sense that this method is not coming back.
I think however that we might think to make some kind of easier replacements for some "common" methods that people used to use - there are a few methods (including get_previous_dag_run) that keeps on repeating and for me this is a signal that we might need to do something about it - either to have such methods in Task SDK directly or making it easier to use REST API from within the tasks.
I will mark it as "affects 3.0" and likely we should discuss it @ashb @kaxil @amoghrajesh -> tagging those who worked most on the task-sdk.
potiuk commentedon Jul 22, 2025
Ah .. I see @ashb already marked it as "3.0.4" - likely with similar ideas
nrnavaneet commentedon Jul 22, 2025
Ah alright , got it ! I can migrate the logic to use the REST API instead of relying on get_previous_dagrun(). What do u say? If there are any specifics lmk :). Ill try my best to incorporate them
potiuk commentedon Jul 22, 2025
Well. it requires the user to change it - nothing in Airflow code can be done to change it. User neds to change their code in the Dag currently to use REST API.
nrnavaneet commentedon Jul 22, 2025
Oh learnt smntg new :)
kaxil commentedon Jul 22, 2025
@cristian-fatu There is a
context["ti"].get_dagrun_state()state method already available -- which you could use in AF 3.0airflow/task-sdk/src/airflow/sdk/execution_time/task_runner.py
Lines 519 to 526 in 3dc4cd2
and
airflow/task-sdk/src/airflow/sdk/execution_time/task_runner.py
Lines 496 to 516 in 3dc4cd2
cristian-fatu commentedon Jul 22, 2025
@kaxil that would still leave the issue of getting the id of the previous run
kaxil commentedon Jul 22, 2025
That is true, it is currently generic. We will add something more convenient.
ashb commentedon Jul 22, 2025
It was certainly an "unintentional or not thought out" breaking of the python interface from 2 to 3. We should see if we can fix it nicely, but make not guarantees that we will be able to nicely.
kaxil commentedon Jul 22, 2025
I have a PR (with a couple of TODOs): #53655 . The access pattern is a little different, but it give you the data you need
kaxil commentedon Jul 23, 2025
@cristian-fatu Could you check with the solution in #53655
Restore ``get_previous_dagrun`` functionality for task context (apach…