Ben Epstein
03/24/2025, 4:45 PMfrom prefect.blocks.notifications import SlackWebhook
slack_webhook_block = SlackWebhook.load("BLOCK_NAME")
slack_webhook_block.notify("Hello from Prefect!")
this is working for me. I wrote a function to do that as per this thread and added the on_failure=[notify_slack]
but i'm getting the error (and mypy is showing the same)
packages/flows/notify.py", line 7, in notify_slack
slack_webhook_block.notify(
^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'coroutine' object has no attribute 'notify'
I'm curious why this is working when I just run this code directly, but within prefect flow infrastructure it's failing. I'm seeing that in pylance the variable is of type slack_webhook_block: SlackWebhook | Coroutine[Any, Any, SlackWebhook]
-- so perhaps because my flow is async, this notify_slack
function needs to be async as well?Ben Epstein
03/24/2025, 4:49 PMasync def notify_slack(flow: Flow, flow_run, state):
slack_webhook_block = await SlackWebhook.aload("my-block")
slack_webhook_block.notify("...")
and prefect says its working
Hook 'notify_slack' finished running successfully
but i didnt actually get the slack notificationBen Epstein
03/24/2025, 5:06 PMslack_webhook_block.notify
needs to be awaited. There seems to be a typing issue with prefect here, but perhaps you already know about it based on the comments
@Alexander Azzam i feel like you and I have talked about something like this in the past.Ben Epstein
03/24/2025, 5:07 PMChris White
_sync=True/False
that you can use to hardcode deterministic behavior on methods such as this.
More info here: https://github.com/PrefectHQ/prefect/issues/15008
It makes type-hinting challenging to say the least!Ben Epstein
03/24/2025, 5:14 PMfrom prefect import Flow
from prefect.blocks.notifications import SlackWebhook
from prefect.settings import PREFECT_UI_URL
from prefect.server.schemas.core import FlowRun
from prefect.server.schemas.states import State
async def notify_slack(flow: Flow, flow_run: FlowRun, state: State):
slack_webhook_block = await SlackWebhook.aload("my-slack-webhook")
await slack_webhook_block.notify( # type: ignore
(
f"Your job {flow_run.name} entered {state.name} "
f"with message:\n\n"
f"See <{PREFECT_UI_URL.value()}/flow-runs/"
f"flow-run/{flow_run.id}|the flow run in the UI>\n\n"
f"Tags: {flow_run.tags}\n\n"
f"Scheduled start: {flow_run.expected_start_time}"
)
)
Chris White
anotify
as well but until then this is the right way to proceedBen Epstein
03/24/2025, 5:38 PMChris White
Ben Epstein
03/24/2025, 8:46 PMPlatform Engineering
section. LMK if you like that and ill add itChris White
Chris White
Chris White
Ben Epstein
03/25/2025, 12:09 AM