guardrail

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package guardrail provides output validation guardrails for AI agent responses.

Guardrails enforce contracts on agent output — reject, retry, or constrain responses based on structural, lexical, or semantic rules.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BannedWordsGuardrail

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

BannedWordsGuardrail validates that output does not contain any banned words.

func NewBannedWordsGuardrail

func NewBannedWordsGuardrail(words ...string) *BannedWordsGuardrail

NewBannedWordsGuardrail creates a BannedWordsGuardrail with the given banned words.

func (*BannedWordsGuardrail) Validate

func (g *BannedWordsGuardrail) Validate(output string) error

Validate checks the output for banned words.

type Chain added in v0.9.0

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

Chain runs multiple guardrails sequentially and collects all errors. It implements both Guardrail and ContextGuardrail.

func NewChain added in v0.9.0

func NewChain(guardrails []Guardrail, opts ...ChainOption) *Chain

NewChain creates a Chain wrapping the given guardrails.

func (*Chain) Validate added in v0.9.0

func (c *Chain) Validate(output string) error

Validate runs each guardrail sequentially and collects all non-nil errors.

func (*Chain) ValidateWithContext added in v0.9.0

func (c *Chain) ValidateWithContext(ctx context.Context, output string) error

ValidateWithContext runs each guardrail sequentially, passing ctx to child guardrails that implement ContextGuardrail.

type ChainOption added in v0.9.0

type ChainOption func(*Chain)

ChainOption configures a Chain.

func WithMaxErrors added in v0.9.0

func WithMaxErrors(n int) ChainOption

WithMaxErrors limits the number of errors collected before stopping (0 = unlimited).

func WithStopOnFirst added in v0.9.0

func WithStopOnFirst(stop bool) ChainOption

WithStopOnFirst configures whether the chain stops on the first error (default false).

type ContainsGuardrail

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

ContainsGuardrail validates that output contains all required substrings.

func NewContainsGuardrail

func NewContainsGuardrail(substrings ...string) *ContainsGuardrail

NewContainsGuardrail creates a ContainsGuardrail requiring the given substrings.

func (*ContainsGuardrail) Validate

func (g *ContainsGuardrail) Validate(output string) error

Validate checks the output contains all required substrings.

type ContextGuardrail added in v0.9.0

type ContextGuardrail interface {
	Guardrail
	ValidateWithContext(ctx context.Context, output string) error
}

ContextGuardrail is an optional extension of Guardrail for guardrails that need runtime context such as task description, agent goal, or source materials. The validation loop detects ContextGuardrail via type assertion and calls ValidateWithContext instead of Validate when available.

type Guardrail

type Guardrail interface {
	Validate(output string) error
}

Guardrail validates an output string and returns an error if validation fails.

type HallucinationGuardrail added in v0.9.0

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

HallucinationGuardrail cross-checks output claims against source context using an LLM.

func NewHallucinationGuardrail added in v0.9.0

func NewHallucinationGuardrail(sourceContext string, client llm.Client, opts ...HallucinationGuardrailOption) *HallucinationGuardrail

NewHallucinationGuardrail creates a guardrail that detects hallucinations by cross-checking output against the given source context.

func (*HallucinationGuardrail) Validate added in v0.9.0

func (g *HallucinationGuardrail) Validate(output string) error

Validate checks the output for unsupported claims against the source context.

type HallucinationGuardrailOption added in v0.9.0

type HallucinationGuardrailOption func(*HallucinationGuardrail)

HallucinationGuardrailOption configures a HallucinationGuardrail.

func WithHallucinationJudgePrompt added in v0.9.0

func WithHallucinationJudgePrompt(tmpl string) HallucinationGuardrailOption

WithHallucinationJudgePrompt sets a custom judge prompt template with {source_context} and {output} placeholders.

type JSONSchemaGuardrail

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

JSONSchemaGuardrail validates that output is valid JSON matching a JSON Schema (draft-07).

func NewJSONSchemaGuardrail

func NewJSONSchemaGuardrail(schemaStr string) (*JSONSchemaGuardrail, error)

NewJSONSchemaGuardrail creates a JSONSchemaGuardrail with the given JSON Schema string. Returns error if the schema is invalid.

func (*JSONSchemaGuardrail) Validate

func (g *JSONSchemaGuardrail) Validate(output string) error

Validate checks the output conforms to the JSON schema.

type LLMGuardrail added in v0.9.0

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

LLMGuardrail uses an LLM to judge whether the output satisfies a description.

func NewLLMGuardrail added in v0.9.0

func NewLLMGuardrail(description string, client llm.Client, opts ...LLMGuardrailOption) *LLMGuardrail

NewLLMGuardrail creates an LLMGuardrail with the given description and LLM client.

func (*LLMGuardrail) Validate added in v0.9.0

func (g *LLMGuardrail) Validate(output string) error

Validate checks whether the output satisfies the guardrail description using the LLM.

type LLMGuardrailOption added in v0.9.0

type LLMGuardrailOption func(*LLMGuardrail)

LLMGuardrailOption configures an LLMGuardrail.

func WithJudgePrompt added in v0.9.0

func WithJudgePrompt(tmpl string) LLMGuardrailOption

WithJudgePrompt sets a custom judge prompt template with {output} and {description} placeholders.

type LengthGuardrail

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

LengthGuardrail validates that output length is within the specified bounds.

func NewLengthGuardrail

func NewLengthGuardrail(min, max int) *LengthGuardrail

NewLengthGuardrail creates a LengthGuardrail with the given min/max bounds.

func (*LengthGuardrail) Validate

func (g *LengthGuardrail) Validate(output string) error

Validate checks the output length is within bounds.

type RegexGuardrail

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

RegexGuardrail validates that output matches a given regular expression.

func NewRegexGuardrail

func NewRegexGuardrail(pattern string) (*RegexGuardrail, error)

NewRegexGuardrail creates a RegexGuardrail. Returns error if pattern is invalid.

func (*RegexGuardrail) Validate

func (g *RegexGuardrail) Validate(output string) error

Validate checks the output against the regex pattern.

type ValidationError added in v0.9.0

type ValidationError struct {
	Errors []ValidationFailure
}

ValidationError aggregates zero or more ValidationFailure entries.

func (*ValidationError) Error added in v0.9.0

func (e *ValidationError) Error() string

func (*ValidationError) Unwrap added in v0.9.0

func (e *ValidationError) Unwrap() error

Unwrap returns the first ValidationFailure, enabling errors.Is and errors.As traversal.

type ValidationFailure added in v0.9.0

type ValidationFailure struct {
	Field    string
	Expected string
	Got      string
	Message  string
}

ValidationFailure represents a single validation failure with optional field-level metadata.

func (ValidationFailure) Error added in v0.9.0

func (f ValidationFailure) Error() string

Jump to

Keyboard shortcuts

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