<@ULVA73B9P> i have 3 prefect flows, A,B,C. B is a...
# ask-marvin
m
@Marvin i have 3 prefect flows, A,B,C. B is a subflow of A C is a subflow of B how to get run id of A while runiing flow C , which is grand sun of flow A
m
thought for 125.2 seconds
In Prefect 3.x, you can read the root (top-level) flow’s run ID from any subflow using the runtime context. - In flow C,
prefect.runtime.flow_run.root_flow_run_id
gives you A’s run ID -
prefect.runtime.flow_run.parent_flow_run_id
gives you B’s run ID -
prefect.runtime.flow_run.id
gives you C’s run ID Note: these are attributes, not function calls (no parentheses). Example:
Copy code
from prefect import flow
from prefect.runtime import flow_run

@flow
def flow_c():
    print(f"C run id: {flow_run.id}")
    print(f"B (parent) run id: {flow_run.parent_flow_run_id}")
    print(f"A (root) run id: {flow_run.root_flow_run_id}")

@flow
def flow_b():
    return flow_c()

@flow
def flow_a():
    return flow_b()

if __name__ == "__main__":
    flow_a()
Alternative (low-level) via context if you prefer UUID objects:
Copy code
from prefect.context import FlowRunContext

ctx = FlowRunContext.get()
a_run_id = str(ctx.flow_run.root_flow_run_id)  # UUID -> string
If you want links to docs, see: - Runtime flow run attributes: prefect.runtime.flow_run