Chris Jakuc

07/14/2023, 2:17 PM
Hi everyone! Does anyone know if there's a way to access a deployment name in an on flow failure hook? I understand that
flow, flow_run, state
are the 3 parameters passed to the hook but it's not clear to me how to access the deployment name from any of these. Would be greatly appreciated as we have some flows deployed many times with different parameters and would be very useful for understanding what is failing at a glance in our Slack alerts. Thanks!

Jake Kaplan

07/14/2023, 2:35 PM
Hey you should be able to do something like this:
Copy code
from prefect import flow
from prefect.runtime import deployment

def on_failure_hook(flow, flow_run, state):

def my_flow():
    raise ValueError("I'm a bad flow!")

if __name__ == '__main__':
when executed from a deployment you'll get something like:
Copy code
Traceback (most recent call last):
  File "/Users/jakekaplan/opt/anaconda3/envs/demo-flows/lib/python3.10/site-packages/prefect/", line 833, in orchestrate_flow_run
    result = await flow_call.aresult()
  File "/Users/jakekaplan/opt/anaconda3/envs/demo-flows/lib/python3.10/site-packages/prefect/_internal/concurrency/", line 292, in aresult
    return await asyncio.wrap_future(self.future)
  File "/Users/jakekaplan/opt/anaconda3/envs/demo-flows/lib/python3.10/site-packages/prefect/_internal/concurrency/", line 316, in _run_sync
    result = self.fn(*self.args, **self.kwargs)
  File "/Users/jakekaplan/PycharmProjects/demo-flows/", line 9, in my_flow
    raise ValueError("I'm a bad flow!")
ValueError: I'm a bad flow!
10:34:44.517 | INFO    | Flow run 'meek-tarsier' - Running hook 'on_failure_hook' in response to entering state 'Failed'
10:34:44.873 | INFO    | Flow run 'meek-tarsier' - Hook 'on_failure_hook' finished running successfully
10:34:44.875 | ERROR   | Flow run 'meek-tarsier' - Finished in state Failed("Flow run encountered an exception. ValueError: I'm a bad flow!")
THIS IS FAILED IN DEPLOYMENT: on-failure-deployment
10:34:45.614 | INFO    | prefect.flow_runs.worker - Process 93245 exited cleanly.

Chris Jakuc

07/14/2023, 2:40 PM
This is great, thank you!! I had been messing around with
locally but didn't want to go through the steps to test it in a deployed flow.
🙌 1