adk-agent 3.0.0

Agent implementations for Rust Agent Development Kit (ADK-Rust): LLM, custom, workflow, and team agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Guardrail integration for LlmAgent
//!
//! This module provides guardrail support when the `guardrails` feature is enabled.

use adk_core::{CitationMetadata, Content, Event, GroundingSource, Result};

#[cfg(feature = "guardrails")]
use adk_core::Part;

#[cfg(feature = "guardrails")]
pub use adk_guardrail::{
    CitationRequired, ContentFilter, ContentFilterConfig, GUARDRAIL_WARNINGS_KEY, Guardrail,
    GuardrailError, GuardrailExecutor, GuardrailFailure, GuardrailPhase, GuardrailResult,
    GuardrailSet, INPUT_BLOCKED_CODE, OUTPUT_BLOCKED_CODE, OutputEvidence, PiiRedactor, PiiType,
    Severity, WhenNothingRetrieved,
};

#[cfg(feature = "guardrails")]
pub use adk_guardrail::SchemaValidator;

/// Placeholder type when guardrails feature is disabled
#[cfg(not(feature = "guardrails"))]
pub struct GuardrailSet;

#[cfg(not(feature = "guardrails"))]
impl GuardrailSet {
    /// Create an empty guardrail set (no-op when feature is disabled).
    pub fn new() -> Self {
        Self
    }
    /// Returns `true` (always empty when feature is disabled).
    pub fn is_empty(&self) -> bool {
        true
    }
}

#[cfg(not(feature = "guardrails"))]
impl Default for GuardrailSet {
    fn default() -> Self {
        Self::new()
    }
}

/// What a guardrail set is screening.
#[derive(Debug, Clone, Copy)]
pub(crate) enum Phase {
    /// A user message.
    Input,
    /// What the agent produces.
    Output,
}

#[cfg(feature = "guardrails")]
impl From<Phase> for GuardrailPhase {
    fn from(phase: Phase) -> Self {
        match phase {
            Phase::Input => Self::Input,
            Phase::Output => Self::Output,
        }
    }
}

/// What an agent knows about a final answer's grounding: the sources its run retrieved and
/// the answer's citations.
#[derive(Clone, Copy)]
#[cfg_attr(
    not(feature = "guardrails"),
    expect(dead_code, reason = "only output guardrails read the evidence")
)]
pub(crate) struct Grounding<'a> {
    pub(crate) retrieved: &'a [GroundingSource],
    pub(crate) citations: Option<&'a CitationMetadata>,
}

/// Content a guardrail set let through, with the low-severity failures it passed.
pub(crate) struct Screened {
    pub(crate) content: Content,
    pub(crate) warnings: Warnings,
}

impl Screened {
    pub(crate) fn unscreened(content: Content) -> Self {
        Self { content, warnings: Warnings::default() }
    }
}

/// The low-severity failures of the output guardrails that screened one event.
#[derive(Default)]
pub(crate) struct Warnings {
    #[cfg(feature = "guardrails")]
    failures: Vec<GuardrailFailure>,
}

impl Warnings {
    /// Adds the warnings of another screening of the same event.
    pub(crate) fn extend(&mut self, other: Warnings) {
        #[cfg(feature = "guardrails")]
        self.failures.extend(other.failures);
        #[cfg(not(feature = "guardrails"))]
        let _ = other;
    }

    /// Records the failures on `event`, under `GUARDRAIL_WARNINGS_KEY`, so the client and the
    /// session see what the guardrails flagged but passed.
    pub(crate) fn record_on(&self, event: &mut Event) {
        #[cfg(feature = "guardrails")]
        if !self.failures.is_empty() {
            event.provider_metadata.insert(
                GUARDRAIL_WARNINGS_KEY.to_string(),
                serde_json::to_string(&self.failures).unwrap_or_default(),
            );
        }
        #[cfg(not(feature = "guardrails"))]
        let _ = event;
    }
}

