How can I set a custom flow run name instead of using the randomly generated adjective-noun combination?

The flow run name can not be set in advance, but it can be changed using the RenameFlowRun task after the flow run has been created.

You can use this task inside the Flow block or through a flow-level state handler. When calling the task from a state handler, make sure to call the task’s .run() method.

Here is a flow-level state handler example:

from prefect import Flow, task
from prefect.tasks.prefect import RenameFlowRun

def rename_handler(flow, new_state, old_state):
    if new_state.is_running():

def first_task():
    return 1

with Flow("test-flow", state_handlers=[rename_handler]) as flow:

There is incorrect order of states in your example.

Handler should have following signature:
state_handler(flow: Flow, old_state: State, new_state: State) -> Optional[State]

Thanks, @Nikita_Samoylov! It actually doesn’t matter because regardless of whether the obj is a Flow or Task object, it will be passed to the state handler, depending on whether you attach the state handler to a task or a flow. But I agree with you that renaming it to flow makes it more explicitly, so I’ll change it.

Thanks for posting this! :clap:

Hi, I got an error: Import “prefect.tasks.prefect” could not be resolved
I’m using Prefect 2.4.5

this is Prefect 1 syntax, don’t use it in Prefect 2

Docs for Prefect 2 are available here: