<@ULVA73B9P> What is the easiest way to get the pa...
# ask-marvin
s
@Marvin What is the easiest way to get the parent flow run ID from within a running subflow?
m
thought for 88.1 seconds
Easiest: use the runtime helper in Prefect 3.x.
Copy code
from prefect import flow
from prefect.runtime import flow_run

@flow
def child_flow():
    parent_id = flow_run.parent_flow_run_id  # None if this is a root flow
    print(f"Parent flow run ID: {parent_id}")
    return parent_id
Notes: -
flow_run.parent_flow_run_id
returns the calling flow’s run ID when you’re in a subflow, otherwise
None
. - Related helpers you may find useful:
flow_run.id
(current flow run),
flow_run.root_flow_run_id
(top-level flow in the chain). If you need to fetch details about the parent run:
Copy code
from prefect import flow
from prefect.runtime import flow_run
from prefect.client.orchestration import get_client

@flow
async def child_flow():
    parent_id = flow_run.parent_flow_run_id
    if parent_id:
        async with get_client() as client:
            parent = await client.read_flow_run(parent_id)
            print(f"Parent flow name: {parent.name}")
    return parent_id