From a4766e296f038e09d92f0cc7647d5c6a7d3b20ab Mon Sep 17 00:00:00 2001 From: Agus Zubiaga Date: Wed, 14 May 2025 02:51:31 +0200 Subject: [PATCH] Add tool result image support to Gemini models (#30647) Release Notes: - Add tool result image support to Gemini models --- crates/language_models/src/provider/google.rs | 63 +++++++++++++------ 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/crates/language_models/src/provider/google.rs b/crates/language_models/src/provider/google.rs index 4f3c0cb112..b79fcb2e86 100644 --- a/crates/language_models/src/provider/google.rs +++ b/crates/language_models/src/provider/google.rs @@ -398,43 +398,68 @@ pub fn into_google( fn map_content(content: Vec) -> Vec { content .into_iter() - .filter_map(|content| match content { + .flat_map(|content| match content { language_model::MessageContent::Text(text) | language_model::MessageContent::Thinking { text, .. } => { if !text.is_empty() { - Some(Part::TextPart(google_ai::TextPart { text })) + vec![Part::TextPart(google_ai::TextPart { text })] } else { - None + vec![] } } - language_model::MessageContent::RedactedThinking(_) => None, + language_model::MessageContent::RedactedThinking(_) => vec![], language_model::MessageContent::Image(image) => { - Some(Part::InlineDataPart(google_ai::InlineDataPart { + vec![Part::InlineDataPart(google_ai::InlineDataPart { inline_data: google_ai::GenerativeContentBlob { mime_type: "image/png".to_string(), data: image.source.to_string(), }, - })) + })] } language_model::MessageContent::ToolUse(tool_use) => { - Some(Part::FunctionCallPart(google_ai::FunctionCallPart { + vec![Part::FunctionCallPart(google_ai::FunctionCallPart { function_call: google_ai::FunctionCall { name: tool_use.name.to_string(), args: tool_use.input, }, - })) + })] + } + language_model::MessageContent::ToolResult(tool_result) => { + match tool_result.content { + language_model::LanguageModelToolResultContent::Text(txt) => { + vec![Part::FunctionResponsePart( + google_ai::FunctionResponsePart { + function_response: google_ai::FunctionResponse { + name: tool_result.tool_name.to_string(), + // The API expects a valid JSON object + response: serde_json::json!({ + "output": txt + }), + }, + }, + )] + } + language_model::LanguageModelToolResultContent::Image(image) => { + vec![ + Part::FunctionResponsePart(google_ai::FunctionResponsePart { + function_response: google_ai::FunctionResponse { + name: tool_result.tool_name.to_string(), + // The API expects a valid JSON object + response: serde_json::json!({ + "output": "Tool responded with an image" + }), + }, + }), + Part::InlineDataPart(google_ai::InlineDataPart { + inline_data: google_ai::GenerativeContentBlob { + mime_type: "image/png".to_string(), + data: image.source.to_string(), + }, + }), + ] + } + } } - language_model::MessageContent::ToolResult(tool_result) => Some( - Part::FunctionResponsePart(google_ai::FunctionResponsePart { - function_response: google_ai::FunctionResponse { - name: tool_result.tool_name.to_string(), - // The API expects a valid JSON object - response: serde_json::json!({ - "output": tool_result.content - }), - }, - }), - ), }) .collect() }