Marco Ruta
03/17/2025, 10:26 AMMarvin
03/17/2025, 10:26 AM{{ flow_run.id }}
is not being resolved correctly, leading to a KeyError
.
Here's a concise explanation of the problem and potential solutions:
1. Placeholder Usage: Prefect 3.x uses placeholders for dynamic values, but there might be a mismatch in how placeholders are being resolved in your webhook configuration. The error suggests that flow_run.id
is not being recognized as a valid placeholder.
2. Webhook Configuration: Ensure that the placeholder syntax is supported in the context you are using it. You might need to verify if the webhook block supports dynamic placeholders in query parameters as expected.
3. Common Gotchas: Prefect 3.x has several changes and gotchas compared to previous versions. For example, the use of placeholders and how data is passed around might differ. Ensure that your configuration aligns with the latest practices in Prefect 3.x.
To resolve the issue, you might want to:
- Double-check the configuration of your webhook block and ensure that placeholders are used correctly.
- Consider using Prefect's automation or event system to trigger webhooks, which might offer more robust handling of dynamic values.
- Refer to the Prefect 3.x documentation on webhook triggers for more details on how to properly configure and use webhooks.
If the issue persists, you may need to explore alternative ways to pass dynamic data, such as using Prefect's automation features to handle such scenarios.Marco Ruta
03/17/2025, 10:28 AMMarvin
03/17/2025, 10:28 AM@flow
decorator.
2. Define a Webhook: You need to create a webhook that will be triggered upon the completion of the flow. This can be done through the Prefect Cloud UI or CLI. The webhook will have a unique URL where it will receive POST requests.
3. Use on_completion
Hook: Utilize the on_completion
hook in your flow to trigger the webhook. This hook allows you to specify a function that runs when the flow completes.
4. Send Notification: In the on_completion
function, send a POST request to your custom webhook URL.
Here is a simple example:
python
from prefect import flow, task, get_run_logger
import requests
@task
def my_task():
logger = get_run_logger()
<http://logger.info|logger.info>("Executing task...")
return "Task completed!"
@flow
def my_flow():
result = my_task()
return result
# Define a function to be called on flow completion
def notify_on_completion(flow_run):
webhook_url = "<https://myurl/notify>"
payload = {"run_id": flow_run.id}
response = <http://requests.post|requests.post>(webhook_url, json=payload)
if response.status_code == 200:
print("Notification sent successfully.")
else:
print("Failed to send notification.")
# Attach the on_completion hook
my_flow.on_completion(notify_on_completion)
# Execute the flow
my_flow()
### Key Points:
- Flow Definition: Use the @flow
decorator to define your flow.
- Webhook URL: Replace <https://myurl/notify>
with your actual webhook URL.
- Payload: Customize the payload as needed. Here, it's a simple dictionary with the run_id
.
- Error Handling: Ensure to handle errors in the POST request appropriately.
For more details, refer to the Prefect documentation on webhook triggers.Marco Ruta
03/17/2025, 10:29 AMMarvin
03/17/2025, 10:29 AMflow_run.id
, use the appropriate templating syntax supported by Prefect.
7. Save the Automation:
- Review your settings and save the automation. It will now automatically send notifications to your webhook whenever the specified trigger conditions are met.
For more detailed guidance, you can refer to the Prefect documentation on setting up automations which provides step-by-step instructions on using the Prefect UI for such configurations.