8 releases
Uses new Rust 2024
| new 0.3.2 | Feb 21, 2026 |
|---|---|
| 0.3.1 | Feb 15, 2026 |
| 0.2.1 | Jan 22, 2026 |
| 0.1.9 | Jan 3, 2026 |
| 0.1.8 | Dec 28, 2025 |
#1326 in Asynchronous
Used in 2 crates
120KB
2.5K
SLoC
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
[dependencies]
adk-guardrail = "0.3.2"
# With JSON schema validation (default)
adk-guardrail = { version = "0.3.2", features = ["schema"] }
Quick Start
PII Redaction
use adk_guardrail::{PiiRedactor, Guardrail};
use adk_core::Content;
let redactor = PiiRedactor::new();
let content = Content::new("user").with_text("Contact me at test@example.com");
let result = redactor.validate(&content).await;
// Result is Transform with "[EMAIL REDACTED]"
Content Filtering
use adk_guardrail::ContentFilter;
// Block harmful content
let filter = ContentFilter::harmful_content();
// Keep responses on-topic
let filter = ContentFilter::on_topic("cooking", vec!["recipe".into(), "bake".into()]);
// Limit response length
let filter = ContentFilter::max_length(1000);
// Block specific keywords
let filter = ContentFilter::blocked_keywords(vec!["forbidden".into()]);
Agent Integration
Requires guardrails feature on adk-agent:
adk-agent = { version = "0.3.2", features = ["guardrails"] }
use adk_agent::LlmAgentBuilder;
use adk_guardrail::{GuardrailSet, ContentFilter, PiiRedactor};
let input_guardrails = GuardrailSet::new()
.with(ContentFilter::harmful_content())
.with(PiiRedactor::new());
let agent = LlmAgentBuilder::new("assistant")
.input_guardrails(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 adk_guardrail::SchemaValidator;
use serde_json::json;
let schema = json!({
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" }
},
"required": ["name"]
});
let validator = SchemaValidator::new("user_schema", schema)?;
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.
Dependencies
~11–18MB
~232K SLoC