From f4f275f2f9963cfd44c1ce45c0955f8cc5ca66e2 Mon Sep 17 00:00:00 2001 From: Andre Cipriani Bandarra Date: Sat, 17 Oct 2026 14:49:53 +0100 Subject: [PATCH] feat: support multimodal function response parts Adds an optional parts field to FunctionResponseData, serialized as functionResponse.parts with inlineData, so function responses can carry images. It is omitted when empty, so existing requests are unchanged. --- src/types/common.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/types/common.rs b/src/types/common.rs index 3c08ff5..6b2c17d 100644 --- a/src/types/common.rs +++ b/src/types/common.rs @@ -151,6 +151,7 @@ impl Part { function_response: Some(FunctionResponseData { name: name.into(), response, + parts: vec![], }), ..Default::default() } @@ -167,6 +168,16 @@ pub struct FunctionCallData { pub struct FunctionResponseData { pub name: String, pub response: Value, + /// Media returned by the function (multimodal function responses), sent as + /// `functionResponse.parts`. Omitted when empty. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub parts: Vec, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct FunctionResponsePart { + pub inline_data: InlineData, } #[derive(Clone, Debug, Serialize, Deserialize)] @@ -182,3 +193,46 @@ pub struct FileData { pub mime_type: String, pub file_uri: String, } + +#[cfg(test)] +mod tests { + use serde_json::json; + + use super::{FunctionResponsePart, InlineData, Part}; + + #[test] + fn function_response_omits_empty_parts() { + let part = Part::function_response("lookup", json!({"output": "ok"})); + assert_eq!( + serde_json::to_value(&part).unwrap(), + json!({"functionResponse": {"name": "lookup", "response": {"output": "ok"}}}) + ); + } + + #[test] + fn function_response_serializes_inline_data_parts() { + let mut part = Part::function_response("screenshot", json!({})); + part.function_response.as_mut().unwrap().parts = vec![FunctionResponsePart { + inline_data: InlineData { + mime_type: "image/png".to_string(), + data: "aGVsbG8=".to_string(), + }, + }]; + assert_eq!( + serde_json::to_value(&part).unwrap(), + json!({"functionResponse": { + "name": "screenshot", + "response": {}, + "parts": [{"inlineData": {"mimeType": "image/png", "data": "aGVsbG8="}}] + }}) + ); + } + + #[test] + fn function_response_without_parts_deserializes() { + let part: Part = + serde_json::from_value(json!({"functionResponse": {"name": "lookup", "response": {}}})) + .unwrap(); + assert!(part.function_response.unwrap().parts.is_empty()); + } +}