https://prefect.io logo
Title
j

jack

04/05/2023, 6:36 PM
Is it possible to get
flow_run.name
from inside a task? From inside a flow this works:
from prefect.context import get_run_context
get_run_context().flow_run.name
but inside a task, it throws
AttributeError: 'TaskRunContext' object has no attribute 'flow_run'
1
a

alex

04/05/2023, 8:21 PM
def get_flow_run_id() -> str:
    run_context = get_run_context()

    if isinstance(run_context, TaskRunContext):
        flow_run_id = run_context.task_run.dict().get("flow_run_id")
    else:
        flow_run_id = run_context.flow_run.dict().get("id")
    return str(flow_run_id)
j

jack

04/05/2023, 10:54 PM
Is it posible to get the
flow_run_name
from the
flow_run_id
?
a

alex

04/06/2023, 2:25 PM
It's not part of the context but I suppose you could pull it by calling the rest api
j

jack

04/06/2023, 4:41 PM
Got an answer from prefect that this works if using prefect==2.9.0 or later:
import prefect.runtime

prefect.runtime.flow_run.name
👍 2