Skip to main content

adk_agent/
guardrails.rs

1//! Guardrail integration for LlmAgent
2//!
3//! This module provides guardrail support when the `guardrails` feature is enabled.
4
5use adk_core::{Content, Result};
6
7#[cfg(feature = "guardrails")]
8use adk_core::AdkError;
9
10#[cfg(feature = "guardrails")]
11pub use adk_guardrail::{
12    ContentFilter, ContentFilterConfig, Guardrail, GuardrailExecutor, GuardrailResult,
13    GuardrailSet, PiiRedactor, PiiType, Severity,
14};
15
16#[cfg(feature = "guardrails")]
17pub use adk_guardrail::SchemaValidator;
18
19/// Placeholder type when guardrails feature is disabled
20#[cfg(not(feature = "guardrails"))]
21pub struct GuardrailSet;
22
23#[cfg(not(feature = "guardrails"))]
24impl GuardrailSet {
25    /// Create an empty guardrail set (no-op when feature is disabled).
26    pub fn new() -> Self {
27        Self
28    }
29    /// Returns `true` (always empty when feature is disabled).
30    pub fn is_empty(&self) -> bool {
31        true
32    }
33}
34
35#[cfg(not(feature = "guardrails"))]
36impl Default for GuardrailSet {
37    fn default() -> Self {
38        Self::new()
39    }
40}
41
42#[cfg(feature = "guardrails")]
43pub(crate) async fn enforce_guardrails(
44    guardrails: &GuardrailSet,
45    content: &Content,
46    phase: &str,
47) -> Result<Content> {
48    let result = GuardrailExecutor::run(guardrails, content)
49        .await
50        .map_err(|err| AdkError::agent(format!("{phase} guardrail failed: {err}")))?;
51
52    if !result.passed {
53        let failures = result
54            .failures
55            .iter()
56            .map(|(name, reason, severity)| format!("{name} ({severity:?}): {reason}"))
57            .collect::<Vec<_>>()
58            .join("; ");
59        return Err(AdkError::agent(format!("{phase} guardrails blocked content: {failures}")));
60    }
61
62    Ok(result.transformed_content.unwrap_or_else(|| content.clone()))
63}
64
65#[cfg(not(feature = "guardrails"))]
66pub(crate) async fn enforce_guardrails(
67    _guardrails: &GuardrailSet,
68    content: &Content,
69    _phase: &str,
70) -> Result<Content> {
71    Ok(content.clone())
72}