Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Guardrails

Guardrails provide input/output validation to protect production agents from malicious input and sensitive information leakage.

Core Concepts

ConceptTypeDescription
InputGuardrailTraitValidates agent input before execution
OutputGuardrailTraitValidates agent output after execution
GuardrailsConfigStructConfigures input + output guardrails
GuardrailRunnerStructExecutes guardrail validation pipeline
GuardedAgentStructAgent wrapper with automatic guardrail enforcement

Guardrail Result

Input and output guardrails return different result types, so the type system enforces that Modify can only be produced on the output side.

#![allow(unused)]
fn main() {
// Input side: no Modify variant — input guardrails can only Pass or Block
pub enum InputGuardrailResult {
    Pass,
    Block { reason: String },
}

// Output side: Modify is the only place rewriting is allowed
pub enum OutputGuardrailResult {
    Pass,
    Block { reason: String },
    Modify { new_value: String },
}
}

Built-in Validators

ValidatorSideDescription
MaxLengthGuardrailInputBlocks if input exceeds character limit
ForbiddenWordsGuardrailInputBlocks on forbidden word match (case-insensitive)
SensitiveInfoGuardrailOutputDetects API keys, emails, credit cards, sensitive keywords

Usage

#![allow(unused)]
fn main() {
use langchainrust::{
    GuardrailsConfig, GuardedAgent, GuardrailRunner,
    MaxLengthGuardrail, ForbiddenWordsGuardrail, SensitiveInfoGuardrail,
};
use std::sync::Arc;

// Configure guardrails
let config = GuardrailsConfig::new()
    .with_input(Arc::new(MaxLengthGuardrail::new(1000)))
    .with_input(Arc::new(ForbiddenWordsGuardrail::new(vec![
        "hack".into(), "exploit".into(),
    ])))
    .with_output(Arc::new(SensitiveInfoGuardrail::new()));

// Wrap agent with guardrails
let mut guarded = GuardedAgent::new(
    Arc::new(executor),
    config,
);

let result = guarded.invoke("What is my password?".to_string()).await?;
// Returns Err(GuardrailError::Blocked { .. }) if guardrail triggers

// Check violations
let violations = guarded.violations();
}

SensitiveInfoGuardrail Details

Detects:

  • Sensitive keywords: “password”, “token”, “secret”, “api_key”, “credential” (plus Chinese equivalents)
  • OpenAI-style API keys (regex sk-...)
  • Email addresses
  • Credit card numbers (with Luhn validation)
#![allow(unused)]
fn main() {
let guardrail = SensitiveInfoGuardrail::new()
    .with_keywords(vec!["internal_key".into()]);
}

Custom Guardrails

#![allow(unused)]
fn main() {
use langchainrust::{InputGuardrail, InputGuardrailResult};

struct NoSQLInjection;

#[async_trait]
impl InputGuardrail for NoSQLInjection {
    fn name(&self) -> &str { "no_sql_injection" }
    async fn validate(&self, input: &str) -> InputGuardrailResult {
        if input.contains("{$") || input.contains("{$gt:") {
            InputGuardrailResult::Block { reason: "Potential NoSQL injection".into() }
        } else {
            InputGuardrailResult::Pass
        }
    }
}
}

输入护栏返回 [InputGuardrailResult](没有 Modify 变体),输出护栏返回 [OutputGuardrailResult](含 Modify)。类型系统强制“修改结果仅输出侧可产生“。