/// Runs `guardrails` over `content`, failing with a structured block (category
/// `InvalidInput`, code `guardrail.input_blocked` or `guardrail.output_blocked`, and the
/// blocking guardrail's name and severity in the error details).
#[cfg(feature = "guardrails")]
pub(crate) async fn enforce_guardrails(
    guardrails: &GuardrailSet,
    content: &Content,
    phase: Phase,
) -> Result<Screened> {
    if guardrails.is_empty() {
        return Ok(Screened::unscreened(content.clone()));
    }
    let screening = GuardrailExecutor::enforce(guardrails, content, phase.into(), false).await?;
    Ok(Screened { content: screening.content, warnings: Warnings { failures: screening.warnings } })
}

/// Runs output `guardrails` over an agent's final answer with `grounding` as the evidence,
/// failing with a structured block as [`enforce_guardrails`] does.
#[cfg(all(feature = "guardrails", feature = "codeact"))]
pub(crate) async fn enforce_final_output(
    guardrails: &GuardrailSet,
    content: &Content,
    grounding: Grounding<'_>,
) -> Result<Screened> {
    if guardrails.is_empty() {
        return Ok(Screened::unscreened(content.clone()));
    }
    let evidence = OutputEvidence::new(grounding.retrieved, grounding.citations);
    let screening = GuardrailExecutor::enforce_output(guardrails, content, &evidence).await?;
    Ok(Screened { content: screening.content, warnings: Warnings { failures: screening.warnings } })
}

#[cfg(all(not(feature = "guardrails"), feature = "codeact"))]
pub(crate) async fn enforce_final_output(
    _guardrails: &GuardrailSet,
    content: &Content,
    _grounding: Grounding<'_>,
) -> Result<Screened> {
    Ok(Screened::unscreened(content.clone()))
}

#[cfg(not(feature = "guardrails"))]
pub(crate) async fn enforce_guardrails(
    _guardrails: &GuardrailSet,
    content: &Content,
    _phase: Phase,
) -> Result<Screened> {
    Ok(Screened::unscreened(content.clone()))
}

/// Where a text shown to output guardrails sits in the model turn.
#[cfg(feature = "guardrails")]
enum Slot {
    /// A text part.
    Text(usize),
    /// A text item of a provider-native assistant message kept for replay.
    Native { part: usize, item: usize },
    /// Any other part of a final turn, shown to the guardrails unchanged.
    Other(usize),
}

/// The text items of a provider-native assistant message that a turn carries for replay, such
/// as an OpenAI Responses `commentary` message written beside a tool call.
#[cfg(feature = "guardrails")]
fn native_message_texts(payload: &serde_json::Value) -> Vec<(usize, String)> {
    if payload.get("type").and_then(serde_json::Value::as_str) != Some("message")
        || payload.get("role").and_then(serde_json::Value::as_str) != Some("assistant")
    {
        return Vec::new();
    }
    payload
        .get("content")
        .and_then(serde_json::Value::as_array)
        .map(|items| {
            items
                .iter()
                .enumerate()
                .filter_map(|(item, entry)| {
                    entry.get("text").and_then(serde_json::Value::as_str).map(|t| (item, t.into()))
                })
                .collect()
        })
        .unwrap_or_default()
}

