<@ULVA73B9P> in Prefect 2.20 I'm setting up JSON P...
# ask-marvin
m
@Marvin in Prefect 2.20 I'm setting up JSON Payload to give information about the flow_run id and status to my custom webhook, how can I use these placeholder in the json?
m
In Prefect 2.20, you can use Jinja templating to include placeholders in your JSON payload for a webhook. Here's how you can set up placeholders for the flow run ID and status: 1. Flow Run ID: Use
{{ flow_run.id }}
to include the flow run ID. 2. Flow Run Status: Use
{{ flow_run.state.name }}
to include the flow run's current state or status. For example, your JSON payload could look something like this:
Copy code
json
{
  "flow_run_id": "{{ flow_run.id }}",
  "status": "{{ flow_run.state.name }}"
}
Make sure the output of your template, when rendered, is valid JSON. You can find more details and examples in the Prefect 2.x Automations documentation. If you need further guidance on setting up webhooks, you might find the Prefect Webhooks Guide useful.
m
152305.468 | ERROR | prefect.server.services.flowrunnotifications - Error sending notification for policy 5f83d529-f205-405c-bd49-d9ee30894f01 on flow run 70983dfc-8675-40e5-a602-571b98d488da Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/prefect/server/services/flow_run_notifications.py", line 92, in send_flow_run_notification block = Block._from_block_document( File "/usr/local/lib/python3.10/site-packages/prefect/blocks/core.py", line 643, in _from_block_document block = block_cls.parse_obj(block_document.data) File "/usr/local/lib/python3.10/site-packages/pydantic/v1/main.py", line 526, in parse_obj return cls(**obj) File "/usr/local/lib/python3.10/site-packages/prefect/blocks/core.py", line 270, in init self.block_initialization() File "/usr/local/lib/python3.10/site-packages/prefect/blocks/notifications.py", line 745, in block_initialization raise KeyError(f"{name}/{placeholder}") KeyError: "json_data/Placeholder(full_match='{{ flow_run.id }}', name='flow_run.id', type=<PlaceholderType.STANDARD: 'standard'>)"
@Marvin 152305.468 | ERROR | prefect.server.services.flowrunnotifications - Error sending notification for policy 5f83d529-f205-405c-bd49-d9ee30894f01 on flow run 70983dfc-8675-40e5-a602-571b98d488da Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/prefect/server/services/flow_run_notifications.py", line 92, in send_flow_run_notification block = Block._from_block_document( File "/usr/local/lib/python3.10/site-packages/prefect/blocks/core.py", line 643, in _from_block_document block = block_cls.parse_obj(block_document.data) File "/usr/local/lib/python3.10/site-packages/pydantic/v1/main.py", line 526, in parse_obj return cls(**obj) File "/usr/local/lib/python3.10/site-packages/prefect/blocks/core.py", line 270, in init self.block_initialization() File "/usr/local/lib/python3.10/site-packages/prefect/blocks/notifications.py", line 745, in block_initialization raise KeyError(f"{name}/{placeholder}") KeyError: "json_data/Placeholder(full_match='{{ flow_run.id }}', name='flow_run.id', type=<PlaceholderType.STANDARD: 'standard'>)"
m
The error you're encountering indicates a problem with resolving the placeholder
{{ flow_run.id }}
in your JSON payload. This usually happens when the placeholder isn't being correctly substituted with the expected runtime value. Here are a few things to check: 1. Placeholder Syntax: Ensure that the placeholders are correctly formatted using Jinja syntax. Double-check that you're using
{{ flow_run.id }}
exactly as shown. 2. Context Availability: Verify that the context in which the placeholders are being rendered has access to the
flow_run
object. This is necessary for the placeholders to be substituted with actual values. 3. Notification Configuration: Review your notification policy configuration to ensure that it is set up to pass the correct context to the webhook. Since there are no related GitHub issues, this might be a specific configuration issue or a bug not yet reported. If the problem persists, consider reaching out to the Prefect community or support for further assistance.
m
@Marvin maybe templates are not available for custom notification?
m
It appears that while Prefect does offer templating options for notifications, these might be more robustly supported in Prefect Cloud rather than in self-hosted environments. For custom notifications, especially when using webhooks, you might face limitations if you're not using Prefect Cloud's automations interface. Here's what you should consider: 1. Notification Configuration: In self-hosted Prefect, notification configurations might not fully support templating for placeholders like
{{ flow_run.id }}
. Prefect Cloud, on the other hand, provides more comprehensive support for automations and templating in notifications. 2. Webhook Templates: Prefect's webhook system allows you to define templates for how notifications are processed. However, if you're seeing errors related to placeholders, it might be due to the limited support in your current setup. If you're relying on a self-hosted Prefect server and experiencing issues with placeholders in custom notifications, you may need to consider alternative approaches, such as processing the notification payload externally before sending it to the webhook. For more detailed guidance, you can refer to the Prefect Webhooks Guide and the Notification Configuration sections.