guardrails

command
v0.1.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 7 Imported by: 0

README

Guardrails Example

This example demonstrates how to implement guardrails for LLMs and tools in the Agent SDK.

Features

  • Content filtering to block or redact harmful content
  • Token limiting to control response length
  • PII (Personally Identifiable Information) filtering
  • Tool usage restrictions
  • Rate limiting to prevent excessive API calls

Usage

Prerequisites
  • Set the OPENAI_API_KEY environment variable with your OpenAI API key
  • For web search tool functionality, set GOOGLE_API_KEY and GOOGLE_SEARCH_ENGINE_ID
export OPENAI_API_KEY=your_openai_api_key
export GOOGLE_API_KEY=your_google_api_key
export GOOGLE_SEARCH_ENGINE_ID=your_search_engine_id
Running the Example
go run main.go

Code Explanation

Creating Guardrails
// Content filter
contentFilter := guardrails.NewContentFilter(
    []string{"hate", "violence", "profanity", "sexual"},
    guardrails.RedactAction,
)

// Token limit
tokenLimit := guardrails.NewTokenLimit(
    100,
    nil, // Use simple token counter
    guardrails.RedactAction,
    "end",
)

// PII filter
piiFilter := guardrails.NewPiiFilter(
    guardrails.RedactAction,
)

// Tool restriction
toolRestriction := guardrails.NewToolRestriction(
    []string{"web_search", "calculator"},
    guardrails.BlockAction,
)

// Rate limit
rateLimit := guardrails.NewRateLimit(
    10, // 10 requests per minute
    guardrails.BlockAction,
)
Creating a Guardrails Pipeline
pipeline := guardrails.NewPipeline(
    []guardrails.Guardrail{
        contentFilter,
        tokenLimit,
        piiFilter,
        toolRestriction,
        rateLimit,
    },
    logger,
)
Applying Guardrails to LLM
openaiClient := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
llmWithGuardrails := guardrails.NewLLMMiddleware(openaiClient, pipeline)

// Use the guarded LLM
response, err := llmWithGuardrails.Generate(ctx, prompt, nil)
Applying Guardrails to Tools
tool := websearch.New(
    os.Getenv("GOOGLE_API_KEY"),
    os.Getenv("GOOGLE_SEARCH_ENGINE_ID"),
)
toolWithGuardrails := guardrails.NewToolMiddleware(tool, pipeline)

// Use the guarded tool
output, err := toolWithGuardrails.Run(ctx, input)

Available Actions

When a guardrail is triggered, it can take one of these actions:

  • RedactAction - Redacts or modifies the content to remove problematic parts
  • BlockAction - Blocks the request entirely and returns an error
  • LogAction - Allows the request but logs the violation

Customization

You can create custom guardrails by implementing the Guardrail interface:

type Guardrail interface {
    ProcessLLMRequest(ctx context.Context, request *LLMRequest) (*LLMRequest, error)
    ProcessLLMResponse(ctx context.Context, response *LLMResponse) (*LLMResponse, error)
    ProcessToolRequest(ctx context.Context, request *ToolRequest) (*ToolRequest, error)
    ProcessToolResponse(ctx context.Context, response *ToolResponse) (*ToolResponse, error)
}

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL