Skip to main content

adk_guardrail/
traits.rs

1use adk_core::Content;
2use async_trait::async_trait;
3use serde::{Deserialize, Serialize};
4
5/// Severity level for guardrail failures
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7pub enum Severity {
8    Low,
9    Medium,
10    High,
11    Critical,
12}
13
14/// Result of guardrail validation
15#[derive(Debug, Clone)]
16pub enum GuardrailResult {
17    /// Content passed validation
18    Pass,
19    /// Content failed validation
20    Fail { reason: String, severity: Severity },
21    /// Content was transformed (e.g., PII redacted)
22    Transform { new_content: Content, reason: String },
23}
24
25impl GuardrailResult {
26    pub fn pass() -> Self {
27        Self::Pass
28    }
29
30    pub fn fail(reason: impl Into<String>, severity: Severity) -> Self {
31        Self::Fail { reason: reason.into(), severity }
32    }
33
34    pub fn transform(new_content: Content, reason: impl Into<String>) -> Self {
35        Self::Transform { new_content, reason: reason.into() }
36    }
37
38    pub fn is_pass(&self) -> bool {
39        matches!(self, Self::Pass)
40    }
41
42    pub fn is_fail(&self) -> bool {
43        matches!(self, Self::Fail { .. })
44    }
45}
46
47/// Core guardrail trait for input/output validation
48#[async_trait]
49pub trait Guardrail: Send + Sync {
50    /// Unique name for this guardrail
51    fn name(&self) -> &str;
52
53    /// Validate content and return result
54    async fn validate(&self, content: &Content) -> GuardrailResult;
55
56    /// Whether to run in parallel with other guardrails (default: true)
57    fn run_parallel(&self) -> bool {
58        true
59    }
60
61    /// Whether to fail fast on this guardrail's failure (default: true for High/Critical)
62    fn fail_fast(&self) -> bool {
63        true
64    }
65}