<@ULVA73B9P> how can I get flow run name via pytho...
# ask-marvin
p
@Marvin how can I get flow run name via python SDK when I only have the
flow_run_id
? I am looking to get the real flow name defined in the code like for example
train-models
in the following snippet:
Copy code
@flow(name="train-models", description="Train models")
async def train_models(workflow_input):
    return workflow_input
m
To get the flow run name when you only have the
flow_run_id
, you would typically need to use the Prefect Python SDK to query for the flow run information from the Prefect backend. Unfortunately, the document search did not return relevant information on how to do this with the Prefect 2.x SDK directly. However, in Prefect 2.x (also referred to as Prefect Orion), you can use the
get_flow_run
function from the Prefect API to retrieve the flow run details by passing the
flow_run_id
. The response will include the flow run name as defined in the code. Here's a general approach on how to do it:
Copy code
python
from prefect.client import get_client

async def get_flow_run_name(flow_run_id):
    async with get_client() as client:
        flow_run = await client.read_flow_run(flow_run_id)
        return flow_run.name

flow_run_id = "your-flow-run-id"
flow_run_name = await get_flow_run_name(flow_run_id)
print("Flow run name:", flow_run_name)
You need to replace
"your-flow-run-id"
with your actual flow run ID. The function
get_flow_run_name
uses
get_client
to create a client that can asynchronously communicate with the Prefect backend to read the flow run details. The
flow_run.name
attribute will give you the name of the flow as defined in your flow code. If you have difficulty implementing this or need further assistance, please let me know, and I can provide more detailed instructions or troubleshoot any issues you might encounter.
p
@Marvin this returns the random name created by prefect on runtime, e.g., 'beneficial-saluki'
m
The search results from the documents did not directly address the question of how to obtain the original flow name specified in the code using a
flow_run_id
in Prefect 2.x SDK. However, based on my understanding of Prefect 2.x, the original flow name specified in the code (through the
@flow(name="train-models")
decorator or similar) can be retrieved once you have the details of the flow run. Since the flow run name does not directly contain this information (as it contains the name generated at runtime like 'beneficial-saluki'), you'll need to fetch related flow details. Here is an approach using the Prefect API:
Copy code
python
from prefect import get_client

async def get_original_flow_name(flow_run_id):
    async with get_client() as client:
        flow_run = await client.read_flow_run(flow_run_id)
        flow = await client.read_flow(flow_run.flow_id)
        return flow.name

flow_run_id = "your-flow-run-id"
original_flow_name = await get_original_flow_name(flow_run_id)
print("Original flow name defined in code:", original_flow_name)
You would replace "your-flow-run-id" with your actual
flow_run_id
. The function
get_original_flow_name
queries the flow run to get the
flow_id
, and subsequently, it queries for the flow associated with that ID. The
flow.name
from the response will provide you with the name as defined in your flow code. Please try this approach, and if you come across any issues, let me know so I can provide further assistance.
1