/// Screens a model turn with output guardrails before it reaches the client or the session.
///
/// A final turn is shown to the guardrails whole and `validate_output` decides, with
/// `grounding` as the evidence. A `preamble` turn —
/// one that also calls tools, or that the provider paused — is screened with
/// `validate_preamble`, and only its text is shown, so its function calls pass untouched. In
/// both, the text of a provider-native assistant message carried for replay is shown as a text
/// part and written back into the message. A block fails the turn; a transform rewrites the
/// text in place.
#[cfg(feature = "guardrails")]
pub(crate) async fn screen_model_output(
    guardrails: &GuardrailSet,
    mut content: Content,
    preamble: bool,
    grounding: Grounding<'_>,
) -> Result<Screened> {
    if guardrails.is_empty() {
        return Ok(Screened::unscreened(content));
    }
    let mut slots = Vec::new();
    let mut view = Vec::new();
    for (index, part) in content.parts.iter().enumerate() {
        let native = match part {
            Part::ServerToolCall { server_tool_call } => native_message_texts(server_tool_call),
            _ => Vec::new(),
        };
        if !native.is_empty() {
            for (item, text) in native {
                slots.push(Slot::Native { part: index, item });
                view.push(Part::Text { text });
            }
        } else if let Part::Text { text } = part {
            slots.push(Slot::Text(index));
            view.push(Part::Text { text: text.clone() });
        } else if !preamble {
            slots.push(Slot::Other(index));
            view.push(part.clone());
        }
    }
    if preamble && view.is_empty() {
        return Ok(Screened::unscreened(content));
    }

    let shown = Content { role: content.role.clone(), parts: view };
    let screening = if preamble {
        GuardrailExecutor::enforce(guardrails, &shown, GuardrailPhase::Output, true).await?
    } else {
        let evidence = OutputEvidence::new(grounding.retrieved, grounding.citations);
        GuardrailExecutor::enforce_output(guardrails, &shown, &evidence).await?
    };
    let mut screened = screening.content.parts;

    if screened.len() == slots.len() {
        for (slot, part) in slots.iter().zip(screened) {
            match *slot {
                Slot::Text(index) | Slot::Other(index) => content.parts[index] = part,
                Slot::Native { part: index, item } => {
                    let text = part.text().unwrap_or_default().to_string();
                    if let Part::ServerToolCall { server_tool_call } = &mut content.parts[index]
                        && let Some(entry) = server_tool_call
                            .get_mut("content")
                            .and_then(|items| items.get_mut(item))
                    {
                        entry["text"] = serde_json::Value::String(text);
                    }
                }
            }
        }
    } else if !preamble {
        // A guardrail that restructured the turn replaces it, as it always has.
        content.parts = screened;
    } else {
        // The guardrail returned a different number of parts: its text takes the first text
        // slot and the other slots are emptied, so no unscreened text survives and the calls
        // are kept.
        let joined: String =
            screened.drain(..).filter_map(|part| part.text().map(str::to_string)).collect();
        let mut first = Some(joined);
        let mut emptied = Vec::new();
        for slot in &slots {
            let text = first.take().unwrap_or_default();
            match *slot {
                Slot::Text(index) => {
                    if text.is_empty() {
                        emptied.push(index);
                    }
                    content.parts[index] = Part::Text { text };
                }
                Slot::Native { part: index, item } => {
                    if let Part::ServerToolCall { server_tool_call } = &mut content.parts[index]
                        && let Some(entry) = server_tool_call
                            .get_mut("content")
                            .and_then(|items| items.get_mut(item))
                    {
                        entry["text"] = serde_json::Value::String(text);
                    }
                }
                Slot::Other(_) => {}
            }
        }
        let mut index = 0;
        content.parts.retain(|_| {
            let keep = !emptied.contains(&index);
            index += 1;
            keep
        });
    }
    Ok(Screened { content, warnings: Warnings { failures: screening.warnings } })
}

#[cfg(not(feature = "guardrails"))]
pub(crate) async fn screen_model_output(
    _guardrails: &GuardrailSet,
    content: Content,
    _preamble: bool,
    _grounding: Grounding<'_>,
) -> Result<Screened> {
    Ok(Screened::unscreened(content))
}

#[cfg(feature = "guardrails")]
pub use adk_guardrail::{
    DeniedArgumentPattern, PathAllowList, ResultTextGuardrail, ToolCallDecision, ToolGuardrail,
    ToolGuardrailResult, ToolGuardrailSet, ToolResultDecision, ToolResultGuardrailResult,
};

/// Placeholder type when the guardrails feature is disabled.
#[cfg(not(feature = "guardrails"))]
pub struct ToolGuardrailSet;

#[cfg(not(feature = "guardrails"))]
impl ToolGuardrailSet {
    /// Create an empty tool guardrail set (no-op when the feature is disabled).
    pub fn new() -> Self {
        Self
    }
    /// Returns `true` (always empty when the feature is disabled).
    pub fn is_empty(&self) -> bool {
        true
    }
}

#[cfg(not(feature = "guardrails"))]
impl Default for ToolGuardrailSet {
    fn default() -> Self {
        Self::new()
    }
}

