pub trait HostFunction: Send + Sync {
// Required methods
fn name(&self) -> &str;
fn description(&self) -> &str;
fn call<'life0, 'async_trait>(
&'life0 self,
args: Vec<Value>,
kwargs: Map<String, Value>,
) -> Pin<Box<dyn Future<Output = Result<Value, HostFunctionError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait;
// Provided method
fn signature(&self) -> String { ... }
}Available on crate features
code and embedded-python only.Expand description
A Rust function callable from Python scripts executed by a Monty executor.
Register implementations with
MontyExecutorBuilder::function,
or use
MontyExecutorBuilder::function_fn
for the closure-based common case.
§Example
use adk_code::{HostFunction, HostFunctionError};
use async_trait::async_trait;
use serde_json::{Map, Value, json};
struct GetWeather;
#[async_trait]
impl HostFunction for GetWeather {
fn name(&self) -> &str {
"get_weather"
}
fn description(&self) -> &str {
"Current weather for a city."
}
fn signature(&self) -> String {
"get_weather(city: str) -> dict".to_string()
}
async fn call(
&self,
args: Vec<Value>,
_kwargs: Map<String, Value>,
) -> Result<Value, HostFunctionError> {
let city = args
.first()
.and_then(Value::as_str)
.ok_or_else(|| HostFunctionError::new("pass the city as the first argument"))?;
Ok(json!({ "city": city, "temp_c": 21 }))
}
}Required Methods§
Sourcefn description(&self) -> &str
fn description(&self) -> &str
One-line description — becomes the Python function’s docstring and is surfaced to the LLM through the executor’s prompt snippet.
Sourcefn call<'life0, 'async_trait>(
&'life0 self,
args: Vec<Value>,
kwargs: Map<String, Value>,
) -> Pin<Box<dyn Future<Output = Result<Value, HostFunctionError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn call<'life0, 'async_trait>(
&'life0 self,
args: Vec<Value>,
kwargs: Map<String, Value>,
) -> Pin<Box<dyn Future<Output = Result<Value, HostFunctionError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Invoke with positional and keyword arguments (JSON-converted).
§Errors
An Err becomes a catchable Python exception carrying the message.
Provided Methods§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".