Skip to main content

parse_declared_tool_calls

Function parse_declared_tool_calls 

Source
pub fn parse_declared_tool_calls(
    text: &str,
    declared_tools: &HashSet<String>,
) -> Option<Vec<Part>>
Available on crate feature models only.
Expand description

Parse text-based tool calls, accepting them only when they call declared tools.

Returns None — the caller keeps the text as text — when declared_tools is empty, when no tool call is detected, or when any parsed call names a tool outside declared_tools. This keeps ordinary output such as a JSON example with a "name" key from being turned into a tool call.

§Example

use std::collections::HashSet;

use adk_core::Part;
use adk_model::tool_call_parser::parse_declared_tool_calls;

let declared = HashSet::from(["lookup".to_string()]);
let example = "```json\n{\"name\": \"Alice\", \"age\": 30}\n```";
assert!(parse_declared_tool_calls(example, &declared).is_none());

let call = "<tool_call>{\"name\": \"lookup\", \"arguments\": {\"id\": 7}}</tool_call>";
let parts = parse_declared_tool_calls(call, &declared).unwrap();
assert!(matches!(&parts[0], Part::FunctionCall { name, .. } if name == "lookup"));