feat: support parametersJsonSchema on function declarations #2

Merged
andreban merged 1 commits from feat/parameters-json-schema into main 2026-10-18 08:54:33 +01:00
2 changed files with 38 additions and 3 deletions
Showing only changes of commit 43cf7933fc - Show all commits

View File

@@ -45,7 +45,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
function_declarations: Some(vec![FunctionDeclaration { function_declarations: Some(vec![FunctionDeclaration {
name: "take_snapshot".to_string(), name: "take_snapshot".to_string(),
description: "Takes a snapshot of the camera and returns it as an image.".to_string(), description: "Takes a snapshot of the camera and returns it as an image.".to_string(),
parameters: json!({"type": "object", "properties": {}}), parameters: Some(json!({"type": "object", "properties": {}})),
parameters_json_schema: None,
}]), }]),
..Default::default() ..Default::default()
}]; }];

View File

@@ -328,7 +328,14 @@ pub struct UsageMetadata {
pub struct FunctionDeclaration { pub struct FunctionDeclaration {
pub name: String, pub name: String,
pub description: String, pub description: String,
pub parameters: Value, /// Parameters in Vertex's OpenAPI schema subset. Don't set both this and
/// `parameters_json_schema`: Vertex rejects the request.
#[serde(skip_serializing_if = "Option::is_none")]
pub parameters: Option<Value>,
/// Parameters as a JSON Schema, which accepts keywords the OpenAPI subset doesn't
/// (`$schema`, `$ref`/`$defs`, `const`, type arrays, ...).
#[serde(skip_serializing_if = "Option::is_none")]
pub parameters_json_schema: Option<Value>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@@ -370,7 +377,34 @@ impl GenerateContentResponse {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{GenerateContentResponse, GenerateContentResponseResult}; use serde_json::json;
use super::{FunctionDeclaration, GenerateContentResponse, GenerateContentResponseResult};
#[test]
fn serializes_function_declaration_with_only_the_set_schema_field() {
let schema = json!({"type": "object", "properties": {"q": {"type": "string"}}});
let json_schema = FunctionDeclaration {
name: "probe".to_string(),
description: "Probe".to_string(),
parameters: None,
parameters_json_schema: Some(schema.clone()),
};
assert_eq!(
serde_json::to_value(&json_schema).unwrap(),
json!({"name": "probe", "description": "Probe", "parametersJsonSchema": schema})
);
let openapi = FunctionDeclaration {
parameters: Some(schema.clone()),
parameters_json_schema: None,
..json_schema
};
assert_eq!(
serde_json::to_value(&openapi).unwrap(),
json!({"name": "probe", "description": "Probe", "parameters": schema})
);
}
#[test] #[test]
pub fn parses_empty_metadata_response() { pub fn parses_empty_metadata_response() {