Azure Content Safety provider
Overview
Design goals: This provider bridges Azure Content Safety into Mastra so teams can rely on tenant-hosted moderation resources and Azure-managed authentication instead of external services. It centralizes auth selection, blocklist, threshold, and retry behavior so moderation features that Mastra doesn't implement natively can be consumed uniformly.
Docs guideline: This page assumes a tenant-first deployment model: write as if the Content Safety resource is deployed in the reader's Azure tenant and emphasize Managed Identity-first usage. Include short env-var examples when helpful and cross-link back to the Azure introduction's "Documentation guideline" for global guidance.
Mastra routes every moderation call through src/mastra/providers/azure-content-safety.ts. The module wraps the REST-based Azure AI Content Safety client, enforces configurable retry logic, and exposes helper types such as TextModerationInput and ImageModerationInput so callers can provide text, image URLs/base64 blobs, optional category filters, and signal-aware timeouts.
Authentication and retries
- Authentication precedence is explicit:
AZURE_CONTENT_SAFETY_USE_MANAGED_IDENTITY=trueforces managed identity; otherwise the provider uses API key whenAZURE_CONTENT_SAFETY_KEY/CONTENT_SAFETY_KEYis present; if no key exists it falls back to managed identity. The provider caches both the credential and the client (ensureClient) to avoid recreating theContentSafetyClientfor every request. - The Azure endpoint is mandatory (
AZURE_CONTENT_SAFETY_ENDPOINTorCONTENT_SAFETY_ENDPOINT). A missing value throws before any HTTP traffic, keeping tooling from spinning up without a target. - Calls go through
executeWithRetry, which implements exponential backoff for HTTP 408/429/5xx responses. The helper also respects theretry-afterheader, abort signals, and theAZURE_CONTENT_SAFETY_DEBUG_TIMINGSflag for extra logging.
Blocklists, categories, and thresholds
- Blocklists come from environment variables (
AZURE_CONTENT_SAFETY_BLOCKLIST_NAME). The helpergetConfiguredContentSafetyBlocklistNames()parses comma-separated names, whileresolveBlocklistsandresolveSingleBlocklistNameenforce whether a request can omit the blocklist or must pick one when several are configured. - Every moderation result returns a list of
ContentSafetyCategoryentries, each normalized vianormalizeCategoriesso the highest severity comes first. ThemaxSeverity/flaggedproperties mirrorrawResponsefields so callers can easily gate actions. - Blocklist hits are surfaced with
ContentSafetyBlocklistHitmetadata, allowing tooling to log the blocklist name, entry ID, and textual match.
Text + image helpers
- Text moderation payloads accept
categories, customblocklistNames, and ahaltOnBlocklistHitflag when one match should stop all scoring. Image moderation also accepts the same categories and leverages the sameexecuteWithRetry/ensureClientplumbing. - Image inputs are normalized via
normalizeImageInput, which handles URLs, raw base64 strings, andArrayBuffer/Bufferpayloads so callers only need to supply the raw bytes or blob reference. - Both helpers surface the
authMode(managed-identityvsapi-key), the raw Azure REST response, and the highest severity category per call.
Usage notes
- Tooling that wires into Azure Content Safety typically obtains the blocklist name from the caller or the configured default (
getDefaultContentSafetyBlocklistName). - The provider logs timing diagnostics only when either
AZURE_CONTENT_SAFETY_DEBUG_TIMINGSorMASTRA_DEBUG_TIMINGSis enabled. - For long-running moderation requests, provide an
AbortSignaltoTextModerationInput/ImageModerationInputso the helper respects cancellation while insidewaitorexecuteWithRetry.