Develop a Custom Alerter

Learning how to develop a custom alerter.

Before diving into the specifics of this component type, it is beneficial to familiarize yourself with our general guide to writing custom component flavors in ZenML. This guide provides an essential understanding of ZenML's component flavor concepts.

Base Abstraction

The base abstraction for alerters is very basic, as it only defines two abstract methods that subclasses should implement:

post() takes a string, posts it to the desired chat service, and returns True if the operation succeeded, else False.

ask() does the same as post(), but after sending the message, it waits until someone approves or rejects the operation from within the chat service (e.g., by sending "approve" / "reject" to the bot as a response). ask() then only returns True if the operation succeeded and was approved, else False.

Then base abstraction looks something like this:

class BaseAlerter(StackComponent, ABC):

"""Base class for all ZenML alerters."""

def post(

self, message: str, params: Optional[BaseAlerterStepParameters]

) -> bool:

"""Post a message to a chat service."""

return True

def ask(

self, question: str, params: Optional[BaseAlerterStepParameters]

) -> bool:

"""Post a message to a chat service and wait for approval."""

return True

This is a slimmed-down version of the base implementation. To see the full docstrings and imports, please check the source code on GitHub.

Building your own custom alerter

Creating your own custom alerter can be done in three steps:

Create a class that inherits from the BaseAlerter and implement the post() and ask() methods.Copyfrom typing import Optional

from zenml.alerter import BaseAlerter, BaseAlerterStepParameters

class MyAlerter(BaseAlerter):
    """My alerter class."""