Skip to main content

ollama_rs/
error.rs

1use serde::Deserialize;
2use static_assertions::assert_impl_all;
3use thiserror::Error;
4
5assert_impl_all!(OllamaError: Send, Sync);
6/// A result type for operations in the ollama-rs crate.
7///
8/// This type is used throughout the crate to represent the result of an operation,
9/// which may be successful or result in an `OllamaError`.
10pub type Result<T> = std::result::Result<T, OllamaError>;
11
12/// An error type for the ollama-rs crate.
13///
14/// This enum represents the various errors that can occur within the crate.
15/// Each variant corresponds to a different kind of error.
16#[derive(Error, Debug)]
17pub enum OllamaError {
18    #[error("Error calling tool")]
19    ToolCallError(#[from] ToolCallError),
20    #[error("Ollama JSON error")]
21    JsonError(#[from] serde_json::Error),
22    #[error("Reqwest error")]
23    ReqwestError(#[from] reqwest::Error),
24    #[error("Internal Ollama error: {}", .0.message)]
25    InternalError(InternalOllamaError),
26    #[error("{0}")]
27    Other(String),
28}
29
30/// Represents an internal error within the Ollama service.
31///
32/// This struct is used to deserialize error messages returned by the service.
33#[derive(Deserialize, Debug)]
34pub struct InternalOllamaError {
35    #[serde(rename = "error")]
36    pub message: String,
37}
38
39/// An error type for tool call operations.
40///
41/// This enum represents errors that can occur when calling tools within the Ollama service.
42/// Each variant corresponds to a different kind of tool call error.
43#[derive(Error, Debug)]
44pub enum ToolCallError {
45    #[error("Ollama attempted to call a tool with a name we do not recognize")]
46    UnknownToolName,
47    #[error(
48        "Could not convert tool arguments from Ollama into what the tool expected, or vice versa"
49    )]
50    InvalidToolArguments(#[from] serde_json::Error),
51    #[error("Tool errored internally when it was called")]
52    InternalToolError(#[from] Box<dyn std::error::Error + Send + Sync>),
53}