ai_sdk_core/
error.rs

1use ai_sdk_provider::Error as ProviderError;
2
3/// A convenience alias for `Result` where the error type is [`Error`].
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// The error type for this crate.
7pub type Error = Box<dyn std::error::Error + Send + Sync>;
8
9/// Unified error type for text generation and streaming
10#[derive(thiserror::Error, Debug)]
11pub enum GenerateError {
12    /// Missing model - must call .model() before execute()
13    #[error("Missing model - must call .model() before execute()")]
14    MissingModel,
15
16    /// Missing prompt - must call .prompt() before execute()
17    #[error("Missing prompt - must call .prompt() before execute()")]
18    MissingPrompt,
19
20    /// Provider error
21    #[error("Provider error: {0}")]
22    ProviderError(#[from] ProviderError),
23
24    /// Tool execution error
25    #[error("Tool execution error: {0}")]
26    ToolError(#[from] ToolError),
27
28    /// Maximum steps reached without completion
29    #[error("Maximum steps reached without completion")]
30    MaxStepsReached,
31
32    /// Invalid parameters provided
33    #[error("Invalid parameters: {0}")]
34    InvalidParameters(String),
35
36    /// Stream error
37    #[error("Stream error: {0}")]
38    StreamError(String),
39
40    /// Model error
41    #[error("Model error: {0}")]
42    ModelError(String),
43}
44
45/// Error that can occur during embedding
46#[derive(thiserror::Error, Debug)]
47pub enum EmbedError {
48    /// Missing model - must call .model() before execute()
49    #[error("Missing model - must call .model() before execute()")]
50    MissingModel,
51
52    /// Missing value - must call .value() before execute()
53    #[error("Missing value - must call .value() before execute()")]
54    MissingValue,
55
56    /// Empty response from embedding model
57    #[error("Empty response from embedding model")]
58    EmptyResponse,
59
60    /// Provider error
61    #[error("Provider error: {0}")]
62    ProviderError(#[from] ProviderError),
63}
64
65/// Error that can occur during tool execution
66#[derive(thiserror::Error, Debug)]
67pub enum ToolError {
68    /// Tool execution failed
69    #[error("Tool execution failed: {0}")]
70    ExecutionError(String),
71
72    /// Tool not found
73    #[error("Tool not found: {0}")]
74    ToolNotFound(String),
75
76    /// Invalid tool input
77    #[error("Invalid tool input: {0}")]
78    InvalidInput(String),
79
80    /// Tool execution denied
81    #[error("Tool execution denied")]
82    ExecutionDenied,
83}
84
85impl ToolError {
86    /// Create an execution error
87    pub fn execution(msg: impl Into<String>) -> Self {
88        ToolError::ExecutionError(msg.into())
89    }
90
91    /// Create a not found error
92    pub fn not_found(name: impl Into<String>) -> Self {
93        ToolError::ToolNotFound(name.into())
94    }
95
96    /// Create an invalid input error
97    pub fn invalid_input(msg: impl Into<String>) -> Self {
98        ToolError::InvalidInput(msg.into())
99    }
100}
101
102/// Errors that can occur during object generation.
103#[derive(Debug, thiserror::Error)]
104pub enum GenerateObjectError {
105    /// Missing model
106    #[error("Model is required")]
107    MissingModel,
108
109    /// Missing prompt
110    #[error("Prompt is required")]
111    MissingPrompt,
112
113    /// Missing output strategy
114    #[error("Output strategy is required")]
115    MissingOutputStrategy,
116
117    /// Provider error
118    #[error("Provider error: {0}")]
119    ProviderError(#[from] ProviderError),
120
121    /// Validation error
122    #[error("Validation failed: {0}")]
123    ValidationFailed(String),
124
125    /// JSON parsing error
126    #[error("JSON parsing error: {0}")]
127    JsonError(#[from] serde_json::Error),
128
129    /// No text content in response
130    #[error("No text content in model response")]
131    NoTextContent,
132
133    /// Model error
134    #[error("Model error: {0}")]
135    ModelError(Box<dyn std::error::Error + Send + Sync>),
136}
137
138/// Errors that can occur during streaming object generation.
139#[derive(Debug, thiserror::Error)]
140pub enum StreamObjectError {
141    /// Missing model
142    #[error("Model is required")]
143    MissingModel,
144
145    /// Missing prompt
146    #[error("Prompt is required")]
147    MissingPrompt,
148
149    /// Missing output strategy
150    #[error("Output strategy is required")]
151    MissingOutputStrategy,
152
153    /// Provider error
154    #[error("Provider error: {0}")]
155    ProviderError(#[from] ProviderError),
156
157    /// Validation error
158    #[error("Validation failed: {0}")]
159    ValidationFailed(String),
160
161    /// Stream error
162    #[error("Stream error: {0}")]
163    StreamError(String),
164
165    /// Model error
166    #[error("Model error: {0}")]
167    ModelError(Box<dyn std::error::Error + Send + Sync>),
168}
169
170/// Registry-specific errors
171#[derive(Debug, thiserror::Error)]
172pub enum RegistryError {
173    /// Provider not found in registry
174    #[error("No such provider: {provider_id} (available providers: {available_providers:?})")]
175    NoSuchProvider {
176        /// The provider ID that was not found
177        provider_id: String,
178        /// The model type being requested
179        model_type: String,
180        /// List of available provider IDs
181        available_providers: Vec<String>,
182    },
183
184    /// Model not found in provider
185    #[error("No such model: {model_id} (type: {model_type})")]
186    NoSuchModel {
187        /// The model ID that was not found
188        model_id: String,
189        /// The model type being requested
190        model_type: String,
191    },
192
193    /// Invalid model ID format
194    #[error("Invalid model ID: {message}")]
195    InvalidModelId {
196        /// The invalid model ID
197        model_id: String,
198        /// The model type being requested
199        model_type: String,
200        /// Error message explaining what's wrong
201        message: String,
202    },
203}