Prefect Slack Integration
The `prefect-slack` library provides official integrations for connecting Prefect 2.x workflows with Slack. It enables sending notifications and messages to Slack channels using webhooks or bot tokens, typically via Prefect Blocks. The current stable version is `0.3.1`, and it's part of the PrefectHQ monorepo, with releases often following the Prefect core library's major versions, though `prefect-slack` `0.3.1` is specifically for Prefect `2.x`.
Common errors
-
ModuleNotFoundError: No module named 'prefect_slack'
cause The `prefect-slack` library has not been installed in your Python environment.fixRun `pip install prefect-slack` to install the library. -
pydantic_core._pydantic_core.ValidationError: 1 validation error for SlackWebhook url URL is not a valid URL [type=url_parsing, input_value='']
cause The `SlackWebhook` block was instantiated without a valid `url` or the provided URL was empty/invalid.fixEnsure the `url` parameter passed to `SlackWebhook(url=...)` is a complete and valid Slack incoming webhook URL. If using environment variables, verify the variable is set and accessible. -
TypeError: notify() missing 1 required positional argument: 'body'
cause The `notify()` method of `SlackWebhook` or `SlackMessage` was called without providing the `body` argument, which is the message content.fixAlways pass the message content as the `body` argument to the `notify()` method, e.g., `webhook_block.notify(body='Your message here')`.
Warnings
- breaking The `prefect-slack` library `0.3.1` is designed for Prefect core `2.x` (specifically versions `>=2.10.0,<3.0.0`). Using it with Prefect core `3.x` (which is currently in development/nightly releases) may lead to incompatibility issues or unexpected behavior. Always ensure your `prefect` core library version is compatible with your `prefect-slack` version.
- gotcha Slack integrations, particularly webhooks, require proper authentication. The `SlackWebhook` block needs a valid URL to post messages. Incorrectly configured URLs or missing environment variables/secrets will prevent notifications from being sent.
- gotcha When using `SlackWebhook` or `SlackMessage` blocks, ensure they are instantiated or loaded correctly within the Prefect flow context. If blocks are not saved in the Prefect backend, they must be instantiated directly in code, which might expose credentials if not handled carefully (e.g., via environment variables).
Install
-
pip install prefect-slack
Imports
- SlackWebhook
from prefect_slack.webhooks import SlackWebhook
- SlackMessage
from prefect_slack.messages import SlackMessage
Quickstart
import os
from prefect import flow
from prefect_slack.webhooks import SlackWebhook
@flow(log_prints=True)
def send_slack_notification_flow(message: str = "Hello from Prefect!"):
"""Sends a custom message to Slack via a configured webhook."""
# Retrieve Slack Webhook URL from environment variables or Prefect secrets
slack_webhook_url = os.environ.get("SLACK_WEBHOOK_URL", "")
if not slack_webhook_url:
print("Warning: SLACK_WEBHOOK_URL environment variable is not set. Skipping Slack notification.")
return
# Initialize the SlackWebhook block with the URL
# For production, consider creating and storing this block in the Prefect UI/API
# and loading it via await SlackWebhook.load("my-slack-webhook-block")
webhook_block = SlackWebhook(url=slack_webhook_url)
# Send the notification
webhook_block.notify(body=message)
print(f"Successfully sent Slack notification: {message}")
if __name__ == "__main__":
# To run this example, set the SLACK_WEBHOOK_URL environment variable:
# export SLACK_WEBHOOK_URL="https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"
send_slack_notification_flow(message="This is a test notification from my Prefect flow!")