Skip to content
Docs

Guard Providers — AI Safety

Beluga AI v2 provides a three-stage safety pipeline for validating content at every point in the agent lifecycle. The guard.Guard interface defines a unified contract for content validation, and providers integrate with external safety APIs for enterprise-grade moderation.

type Guard interface {
Name() string
Validate(ctx context.Context, input GuardInput) (GuardResult, error)
}
type GuardInput struct {
Content string // Text to validate
Role string // Pipeline stage: "input", "output", or "tool"
Metadata map[string]any // Guard-specific key-value pairs
}
type GuardResult struct {
Allowed bool // True when content passes validation
Reason string // Explanation when blocked or modified
Modified string // Optional sanitized content
GuardName string // Which guard produced this result
}

The guard pipeline validates content at three stages, each running an ordered sequence of guards:

import "github.com/lookatitude/beluga-ai/guard"
pipeline := guard.NewPipeline(
guard.Input(inputGuard1, inputGuard2), // Validate user messages
guard.Output(outputGuard1), // Validate model responses
guard.Tool(toolGuard1), // Validate tool call arguments
)

Guards within each stage execute sequentially. The first guard that blocks content stops the pipeline for that stage. If a guard modifies content (e.g., PII redaction), subsequent guards in the same stage see the modified version.

// Validate user input
result, err := pipeline.ValidateInput(ctx, userMessage)
if err != nil {
log.Fatal(err)
}
if !result.Allowed {
fmt.Printf("Blocked by %s: %s\n", result.GuardName, result.Reason)
return
}
// Validate model output
result, err = pipeline.ValidateOutput(ctx, modelResponse)
// Validate tool call
result, err = pipeline.ValidateTool(ctx, "search_web", toolInput)

The guard package includes four built-in guards that require no external dependencies:

GuardRegistry NameDescription
Prompt Injection Detectorprompt_injection_detectorRegex-based detection of injection patterns
PII Redactorpii_redactorDetects and replaces PII (email, phone, SSN, credit card, IP)
Content Filtercontent_filterKeyword-based content moderation with threshold
SpotlightingspotlightingWraps untrusted content in delimiter markers
pipeline := guard.NewPipeline(
guard.Input(
guard.NewPromptInjectionDetector(),
guard.NewSpotlighting(""),
),
guard.Output(
guard.NewPIIRedactor(guard.DefaultPIIPatterns...),
guard.NewContentFilter(guard.WithKeywords("harmful", "dangerous")),
),
)
ProviderGuard NameDescription
Azure Content Safetyazure_content_safetyMicrosoft Azure AI Content Safety
Guardrails AIguardrails_aiGuardrails AI validators (PII, toxicity, hallucination)
Lakera Guardlakera_guardLakera prompt injection and content detection
LLM Guardllm_guardLLM Guard prompt and output scanning
NeMo Guardrailsnemo_guardrailsNVIDIA NeMo Guardrails with Colang configs

Guards can be created through the registry or constructed directly. Built-in guards register via init():

// Via registry (built-in guards)
g, err := guard.New("prompt_injection_detector", nil)
if err != nil {
log.Fatal(err)
}
// Direct construction (external providers)
g, err := azuresafety.New(
azuresafety.WithEndpoint(os.Getenv("AZURE_SAFETY_ENDPOINT")),
azuresafety.WithAPIKey(os.Getenv("AZURE_SAFETY_KEY")),
)

List all registered guards at runtime:

names := guard.List()
// Returns sorted list: ["content_filter", "pii_redactor", "prompt_injection_detector", "spotlighting"]

A production safety pipeline typically combines built-in and external guards:

import (
"github.com/lookatitude/beluga-ai/guard"
"github.com/lookatitude/beluga-ai/guard/providers/lakera"
"github.com/lookatitude/beluga-ai/guard/providers/azuresafety"
)
lakeraGuard, err := lakera.New(
lakera.WithAPIKey(os.Getenv("LAKERA_API_KEY")),
)
if err != nil {
log.Fatal(err)
}
azureGuard, err := azuresafety.New(
azuresafety.WithEndpoint(os.Getenv("AZURE_SAFETY_ENDPOINT")),
azuresafety.WithAPIKey(os.Getenv("AZURE_SAFETY_KEY")),
)
if err != nil {
log.Fatal(err)
}
pipeline := guard.NewPipeline(
guard.Input(
lakeraGuard, // Prompt injection detection
guard.NewPromptInjectionDetector(), // Regex-based fallback
),
guard.Output(
azureGuard, // Content moderation
guard.NewPIIRedactor(guard.DefaultPIIPatterns...), // PII redaction
),
guard.Tool(
guard.NewContentFilter(guard.WithKeywords("rm -rf", "DROP TABLE")),
),
)