Skip to content
Docs

Azure Content Safety Guard Provider

The Azure Content Safety provider implements the guard.Guard interface using the Azure AI Content Safety API. It evaluates text across four harm categories (Hate, SelfHarm, Sexual, Violence) with configurable severity thresholds.

Choose Azure Content Safety when your organization already uses the Azure ecosystem or needs Microsoft’s enterprise compliance certifications. It provides configurable severity thresholds per harm category, making it straightforward to tune strictness for different use cases. For prompt injection detection specifically, consider Lakera Guard. For a self-hosted open-source alternative, consider LLM Guard.

Terminal window
go get github.com/lookatitude/beluga-ai/guard/providers/azuresafety
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/lookatitude/beluga-ai/guard"
"github.com/lookatitude/beluga-ai/guard/providers/azuresafety"
)
func main() {
g, err := azuresafety.New(
azuresafety.WithEndpoint(os.Getenv("AZURE_SAFETY_ENDPOINT")),
azuresafety.WithAPIKey(os.Getenv("AZURE_SAFETY_KEY")),
)
if err != nil {
log.Fatal(err)
}
result, err := g.Validate(context.Background(), guard.GuardInput{
Content: "Hello, how are you?",
Role: "input",
})
if err != nil {
log.Fatal(err)
}
if result.Allowed {
fmt.Println("Content is safe")
} else {
fmt.Printf("Blocked: %s\n", result.Reason)
}
}
OptionTypeDefaultDescription
WithEndpoint(url)string(required)Azure Content Safety endpoint URL
WithAPIKey(key)string(required)Azure Content Safety subscription key
WithThreshold(n)int2Severity threshold (0-6); content at or above is blocked
WithTimeout(d)time.Duration15sHTTP request timeout

Azure Content Safety evaluates text across four categories, each scored from 0 (safe) to 6 (severe):

CategoryDescription
HateHate speech and discriminatory content
SelfHarmContent related to self-harm or suicide
SexualSexually explicit content
ViolenceViolent content or threats

Content is blocked when any category severity meets or exceeds the configured threshold.

The threshold controls sensitivity. Lower values are stricter:

// Strict: block anything above safe (severity >= 1)
g, err := azuresafety.New(
azuresafety.WithEndpoint(endpoint),
azuresafety.WithAPIKey(apiKey),
azuresafety.WithThreshold(1),
)
// Lenient: only block severe content (severity >= 4)
g, err := azuresafety.New(
azuresafety.WithEndpoint(endpoint),
azuresafety.WithAPIKey(apiKey),
azuresafety.WithThreshold(4),
)

Use the guard in a safety pipeline:

pipeline := guard.NewPipeline(
guard.Input(azureGuard),
guard.Output(azureGuard),
)
result, err := pipeline.ValidateInput(ctx, userMessage)
if !result.Allowed {
fmt.Printf("Blocked by %s: %s\n", result.GuardName, result.Reason)
// result.Reason: "flagged: Hate(severity=4), Violence(severity=2)"
}

The guard reports its name as "azure_content_safety" in GuardResult.GuardName.

  1. Create an Azure Content Safety resource in the Azure Portal
  2. Copy the endpoint URL and subscription key from the resource’s “Keys and Endpoint” page
  3. The endpoint format is: https://<resource-name>.cognitiveservices.azure.com
result, err := g.Validate(ctx, input)
if err != nil {
// Possible errors:
// - "azuresafety: endpoint is required" (missing endpoint)
// - "azuresafety: API key is required" (missing key)
// - "azuresafety: validate: ..." (API request failure)
log.Fatal(err)
}