Bo Ritter
07/23/2025, 1:48 PMMarvin
07/23/2025, 1:48 PMMarvin
07/23/2025, 1:50 PMpython
from prefect.blocks.notifications import CustomWebhookNotificationBlock
webhook_block = CustomWebhookNotificationBlock(
name="my-custom-webhook",
url="YOUR_WEBHOOK_URL",
method="POST", # Default is POST, but you can change if needed
headers={"Content-Type": "application/json"}
)
webhook_block.save()
2. In the Prefect UI, create a new automation with:
- A trigger condition (e.g., "When a flow run enters a Failed state")
- An action using the "Call Webhook" action type
3. When configuring the webhook in the automation, you can use the following template variables in your JSON payload:
json
{
"flow_run_data": {{flow_run}},
"flow_name": "{{flow_run.flow_name}}",
"flow_run_id": "{{flow_run.id}}",
"flow_run_status": "{{flow_run.state.type}}",
"flow_run_message": "{{flow_run.state.message}}"
}
Key points to remember:
- The {{flow_run}}
template variable contains the entire flow run object
- You can access specific fields using dot notation: {{flow_run.field_name}}
- The webhook will automatically parse these template variables before sending
- Make sure your receiving endpoint can handle the JSON payload structure you define
Common fields available in the flow_run
object include:
- `id`: The unique identifier of the flow run
- `flow_name`: Name of the flow
- `state.type`: Current state of the flow run (e.g., "COMPLETED", "FAILED")
- `state.message`: Any message associated with the state
- `start_time`: When the flow run started
- `end_time`: When the flow run ended
For example, if you want to send a notification to Slack with flow run details, your webhook payload might look like:
json
{
"text": "Flow Run Update",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Flow *{{flow_run.flow_name}}* (ID: {{flow_run.id}}) is now in state *{{flow_run.state.type}}*\nMessage: {{flow_run.state.message}}"
}
}
]
}
Remember that the receiving webhook endpoint needs to be able to handle the JSON structure you send. Make sure to test the webhook with a sample payload before setting up the automation.
Links for further reading:
- Prefect Automations Documentation
- Prefect Webhooks Guide
Would you like me to provide more specific examples for your use case or clarify any part of the setup process?Bo Ritter
07/23/2025, 1:53 PM@Marvin "reason": "The notification block was invalid: KeyError(\"json_data/Placeholder(full_match='{{flow_run}}', name='flow_run', type=<PlaceholderType.STANDARD: 'standard'>)\")",
Bo Ritter
07/23/2025, 1:53 PM"reason": "The notification block was invalid: KeyError(\"json_data/Placeholder(full_match='{{flow_run}}', name='flow_run', type=<PlaceholderType.STANDARD: 'standard'>)\")",
Marvin
07/23/2025, 1:53 PMMarvin
07/23/2025, 1:54 PM{{flow_run}}
variable is being passed in the JSON data. Here's how to fix it:
Instead of passing the flow run directly as a raw template variable, you need to access specific attributes of the flow run object. Here are two approaches:
1. Using specific fields (Recommended):
json
{
"flow_run_id": "{{flow_run.id}}",
"flow_name": "{{flow_run.flow_name}}",
"state": "{{flow_run.state.type}}",
"message": "{{flow_run.state.message}}",
"start_time": "{{flow_run.start_time}}",
"end_time": "{{flow_run.end_time}}"
}
2. Using the built-in to_json
filter:
json
{
"flow_run_data": "{{flow_run | to_json}}"
}
The error you're seeing occurs because the webhook system can't directly serialize the {{flow_run}}
placeholder into valid JSON. By either:
- Breaking it down into specific fields you want to access, or
- Using the to_json
filter
You should be able to resolve the error.
When setting up your CustomWebhookNotificationBlock, make sure to structure it like this:
python
from prefect.blocks.notifications import CustomWebhookNotificationBlock
webhook_block = CustomWebhookNotificationBlock(
name="my-custom-webhook",
url="YOUR_WEBHOOK_URL",
method="POST",
headers={"Content-Type": "application/json"},
json_data={
"flow_run_id": "{{flow_run.id}}",
"flow_name": "{{flow_run.flow_name}}",
"state": "{{flow_run.state.type}}",
"message": "{{flow_run.state.message}}"
}
)
webhook_block.save()
Would you like to share what specific data you're trying to capture from the flow run? That way, I can provide a more tailored example for your use case.Bo Ritter
07/23/2025, 2:17 PMMarvin
07/23/2025, 2:17 PMMarvin
07/23/2025, 2:18 PMto_json
filter, you'll need to access the flow run properties directly. Here's how you can structure the webhook body in your automation:
json
{
"id": "{{flow_run.id}}",
"name": "{{flow_run.name}}",
"flow_name": "{{flow_run.flow_name}}",
"state_type": "{{flow_run.state.type}}",
"state_message": "{{flow_run.state.message}}",
"start_time": "{{flow_run.start_time}}",
"end_time": "{{flow_run.end_time}}",
"parameters": "{{flow_run.parameters}}",
"tags": "{{flow_run.tags}}",
"deployment_id": "{{flow_run.deployment_id}}"
}
If you need to include specific nested data or have particular fields you want to capture, please let me know and I can help you structure the JSON appropriately.
Also, if you're trying to debug what data is available, you could try setting up a simple webhook (like webhook.site) and first send a simpler payload to see what data you have access to:
json
{
"debug_info": "Flow run {{flow_run.id}} completed with state {{flow_run.state.type}}"
}
Would you like to share what specific flow run data you're trying to capture? That way, I can help you structure the JSON payload more precisely for your needs.