From 43cf7933fc9db5587c88ae7fb13ea9d1ad065f11 Mon Sep 17 00:00:00 2001 From: Andre Cipriani Bandarra Date: Sun, 18 Oct 2026 08:34:56 +0100 Subject: [PATCH] feat: support parametersJsonSchema on function declarations FunctionDeclaration.parameters becomes Option, and the new parameters_json_schema field sends the schema as JSON Schema. Vertex rejects many JSON Schema keywords in parameters ($schema, $ref/$defs, const, type arrays) but accepts them in parametersJsonSchema. Both fields are skipped when None, since Vertex rejects a declaration that sets both. --- examples/function-response-image.rs | 3 ++- src/types/generate_content.rs | 38 +++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/examples/function-response-image.rs b/examples/function-response-image.rs index 6721f2b..d5b0006 100644 --- a/examples/function-response-image.rs +++ b/examples/function-response-image.rs @@ -45,7 +45,8 @@ async fn main() -> Result<(), Box> { function_declarations: Some(vec![FunctionDeclaration { name: "take_snapshot".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() }]; diff --git a/src/types/generate_content.rs b/src/types/generate_content.rs index 37fe674..218a1fb 100644 --- a/src/types/generate_content.rs +++ b/src/types/generate_content.rs @@ -328,7 +328,14 @@ pub struct UsageMetadata { pub struct FunctionDeclaration { pub name: 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, + /// 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, } #[derive(Debug, Serialize, Deserialize)] @@ -370,7 +377,34 @@ impl GenerateContentResponse { #[cfg(test)] 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] pub fn parses_empty_metadata_response() { -- 2.49.1