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() {