<@ULVA73B9P> How do I retrieve the parent flow run...
# ask-marvin
n
@Marvin How do I retrieve the parent flow run id in a child task?
m
To retrieve the parent flow run ID in a child task using Prefect 3.x, you can access the
flow_run_id
from the task's context. Here’s a concise way to do it: 1. Import the necessary context functions from Prefect. 2. Use the context to access the
flow_run_id
. Here's a simple example:
Copy code
python
from prefect import task
from prefect.context import FlowRunContext

@task
def get_parent_flow_run_id():
    flow_run_context = FlowRunContext.get()
    if flow_run_context:
        parent_flow_run_id = flow_run_context.flow_run.id
        print(f"Parent Flow Run ID: {parent_flow_run_id}")
    else:
        print("No flow run context available")
In this example,
FlowRunContext.get()
is used to access the flow run context, and from there, you can retrieve the
flow_run.id
. For more details, you can explore the documentation: - Prefect 3.x Task Run Limits - Prefect 3.x Write Flows If you have any further questions or need more specific guidance, feel free to ask.