Documentation
¶
Overview ¶
Package guardrails provides a pluggable pipeline for request-level guardrails.
Guardrails intercept requests before they reach providers, allowing validation, modification, or rejection.
Guardrails operate on a normalized []Message DTO, decoupled from concrete API request types (ChatRequest, ResponsesRequest, etc.). Adapters in the GuardedProvider convert between concrete requests and the message list.
Execution is driven by a per-guardrail "order" value:
- Guardrails with the same order run in parallel (concurrently).
- Groups are executed sequentially in ascending order.
- Each group receives the output of the previous group.
Example with orders 0, 0, 1, 2, 2:
Group 0 ──┬── guardrail A ──┬──▶ Group 1 ── guardrail C ──▶ Group 2 ──┬── guardrail D ──┬──▶ done
└── guardrail B ──┘ └── guardrail E ──┘
Index ¶
- Constants
- Variables
- func ComputeGuardrailsHash(rules []RuleDescriptor) string
- func EffectiveLLMBasedAlteringMaxTokens(maxTokens int) int
- func IsValidationError(err error) bool
- func NormalizeLLMBasedAlteringRoles(roles []string) ([]string, error)
- func ResolveLLMBasedAlteringPrompt(prompt string) string
- type Catalog
- type ChatCompletionExecutor
- type ContextPipelineResolver
- type Definition
- type Guardrail
- type LLMBasedAlteringConfig
- type LLMBasedAlteringGuardrail
- type Message
- type MongoDBStore
- func (s *MongoDBStore) Close() error
- func (s *MongoDBStore) Delete(ctx context.Context, name string) error
- func (s *MongoDBStore) Get(ctx context.Context, name string) (*Definition, error)
- func (s *MongoDBStore) List(ctx context.Context) ([]Definition, error)
- func (s *MongoDBStore) Upsert(ctx context.Context, definition Definition) error
- func (s *MongoDBStore) UpsertMany(ctx context.Context, definitions []Definition) error
- type Pipeline
- type PostgreSQLStore
- func (s *PostgreSQLStore) Close() error
- func (s *PostgreSQLStore) Delete(ctx context.Context, name string) error
- func (s *PostgreSQLStore) Get(ctx context.Context, name string) (*Definition, error)
- func (s *PostgreSQLStore) List(ctx context.Context) ([]Definition, error)
- func (s *PostgreSQLStore) Upsert(ctx context.Context, definition Definition) error
- func (s *PostgreSQLStore) UpsertMany(ctx context.Context, definitions []Definition) error
- type Registry
- type Result
- type RuleDescriptor
- type SQLiteStore
- func (s *SQLiteStore) Close() error
- func (s *SQLiteStore) Delete(ctx context.Context, name string) error
- func (s *SQLiteStore) Get(ctx context.Context, name string) (*Definition, error)
- func (s *SQLiteStore) List(ctx context.Context) ([]Definition, error)
- func (s *SQLiteStore) Upsert(ctx context.Context, definition Definition) error
- func (s *SQLiteStore) UpsertMany(ctx context.Context, definitions []Definition) error
- type Service
- func (s *Service) BuildPipeline(steps []StepReference) (*Pipeline, string, error)
- func (s *Service) Delete(ctx context.Context, name string) error
- func (s *Service) Get(name string) (*Definition, bool)
- func (s *Service) Len() int
- func (s *Service) List() []Definition
- func (s *Service) ListViews() []View
- func (s *Service) Names() []string
- func (s *Service) Refresh(ctx context.Context) error
- func (s *Service) SetExecutor(ctx context.Context, executor ChatCompletionExecutor) error
- func (s *Service) TypeDefinitions() []TypeDefinition
- func (s *Service) Upsert(ctx context.Context, definition Definition) error
- func (s *Service) UpsertDefinitions(ctx context.Context, definitions []Definition) error
- type StepReference
- type Store
- type SystemPromptGuardrail
- type SystemPromptMode
- type TypeDefinition
- type TypeField
- type TypeOption
- type ValidationError
- type View
- type WorkflowBatchPreparer
- type WorkflowRequestPatcher
Constants ¶
const (
DefaultLLMBasedAlteringMaxTokens = 4096
)
const DefaultLLMBasedAlteringPrompt = `` /* 8087-byte string literal not displayed */
DefaultLLMBasedAlteringPrompt is the built-in prompt used when no custom prompt is configured. It is derived from LiteLLM's data anonymization guardrail prompt and instructs the model to rewrite text conservatively.
Variables ¶
var ErrNotFound = errors.New("guardrail not found")
ErrNotFound indicates a requested guardrail was not found.
Functions ¶
func ComputeGuardrailsHash ¶
func ComputeGuardrailsHash(rules []RuleDescriptor) string
ComputeGuardrailsHash computes the guardrails_hash for a set of rule identifiers. Each rule is represented as "name:type:order:mode:content_hash". The combined seed is sorted for stability, then passed through SHA-256. Uses xxhash64 per-component and SHA-256 for the final hash to balance speed and collision resistance.
The hash is carried on the request context (core.WithGuardrailsHash) and consumed by the response cache as an opaque key component, so guardrail configuration changes invalidate cached completions.
func EffectiveLLMBasedAlteringMaxTokens ¶
EffectiveLLMBasedAlteringMaxTokens returns the effective max_tokens value for the auxiliary rewrite request.
func IsValidationError ¶
IsValidationError reports whether err is a validation error.
func NormalizeLLMBasedAlteringRoles ¶
NormalizeLLMBasedAlteringRoles validates, lowercases, and deduplicates the configured target roles.
func ResolveLLMBasedAlteringPrompt ¶
ResolveLLMBasedAlteringPrompt returns the effective system prompt.
Types ¶
type Catalog ¶
type Catalog interface {
Len() int
Names() []string
BuildPipeline(steps []StepReference) (*Pipeline, string, error)
}
Catalog resolves named guardrail references into executable pipelines. BuildPipeline returns the compiled pipeline, a deterministic configuration hash for cache/change detection, and an error. The hash should change whenever the effective pipeline configuration changes; empty is reserved for "no pipeline".
type ChatCompletionExecutor ¶
type ChatCompletionExecutor interface {
ChatCompletion(ctx context.Context, req *core.ChatRequest) (*core.ChatResponse, error)
}
ChatCompletionExecutor provides the auxiliary model call used by llm_based_altering guardrails.
type ContextPipelineResolver ¶
ContextPipelineResolver resolves a request-scoped guardrails pipeline.
type Definition ¶
type Definition struct {
Name string `json:"name" bson:"name"`
Type string `json:"type" bson:"type"`
Description string `json:"description,omitempty" bson:"description,omitempty"`
UserPath string `json:"user_path,omitempty" bson:"user_path,omitempty"`
Config json.RawMessage `json:"config" bson:"config"`
CreatedAt time.Time `json:"created_at" bson:"created_at"`
UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
}
Definition is one persisted reusable guardrail instance.
type Guardrail ¶
type Guardrail interface {
// Name returns a human-readable identifier for this guardrail.
Name() string
// Process processes a normalized message list.
// Return the (possibly modified) messages, or an error to reject the request.
Process(ctx context.Context, msgs []Message) ([]Message, error)
}
Guardrail processes a message list and returns the (possibly modified) messages or an error. Returning an error rejects the request before it reaches the provider.
type LLMBasedAlteringConfig ¶
type LLMBasedAlteringConfig struct {
Model string
Provider string
UserPath string
Prompt string
Roles []string
SkipContentPrefix string
MaxTokens int
}
LLMBasedAlteringConfig holds the normalized configuration for the auxiliary LLM-backed message rewriting guardrail.
func NormalizeLLMBasedAlteringConfig ¶
func NormalizeLLMBasedAlteringConfig(cfg LLMBasedAlteringConfig) (LLMBasedAlteringConfig, error)
NormalizeLLMBasedAlteringConfig resolves defaults for the auxiliary LLM guardrail config.
type LLMBasedAlteringGuardrail ¶
type LLMBasedAlteringGuardrail struct {
// contains filtered or unexported fields
}
LLMBasedAlteringGuardrail rewrites targeted message contents by calling an auxiliary model before the main provider request runs.
func NewLLMBasedAlteringGuardrail ¶
func NewLLMBasedAlteringGuardrail(name string, cfg LLMBasedAlteringConfig, executor ChatCompletionExecutor) (*LLMBasedAlteringGuardrail, error)
NewLLMBasedAlteringGuardrail constructs an LLM-backed content rewriting guardrail.
func (*LLMBasedAlteringGuardrail) Name ¶
func (g *LLMBasedAlteringGuardrail) Name() string
Name returns the configured guardrail name.
type Message ¶
type Message struct {
Role string // "system", "user", "assistant", "tool"
Content string
ToolCalls []core.ToolCall
ToolCallID string
ContentNull bool
}
Message represents a single message in a conversation. This is the normalized DTO that all text guardrails operate on, decoupled from concrete API request types.
type MongoDBStore ¶
type MongoDBStore struct {
// contains filtered or unexported fields
}
MongoDBStore stores guardrail definitions in MongoDB.
func NewMongoDBStore ¶
NewMongoDBStore creates collection indexes if needed.
func (*MongoDBStore) Close ¶
func (s *MongoDBStore) Close() error
func (*MongoDBStore) Get ¶
func (s *MongoDBStore) Get(ctx context.Context, name string) (*Definition, error)
func (*MongoDBStore) List ¶
func (s *MongoDBStore) List(ctx context.Context) ([]Definition, error)
func (*MongoDBStore) Upsert ¶
func (s *MongoDBStore) Upsert(ctx context.Context, definition Definition) error
func (*MongoDBStore) UpsertMany ¶
func (s *MongoDBStore) UpsertMany(ctx context.Context, definitions []Definition) error
type Pipeline ¶
type Pipeline struct {
// contains filtered or unexported fields
}
Pipeline orchestrates the execution of multiple guardrails.
Guardrails are grouped by their order value. Groups execute sequentially in ascending order. Within a group, all guardrails run in parallel. If a group contains a single guardrail, it runs directly (no goroutine overhead).
func NewPipeline ¶
func NewPipeline() *Pipeline
NewPipeline creates a new empty guardrails pipeline.
func (*Pipeline) Add ¶
Add appends a guardrail with the given execution order. Guardrails with the same order run in parallel; different orders run sequentially.
type PostgreSQLStore ¶
type PostgreSQLStore struct {
// contains filtered or unexported fields
}
PostgreSQLStore stores guardrail definitions in PostgreSQL.
func NewPostgreSQLStore ¶
NewPostgreSQLStore creates the guardrail table and indexes if needed.
func (*PostgreSQLStore) Close ¶
func (s *PostgreSQLStore) Close() error
func (*PostgreSQLStore) Delete ¶
func (s *PostgreSQLStore) Delete(ctx context.Context, name string) error
func (*PostgreSQLStore) Get ¶
func (s *PostgreSQLStore) Get(ctx context.Context, name string) (*Definition, error)
func (*PostgreSQLStore) List ¶
func (s *PostgreSQLStore) List(ctx context.Context) ([]Definition, error)
func (*PostgreSQLStore) Upsert ¶
func (s *PostgreSQLStore) Upsert(ctx context.Context, definition Definition) error
func (*PostgreSQLStore) UpsertMany ¶
func (s *PostgreSQLStore) UpsertMany(ctx context.Context, definitions []Definition) error
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry stores named guardrails so workflows can reference them by id.
func NewRegistry ¶
func NewRegistry() *Registry
NewRegistry creates an empty named guardrail registry.
func (*Registry) BuildPipeline ¶
func (r *Registry) BuildPipeline(steps []StepReference) (*Pipeline, string, error)
BuildPipeline resolves named guardrail references into an executable pipeline and hash.
type Result ¶
type Result struct {
Service *Service
Store Store
Storage storage.Storage
RefreshErrors <-chan error
// contains filtered or unexported fields
}
Result holds the initialized guardrail service and any owned resources.
func New ¶
func New(ctx context.Context, cfg *config.Config, refreshInterval time.Duration, executors ...ChatCompletionExecutor) (*Result, error)
New creates a guardrails subsystem with its own storage connection.
type RuleDescriptor ¶
RuleDescriptor describes a single active guardrail rule for hashing.
type SQLiteStore ¶
type SQLiteStore struct {
// contains filtered or unexported fields
}
SQLiteStore stores guardrail definitions in SQLite.
func NewSQLiteStore ¶
NewSQLiteStore creates the guardrail table and indexes if needed.
func (*SQLiteStore) Close ¶
func (s *SQLiteStore) Close() error
func (*SQLiteStore) Get ¶
func (s *SQLiteStore) Get(ctx context.Context, name string) (*Definition, error)
func (*SQLiteStore) List ¶
func (s *SQLiteStore) List(ctx context.Context) ([]Definition, error)
func (*SQLiteStore) Upsert ¶
func (s *SQLiteStore) Upsert(ctx context.Context, definition Definition) error
func (*SQLiteStore) UpsertMany ¶
func (s *SQLiteStore) UpsertMany(ctx context.Context, definitions []Definition) error
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service keeps reusable guardrails cached in memory and refreshes them from storage.
func NewService ¶
func NewService(store Store, executors ...ChatCompletionExecutor) (*Service, error)
NewService creates a guardrail service backed by the provided store.
func (*Service) BuildPipeline ¶
func (s *Service) BuildPipeline(steps []StepReference) (*Pipeline, string, error)
BuildPipeline resolves named steps through the current in-memory guardrail registry.
func (*Service) Delete ¶
Delete removes a guardrail definition from storage and swaps the snapshot on success.
func (*Service) Get ¶
func (s *Service) Get(name string) (*Definition, bool)
Get returns one cached guardrail by name.
func (*Service) List ¶
func (s *Service) List() []Definition
List returns all cached guardrail definitions sorted by name.
func (*Service) ListViews ¶
ListViews returns all cached guardrail definitions with lightweight summaries.
func (*Service) Refresh ¶
Refresh reloads guardrails from storage and atomically swaps the in-memory snapshot.
func (*Service) SetExecutor ¶
func (s *Service) SetExecutor(ctx context.Context, executor ChatCompletionExecutor) error
SetExecutor swaps the auxiliary chat executor used by llm_based_altering guardrails and rebuilds the in-memory snapshot atomically.
func (*Service) TypeDefinitions ¶
func (s *Service) TypeDefinitions() []TypeDefinition
TypeDefinitions returns the supported guardrail type schemas.
func (*Service) Upsert ¶
func (s *Service) Upsert(ctx context.Context, definition Definition) error
Upsert validates and stores a guardrail definition, then swaps the snapshot on success.
func (*Service) UpsertDefinitions ¶
func (s *Service) UpsertDefinitions(ctx context.Context, definitions []Definition) error
UpsertDefinitions validates and upserts a definition set, then swaps the snapshot on success.
type StepReference ¶
StepReference points to one named guardrail and the step it should run at.
type Store ¶
type Store interface {
List(ctx context.Context) ([]Definition, error)
Get(ctx context.Context, name string) (*Definition, error)
Upsert(ctx context.Context, definition Definition) error
UpsertMany(ctx context.Context, definitions []Definition) error
Delete(ctx context.Context, name string) error
Close() error
}
Store defines persistence operations for reusable guardrail definitions.
type SystemPromptGuardrail ¶
type SystemPromptGuardrail struct {
// contains filtered or unexported fields
}
SystemPromptGuardrail injects, overrides, or decorates system messages.
func NewSystemPromptGuardrail ¶
func NewSystemPromptGuardrail(name string, mode SystemPromptMode, content string) (*SystemPromptGuardrail, error)
NewSystemPromptGuardrail creates a new system prompt guardrail instance. name identifies this instance (e.g. "safety-prompt", "compliance-check"). mode must be "inject", "override", or "decorator". content is the system prompt text to apply.
func (*SystemPromptGuardrail) Name ¶
func (g *SystemPromptGuardrail) Name() string
Name returns this instance's name.
type SystemPromptMode ¶
type SystemPromptMode string
SystemPromptMode defines how the system prompt guardrail modifies messages.
const ( // SystemPromptInject adds a system message only if none exists. SystemPromptInject SystemPromptMode = "inject" // SystemPromptOverride replaces all existing system messages with the configured one. SystemPromptOverride SystemPromptMode = "override" // SystemPromptDecorator prepends the configured content to the first existing // system message (separated by a newline), or adds a new system message if none exists. SystemPromptDecorator SystemPromptMode = "decorator" )
type TypeDefinition ¶
type TypeDefinition struct {
Type string `json:"type"`
Label string `json:"label"`
Description string `json:"description,omitempty"`
Defaults json.RawMessage `json:"defaults"`
Fields []TypeField `json:"fields"`
}
TypeDefinition describes one supported guardrail type and its config schema.
func TypeDefinitions ¶
func TypeDefinitions() []TypeDefinition
TypeDefinitions returns the UI-facing definitions for supported guardrail types.
type TypeField ¶
type TypeField struct {
Key string `json:"key"`
Label string `json:"label"`
Input string `json:"input"`
Required bool `json:"required"`
Help string `json:"help,omitempty"`
Placeholder string `json:"placeholder,omitempty"`
Options []TypeOption `json:"options,omitempty"`
}
TypeField describes one UI field for a guardrail type.
type TypeOption ¶
TypeOption is one allowed option for a typed guardrail config field.
type ValidationError ¶
type ValidationError = validation.Error
ValidationError indicates invalid guardrail input or state.
type View ¶
type View struct {
Definition
Summary string `json:"summary,omitempty"`
}
View is the admin-facing representation of a persisted guardrail.
func ViewFromDefinition ¶
func ViewFromDefinition(def Definition) View
ViewFromDefinition projects one guardrail definition into its admin-facing view.
type WorkflowBatchPreparer ¶
type WorkflowBatchPreparer struct {
// contains filtered or unexported fields
}
WorkflowBatchPreparer applies the guardrails pipeline selected by the current workflow.
func NewWorkflowBatchPreparer ¶
func NewWorkflowBatchPreparer(provider core.RoutableProvider, resolver ContextPipelineResolver) *WorkflowBatchPreparer
NewWorkflowBatchPreparer creates a native-batch preparer that resolves its pipeline per request.
func (*WorkflowBatchPreparer) PrepareBatchRequest ¶
func (p *WorkflowBatchPreparer) PrepareBatchRequest(ctx context.Context, providerType string, req *core.BatchRequest) (*core.BatchRewriteResult, error)
PrepareBatchRequest applies the request-scoped guardrails pipeline to native batch items.
type WorkflowRequestPatcher ¶
type WorkflowRequestPatcher struct {
// contains filtered or unexported fields
}
WorkflowRequestPatcher applies the guardrails pipeline selected by the current workflow.
func NewWorkflowRequestPatcher ¶
func NewWorkflowRequestPatcher(resolver ContextPipelineResolver) *WorkflowRequestPatcher
NewWorkflowRequestPatcher creates a translated-request patcher that resolves its pipeline from the request context on each call.
func (*WorkflowRequestPatcher) PatchChatRequest ¶
func (p *WorkflowRequestPatcher) PatchChatRequest(ctx context.Context, req *core.ChatRequest) (*core.ChatRequest, error)
PatchChatRequest applies the request-scoped guardrails pipeline to a translated chat request.
func (*WorkflowRequestPatcher) PatchResponsesRequest ¶
func (p *WorkflowRequestPatcher) PatchResponsesRequest(ctx context.Context, req *core.ResponsesRequest) (*core.ResponsesRequest, error)
PatchResponsesRequest applies the request-scoped guardrails pipeline to a translated responses request.