adk-guardrail
Guardrails framework for ADK agents - input/output validation, content filtering, PII redaction.
Overview
adk-guardrail provides safety and validation for AI agents in the ADK-Rust framework:
- PiiRedactor - Detect and redact PII (Email, Phone, SSN, Credit Card, IP Address)
- ContentFilter - Block harmful content, off-topic responses, max length
- SchemaValidator - Validate JSON output against schemas
- GuardrailSet - Compose multiple guardrails with parallel execution
Installation
[]
= "0.2.0"
# With JSON schema validation (default)
= { = "0.2.1", = ["schema"] }
Quick Start
PII Redaction
use ;
use Content;
let redactor = new;
let content = new.with_text;
let result = redactor.validate.await;
// Result is Transform with "[EMAIL REDACTED]"
Content Filtering
use ContentFilter;
// Block harmful content
let filter = harmful_content;
// Keep responses on-topic
let filter = on_topic;
// Limit response length
let filter = max_length;
// Block specific keywords
let filter = blocked_keywords;
Agent Integration
Requires guardrails feature on adk-agent:
= { = "0.2.1", = ["guardrails"] }
use LlmAgentBuilder;
use ;
let input_guardrails = new
.with
.with;
let agent = new
.input_guardrails
.build?;
Guardrail Results
Each guardrail returns one of three results:
| Result | Description |
|---|---|
Pass |
Content is valid, continue execution |
Fail |
Content is invalid, block with reason and severity |
Transform |
Content modified (e.g., PII redacted), continue with new content |
Severity Levels
| Level | Description |
|---|---|
Low |
Minor issue, may continue |
Medium |
Moderate issue, should review |
High |
Serious issue, should block |
Critical |
Dangerous content, must block |
Built-in Guardrails
PiiRedactor
Detects and redacts personally identifiable information:
Email→[EMAIL REDACTED]Phone→[PHONE REDACTED]Ssn→[SSN REDACTED]CreditCard→[CREDIT CARD REDACTED]IpAddress→[IP REDACTED]
ContentFilter
Validates content against rules:
harmful_content()- Blocks common harmful patternson_topic(topic, keywords)- Ensures topic relevancemax_length(n)- Limits content lengthblocked_keywords(list)- Blocks specific words
SchemaValidator
Validates JSON output against a schema (requires schema feature):
use SchemaValidator;
use json;
let schema = json!;
let validator = new?;
Features
- Parallel guardrail execution with early exit on failure
- Composable with
GuardrailSetbuilder pattern - Integration with
LlmAgentBuilderviainput_guardrails()/output_guardrails() - Async validation with the
Guardrailtrait
Related Crates
- adk-rust - Meta-crate with all components
- adk-agent - Agent implementations with guardrail support
- adk-core - Core traits and types
License
Apache-2.0
Part of ADK-Rust
This crate is part of the ADK-Rust framework for building AI agents in Rust.