Context tile for pidge notification library v3 - async API with NotificationClient, Message, and dispatch pattern
99
Quality
99%
Does it follow best practices?
Impact
100%
2.85xAverage score across 3 eval scenarios
Passed
No known issues
config = NotificationConfig(api_key=os.environ.get("PIDGE_API_KEY", ""))
async with NotificationClient(config=config) as client:
msg = Message(to=email, subject=subject, body=body)
result = await client.dispatch(msg)The result object returned by client.dispatch() confirms delivery. Check its properties before trusting the send succeeded:
result = await client.dispatch(msg)
if result.success:
logger.info("Notification dispatched: id=%s", result.message_id)
notification_sent = True
else:
logger.warning("Dispatch reported failure: %s", result.status)
notification_sent = FalseAlways log result.message_id when available — it's the canonical reference for delivery tracing.
Catch DispatchError explicitly — do not let it propagate as a 500:
from pidge import DispatchError
try:
result = await client.dispatch(msg)
notification_sent = True
except DispatchError as e:
# Log the error, return a graceful response
notification_sent = FalseFor transient failures (e.g. network timeouts), a simple retry with backoff is appropriate. Avoid retrying on permanent errors such as invalid addresses:
import asyncio
from pidge import DispatchError
for attempt in range(3):
try:
result = await client.dispatch(msg)
notification_sent = True
break
except DispatchError as e:
if attempt < 2:
await asyncio.sleep(2 ** attempt) # exponential backoff
else:
logger.error("Dispatch failed after retries: %s", e)
notification_sent = FalseNotificationClient lifecycle short — open, dispatch, close. Don't hold connections.DispatchError explicitly — don't let it bubble as a 500.result.success and log result.message_id to confirm delivery before marking a send as successful.notification_sent: True/False in your endpoint response so callers know.Notifier, .send()) is removed. Don't use it.