Is it possible to get `flow_run.name` from inside ...
# prefect-cloud
j
Is it possible to get
flow_run.name
from inside a task? From inside a flow this works:
Copy code
from prefect.context import get_run_context
get_run_context().flow_run.name
but inside a task, it throws
Copy code
AttributeError: 'TaskRunContext' object has no attribute 'flow_run'
1
a
Copy code
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
Is it posible to get the
flow_run_name
from the
flow_run_id
?
a
It's not part of the context but I suppose you could pull it by calling the rest api
j
Got an answer from prefect that this works if using prefect==2.9.0 or later:
Copy code
import prefect.runtime

prefect.runtime.flow_run.name
👍 2