<@ULVA73B9P> How can I update the flow run name du...
# ask-marvin
s
@Marvin How can I update the flow run name during the run?
m
thought for 45.3 seconds
Based on the research and module exploration, here's how you can update a flow run name during execution in Prefect 3.x:
Copy code
python
from prefect import flow, get_client

@flow
async def my_flow():
    # Get the current client
    client = get_client()
    
    # Get the current flow run ID from the context
    from prefect.context import get_flow_run_context
    context = get_flow_run_context()
    flow_run_id = context.flow_run.id
    
    # Update the flow run name
    await client.update_flow_run(
        flow_run_id=flow_run_id,
        name="New Flow Run Name"
    )
If you're using synchronous flows, you can use the synchronous version:
Copy code
python
from prefect import flow, get_client

@flow
def my_flow():
    client = get_client()
    context = get_flow_run_context()
    flow_run_id = context.flow_run.id
    
    client.update_flow_run(
        flow_run_id=flow_run_id,
        name="New Flow Run Name"
    )
A few important notes: 1. The name update will be reflected in the UI and any subsequent queries for the flow run 2. You can make the new name dynamic based on any runtime information or parameters 3. The change is permanent and will be stored in Prefect's database You can also set a dynamic name when creating the flow using the
flow_run_name
parameter, which might be more appropriate depending on your use case:
Copy code
python
@flow(flow_run_name=lambda parameters: f"my-flow-{parameters['value']}")
def my_flow(value: str):
    pass
This approach sets the name at the start of the flow run based on input parameters, which might be preferable to updating it during execution.