From 0f0f2272083c542247b5c589de6e4ad2bbc45242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cipriani=20Bandarra?= Date: Fri, 30 Jan 2026 19:38:28 +0000 Subject: [PATCH] Return OllamaResult from Message::tool_response instead of panicking Replaced unwrap() with ? operator so serialization errors propagate as OllamaError::ResponseParseError instead of panicking. This is safer for a library API where callers should decide how to handle errors. --- examples/tool_call.rs | 2 +- src/types/chat.rs | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/tool_call.rs b/examples/tool_call.rs index ffe757e..ef3c106 100644 --- a/examples/tool_call.rs +++ b/examples/tool_call.rs @@ -74,7 +74,7 @@ async fn main() -> Result<(), Box> { let tool_call = &response.message.tool_calls[0]; let arg: GetWeatherArgs = serde_json::from_value(tool_call.function.arguments.clone())?; let result = get_weather(&arg.city); - messages.push(Message::tool_response(&result)); + messages.push(Message::tool_response(&result)?); } Ok(()) } diff --git a/src/types/chat.rs b/src/types/chat.rs index 1f34732..0aade37 100644 --- a/src/types/chat.rs +++ b/src/types/chat.rs @@ -1,6 +1,7 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; +use crate::error::OllamaResult; use crate::types::common::Options; #[derive(Clone, Debug, Serialize, Deserialize)] @@ -38,12 +39,12 @@ impl Message { } } - pub fn tool_response(content: &Value) -> Self { - Message { - content: serde_json::to_string(content).unwrap(), + pub fn tool_response(content: &Value) -> OllamaResult { + Ok(Message { + content: serde_json::to_string(content)?, role: Role::Tool, tool_calls: vec![], - } + }) } }