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.
This commit is contained in:
2026-01-30 19:38:28 +00:00
parent 51771284a7
commit 0f0f227208
2 changed files with 6 additions and 5 deletions

View File

@@ -74,7 +74,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let tool_call = &response.message.tool_calls[0]; let tool_call = &response.message.tool_calls[0];
let arg: GetWeatherArgs = serde_json::from_value(tool_call.function.arguments.clone())?; let arg: GetWeatherArgs = serde_json::from_value(tool_call.function.arguments.clone())?;
let result = get_weather(&arg.city); let result = get_weather(&arg.city);
messages.push(Message::tool_response(&result)); messages.push(Message::tool_response(&result)?);
} }
Ok(()) Ok(())
} }

View File

@@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
use crate::error::OllamaResult;
use crate::types::common::Options; use crate::types::common::Options;
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
@@ -38,12 +39,12 @@ impl Message {
} }
} }
pub fn tool_response(content: &Value) -> Self { pub fn tool_response(content: &Value) -> OllamaResult<Self> {
Message { Ok(Message {
content: serde_json::to_string(content).unwrap(), content: serde_json::to_string(content)?,
role: Role::Tool, role: Role::Tool,
tool_calls: vec![], tool_calls: vec![],
} })
} }
} }