Quick Start
User Interaction Flow
GuardrailResult
Result of guardrail validation.Factory Methods
| Method | Signature | Description |
|---|---|---|
success(result) | fn success(impl Into<String>) -> Self | Pass with modified content |
pass() | fn pass() -> Self | Pass without modification |
failure(error) | fn failure(impl Into<String>) -> Self | Fail with error message |
from_tuple(success, data) | fn from_tuple(bool, impl Into<String>) -> Self | Create from tuple |
Instance Methods
| Method | Signature | Description |
|---|---|---|
with_metadata(key, value) | fn with_metadata(self, impl Into<String>, impl Into<String>) -> Self | Add metadata |
is_success() | fn is_success(&self) -> bool | Check if passed |
is_failure() | fn is_failure(&self) -> bool | Check if failed |
get_result_or(original) | fn get_result_or(&self, &str) -> String | Get result or fallback |
Guardrail Trait
Synchronous validation interface.AsyncGuardrail Trait
Asynchronous validation interface.GuardrailAction
Action to take when validation fails.GuardrailConfig
Configuration for guardrail behavior.Builder Methods
| Method | Signature | Default | Description |
|---|---|---|---|
new() | fn new() -> Self | - | Create config |
on_failure(action) | fn on_failure(self, GuardrailAction) -> Self | Stop | Set failure action |
max_retries(n) | fn max_retries(self, usize) -> Self | 3 | Set retry limit |
fallback_response(msg) | fn fallback_response(self, impl Into<String>) -> Self | None | Set fallback |
log_results(b) | fn log_results(self, bool) -> Self | true | Enable logging |
error_template(t) | fn error_template(self, impl Into<String>) -> Self | None | Custom error format |
Common Guardrail Patterns
Length Limiter
Profanity Filter
Best Practices
Chain multiple guardrails
Chain multiple guardrails
Use different guardrails for different concerns: length, content, format.
Use async for external APIs
Use async for external APIs
When calling moderation APIs, use AsyncGuardrail to avoid blocking.
Provide helpful error messages
Provide helpful error messages
Clear error messages help users understand why content was blocked.
Configure appropriate failure actions
Configure appropriate failure actions
Use Retry for transient issues, Stop for hard violations, Fallback for graceful degradation.

