<@ULVA73B9P>, how can I get the a flow run’s id fr...
# ask-marvin
p
@Marvin, how can I get the a flow run’s id from with the definition of a flow?
m
To retrieve a flow run's ID from within the definition of a flow in Prefect 3.x, you can use the
FlowRunContext
to access the current flow run context. Here's a concise way to achieve this using the imperative programming style you prefer:
Copy code
python
from prefect import flow
from prefect.context import FlowRunContext

@flow
def my_flow():
    flow_run_context = FlowRunContext.get()
    if flow_run_context:
        flow_run_id = flow_run_context.flow_run.id
        print(f"Flow Run ID: {flow_run_id}")
    else:
        print("No flow run context available.")

my_flow()
In this example,
FlowRunContext.get()
is used to access the current flow run context, and from there, you can obtain the flow run's ID with
flow_run_context.flow_run.id
. For additional details, you can refer to the Prefect documentation here and here. If you have any further questions or need more specific guidance, feel free to ask.