guardrails

package
v1.3.9 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package guardrails provides built-in ProviderHook implementations that replace the legacy validator system with hook-based guardrails.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGuardrailHook

func NewGuardrailHook(typeName string, params map[string]any) (hooks.ProviderHook, error)

NewGuardrailHook creates a guardrail ProviderHook from a validator config type name and params map. This is used by ProviderStage to instantiate pack-defined guardrails from metadata.

Types

type BannedWordsHook

type BannedWordsHook struct {
	// contains filtered or unexported fields
}

BannedWordsHook denies provider responses that contain any of the configured banned words. It implements both ProviderHook and ChunkInterceptor for streaming support.

func NewBannedWordsHook

func NewBannedWordsHook(words []string) *BannedWordsHook

NewBannedWordsHook creates a guardrail that rejects responses containing any of the given words. Matching is case-insensitive with word boundaries.

func (*BannedWordsHook) AfterCall

AfterCall checks the completed response for banned words.

func (*BannedWordsHook) BeforeCall

BeforeCall is a no-op — banned words are checked after generation.

func (*BannedWordsHook) Name

func (h *BannedWordsHook) Name() string

Name returns the guardrail type identifier.

func (*BannedWordsHook) OnChunk

OnChunk checks accumulated streaming content for banned words.

type LengthHook

type LengthHook struct {
	// contains filtered or unexported fields
}

LengthHook denies provider responses that exceed character or token limits. It implements both ProviderHook and ChunkInterceptor for streaming support.

func NewLengthHook

func NewLengthHook(maxCharacters, maxTokens int) *LengthHook

NewLengthHook creates a guardrail that rejects responses exceeding the given character and/or token limits. Pass 0 to disable a limit.

func (*LengthHook) AfterCall

AfterCall checks the completed response against length limits.

func (*LengthHook) BeforeCall

func (h *LengthHook) BeforeCall(
	_ context.Context, _ *hooks.ProviderRequest,
) hooks.Decision

BeforeCall is a no-op — length is checked after generation.

func (*LengthHook) Name

func (h *LengthHook) Name() string

Name returns the guardrail type identifier.

func (*LengthHook) OnChunk

func (h *LengthHook) OnChunk(
	_ context.Context, chunk *providers.StreamChunk,
) hooks.Decision

OnChunk checks accumulated streaming content against length limits.

type MaxSentencesHook

type MaxSentencesHook struct {
	// contains filtered or unexported fields
}

MaxSentencesHook denies provider responses that contain more than a configured number of sentences. It does not support streaming because sentence boundaries can only be reliably counted on complete content.

func NewMaxSentencesHook

func NewMaxSentencesHook(maxSentences int) *MaxSentencesHook

NewMaxSentencesHook creates a guardrail that rejects responses exceeding the given sentence count.

func (*MaxSentencesHook) AfterCall

AfterCall checks the completed response for sentence count.

func (*MaxSentencesHook) BeforeCall

BeforeCall is a no-op — sentence count is checked after generation.

func (*MaxSentencesHook) Name

func (h *MaxSentencesHook) Name() string

Name returns the guardrail type identifier.

type RequiredFieldsHook

type RequiredFieldsHook struct {
	// contains filtered or unexported fields
}

RequiredFieldsHook denies provider responses that do not contain all required field strings. It does not support streaming because all content must be available to check for field presence.

func NewRequiredFieldsHook

func NewRequiredFieldsHook(fields []string) *RequiredFieldsHook

NewRequiredFieldsHook creates a guardrail that rejects responses missing any of the given field strings.

func (*RequiredFieldsHook) AfterCall

AfterCall checks the completed response for required fields.

func (*RequiredFieldsHook) BeforeCall

BeforeCall is a no-op — field presence is checked after generation.

func (*RequiredFieldsHook) Name

func (h *RequiredFieldsHook) Name() string

Name returns the guardrail type identifier.

Jump to

Keyboard shortcuts

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