/// Outcome of screening a tool call, independent of whether guardrails are compiled in.
pub(crate) enum ToolScreening {
    /// The call may proceed with these arguments.
    Allow(serde_json::Value),
    /// The call is refused, with a reason to report back to the model.
    ///
    /// Never constructed without the `guardrails` feature, where screening is a no-op that always
    /// allows. The variant still exists so the call site is identical in both builds.
    #[cfg_attr(not(feature = "guardrails"), allow(dead_code))]
    Deny(String),
}

/// Screens a tool call against `guardrails` before it executes.
#[cfg(feature = "guardrails")]
pub(crate) async fn screen_tool_call(
    guardrails: &ToolGuardrailSet,
    tool_name: &str,
    args: &serde_json::Value,
) -> ToolScreening {
    if guardrails.is_empty() {
        return ToolScreening::Allow(args.clone());
    }

    match guardrails.evaluate(tool_name, args).await {
        ToolCallDecision::Allow { args } => ToolScreening::Allow(args),
        ToolCallDecision::Deny { guardrail, reason, severity } => ToolScreening::Deny(format!(
            "Tool '{tool_name}' blocked by guardrail '{guardrail}' ({severity:?}): {reason}"
        )),
    }
}

#[cfg(not(feature = "guardrails"))]
pub(crate) async fn screen_tool_call(
    _guardrails: &ToolGuardrailSet,
    _tool_name: &str,
    args: &serde_json::Value,
) -> ToolScreening {
    ToolScreening::Allow(args.clone())
}

/// Presents a tool guardrail set to the governed execution path as its screen.
pub(crate) struct GuardrailScreen<'a>(pub(crate) &'a ToolGuardrailSet);

#[async_trait::async_trait]
impl adk_core::ToolCallScreen for GuardrailScreen<'_> {
    async fn screen(
        &self,
        tool_name: &str,
        args: &serde_json::Value,
    ) -> std::result::Result<serde_json::Value, String> {
        match screen_tool_call(self.0, tool_name, args).await {
            ToolScreening::Allow(args) => Ok(args),
            ToolScreening::Deny(reason) => Err(reason),
        }
    }
}

/// Screens the function responses of one call's result with the agent's tool guardrails
/// (`ToolGuardrail::validate_result`) before the model and the session see them.
///
/// A withheld result is replaced by `adk_core::result_blocked_response`, and its inline and
/// file data are dropped with it; its status follows `ToolResultStatus::when_withheld`, given
/// whether the tool `executed`. Returns the screened content and the reason for each withheld
/// response, which the caller logs under the payload rule.
#[cfg(feature = "guardrails")]
pub(crate) async fn screen_tool_result(
    guardrails: &ToolGuardrailSet,
    tool_name: &str,
    args: &serde_json::Value,
    executed: bool,
    mut content: Content,
) -> (Content, Vec<String>) {
    use adk_core::ToolResultScreen as _;

    let mut withheld = Vec::new();
    if guardrails.is_empty() {
        return (content, withheld);
    }
    for part in &mut content.parts {
        let previous = part.tool_result_status();
        let Part::FunctionResponse { function_response, .. } = part else { continue };
        match guardrails.screen_result(tool_name, args, &function_response.response).await {
            Ok(result) => function_response.response = result,
            Err(reason) => {
                function_response.response = adk_core::result_blocked_response(tool_name, &reason);
                function_response.inline_data.clear();
                function_response.file_data.clear();
                withheld.push(reason);
                let status = adk_core::ToolResultStatus::when_withheld(previous, executed);
                // A status that stands keeps the cause behind it.
                if previous != Some(status) {
                    part.set_tool_result_status(status, None);
                }
            }
        }
    }
    (content, withheld)
}

#[cfg(not(feature = "guardrails"))]
pub(crate) async fn screen_tool_result(
    _guardrails: &ToolGuardrailSet,
    _tool_name: &str,
    _args: &serde_json::Value,
    _executed: bool,
    content: Content,
) -> (Content, Vec<String>) {
    (content, Vec::new())
}