aether-llm 0.9.9

Multi-provider LLM abstraction layer for the Aether AI agent framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use std::fmt;

use thiserror::Error;

#[doc = include_str!("docs/llm_error.md")]
#[derive(Debug, Error, Clone)]
pub enum LlmError {
    #[error(transparent)]
    ReasoningValidation(#[from] crate::catalog::ReasoningEffortError),
    #[error("Disabling reasoning is not implemented for model '{model}' on this transport")]
    UnsupportedDisableTransport { model: String },
    /// Environment variable not set or invalid
    #[error("{0} environment variable not set")]
    MissingApiKey(String),
    /// HTTP client creation failed
    #[error("Failed to create HTTP client: {0}")]
    HttpClientCreation(String),
    /// Normalized provider-side failure carrying retry classification and
    /// support diagnostics (HTTP status, request ID, provider error code).
    #[error("{0}")]
    Provider(#[from] ProviderError),
    /// IO error while reading stream
    #[error("IO error reading stream: {0}")]
    IoError(String),
    /// JSON parsing/serialization error
    #[error("JSON parsing error: {0}")]
    JsonParsing(String),
    /// Tool parameter parsing error
    #[error("Failed to parse tool parameters for {tool_name}: {error}")]
    ToolParameterParsing { tool_name: String, error: String },
    /// OAuth authentication error
    #[error("OAuth error: {0}")]
    OAuthError(String),
    /// The message contained only content types this provider doesn't support
    #[error("Unsupported content: {0}")]
    UnsupportedContent(String),
    /// Provider endpoint URL has not been configured.
    #[error("Provider '{provider}' requires a URL configured via providers.{provider}.url")]
    MissingProviderUrl { provider: String },
    /// Provider name is not registered with the model parser.
    #[error("Unknown provider: {provider}")]
    UnknownProvider { provider: String },
    /// The model spec did not yield any usable provider.
    #[error("No models provided")]
    EmptyModelSpec,
    /// A single-model-only config field was reused across multiple models of the
    /// same provider within one alloy spec (e.g. bedrock `inferenceProfileArn`
    /// or openai-compatible `requestModel`).
    #[error("providers.{provider}.{field} cannot be used with multiple {provider} models in one alloy spec")]
    DuplicateProvider { provider: String, field: String },
    /// A `provider:model` identity could not be parsed.
    #[error("Invalid model spec: {0}")]
    InvalidModelSpec(String),
    /// A provider request body could not be constructed (e.g. an SDK builder
    /// rejected the input or contained malformed data).
    #[error("Failed to build provider request: {0}")]
    ProviderRequest(String),
    /// An upstream client library rejected an argument as invalid.
    #[error("Invalid argument: {0}")]
    InvalidArgument(String),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProviderErrorKind {
    Authentication,
    Api,
    RateLimit,
    Server,
    Timeout,
    Network,
    StreamInterrupted,
    Unknown,
}

impl ProviderErrorKind {
    pub fn is_retryable(&self) -> bool {
        matches!(
            self,
            Self::RateLimit | Self::Server | Self::Timeout | Self::Network | Self::StreamInterrupted | Self::Unknown
        )
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProviderError {
    pub kind: ProviderErrorKind,
    pub message: String,
    pub http_status: Option<u16>,
    pub request_id: Option<String>,
    pub code: Option<String>,
}

impl ProviderError {
    pub fn new(kind: ProviderErrorKind, message: impl Into<String>) -> Self {
        Self { kind, message: message.into(), http_status: None, request_id: None, code: None }
    }

    pub fn authentication(message: impl Into<String>) -> Self {
        Self::new(ProviderErrorKind::Authentication, message)
    }

    pub fn api(message: impl Into<String>) -> Self {
        Self::new(ProviderErrorKind::Api, message)
    }

    pub fn rate_limit(message: impl Into<String>) -> Self {
        Self::new(ProviderErrorKind::RateLimit, message)
    }

    pub fn server(message: impl Into<String>) -> Self {
        Self::new(ProviderErrorKind::Server, message)
    }

    pub fn timeout(message: impl Into<String>) -> Self {
        Self::new(ProviderErrorKind::Timeout, message)
    }

    pub fn network(message: impl Into<String>) -> Self {
        Self::new(ProviderErrorKind::Network, message)
    }

    pub fn stream_interrupted(message: impl Into<String>) -> Self {
        Self::new(ProviderErrorKind::StreamInterrupted, message)
    }

    pub fn from_http_status(status: u16, message: impl Into<String>) -> Self {
        match status {
            401 | 403 => Self::authentication(message),
            408 | 504 => Self::timeout(message),
            429 => Self::rate_limit(message),
            s if (500..600).contains(&s) => Self::server(message),
            _ => Self::api(message),
        }
        .with_http_status(status)
    }

    pub fn with_http_status(mut self, status: u16) -> Self {
        self.http_status = Some(status);
        self
    }

    pub fn with_http_metadata(mut self, status: Option<u16>, request_id: Option<String>) -> Self {
        self.http_status = status;
        self.request_id = request_id;
        self
    }

    pub fn with_code(mut self, code: Option<String>) -> Self {
        self.code = code;
        self
    }

    pub fn with_request_id(mut self, request_id: Option<String>) -> Self {
        self.request_id = request_id;
        self
    }

    pub fn is_retryable(&self) -> bool {
        self.kind.is_retryable()
    }
}

impl fmt::Display for ProviderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let prefix = match self.kind {
            ProviderErrorKind::Authentication => "Authentication error",
            ProviderErrorKind::Api => "API error",
            ProviderErrorKind::RateLimit => "Rate limited",
            ProviderErrorKind::Server => "Server error",
            ProviderErrorKind::Timeout => "Request timed out",
            ProviderErrorKind::Network => "Network error",
            ProviderErrorKind::StreamInterrupted => "Stream interrupted",
            ProviderErrorKind::Unknown => "Provider error",
        };
        write!(f, "{prefix}: {}", self.message)?;
        let mut diagnostics = Vec::new();
        if let Some(status) = self.http_status {
            diagnostics.push(format!("status {status}"));
        }
        if let Some(code) = &self.code {
            diagnostics.push(format!("code {code}"));
        }
        if let Some(request_id) = &self.request_id {
            diagnostics.push(format!("request_id {request_id}"));
        }
        if !diagnostics.is_empty() {
            write!(f, " ({})", diagnostics.join(", "))?;
        }
        Ok(())
    }
}

impl std::error::Error for ProviderError {}

impl LlmError {
    pub fn is_retryable(&self) -> bool {
        self.provider().is_some_and(ProviderError::is_retryable)
    }

    pub fn provider(&self) -> Option<&ProviderError> {
        match self {
            Self::Provider(error) => Some(error),
            _ => None,
        }
    }
}

impl From<reqwest::Error> for LlmError {
    fn from(error: reqwest::Error) -> Self {
        if error.is_timeout() {
            return ProviderError::timeout(error.to_string()).into();
        }
        if error.is_connect() || error.is_request() {
            return ProviderError::network(error.to_string()).into();
        }
        match error.status().map(|s| s.as_u16()) {
            Some(status) => ProviderError::from_http_status(status, error.to_string()).into(),
            None => ProviderError::network(error.to_string()).into(),
        }
    }
}

impl From<serde_json::Error> for LlmError {
    fn from(error: serde_json::Error) -> Self {
        LlmError::JsonParsing(error.to_string())
    }
}

impl From<std::io::Error> for LlmError {
    fn from(error: std::io::Error) -> Self {
        LlmError::IoError(error.to_string())
    }
}

impl From<reqwest::header::InvalidHeaderValue> for LlmError {
    fn from(error: reqwest::header::InvalidHeaderValue) -> Self {
        LlmError::ProviderRequest(error.to_string())
    }
}

impl From<async_openai::error::OpenAIError> for LlmError {
    fn from(error: async_openai::error::OpenAIError) -> Self {
        use async_openai::error::OpenAIError;
        match error {
            OpenAIError::Reqwest(e) => LlmError::from(e),
            OpenAIError::StreamError(e) => ProviderError::stream_interrupted(e.to_string()).into(),
            OpenAIError::ApiError(api_err) => {
                let status = api_err.status_code.as_u16();
                let code = api_err.api_error.code.clone();
                let message = format!("{status} {}", api_err.api_error);
                ProviderError::from_http_status(status, message).with_code(code).into()
            }
            OpenAIError::JSONDeserialize(e, _) => LlmError::JsonParsing(e.to_string()),
            OpenAIError::Boxed(e) => e
                .downcast::<ProviderError>()
                .map_or_else(|error| ProviderError::api(error.to_string()).into(), |error| (*error).into()),
            OpenAIError::FileSaveError(s) | OpenAIError::FileReadError(s) => LlmError::IoError(s),
            OpenAIError::InvalidArgument(s) => LlmError::InvalidArgument(s),
        }
    }
}

#[cfg(feature = "codex")]
impl From<aether_auth::OAuthError> for LlmError {
    fn from(error: aether_auth::OAuthError) -> Self {
        LlmError::OAuthError(error.to_string())
    }
}

pub type Result<T> = std::result::Result<T, LlmError>;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn retryable_kinds_cover_transient_failures() {
        assert!(!ProviderErrorKind::Authentication.is_retryable());
        assert!(!ProviderErrorKind::Api.is_retryable());
        assert!(ProviderErrorKind::RateLimit.is_retryable());
        assert!(ProviderErrorKind::Server.is_retryable());
        assert!(ProviderErrorKind::Timeout.is_retryable());
        assert!(ProviderErrorKind::Network.is_retryable());
        assert!(ProviderErrorKind::StreamInterrupted.is_retryable());
        assert!(ProviderErrorKind::Unknown.is_retryable());
    }

    #[test]
    fn http_status_classification_covers_known_statuses() {
        assert_eq!(ProviderError::from_http_status(401, "d").kind, ProviderErrorKind::Authentication);
        assert_eq!(ProviderError::from_http_status(403, "d").kind, ProviderErrorKind::Authentication);
        assert_eq!(ProviderError::from_http_status(408, "d").kind, ProviderErrorKind::Timeout);
        assert!(ProviderError::from_http_status(408, "d").is_retryable());
        assert_eq!(ProviderError::from_http_status(429, "d").kind, ProviderErrorKind::RateLimit);
        assert_eq!(ProviderError::from_http_status(500, "d").kind, ProviderErrorKind::Server);
        assert_eq!(ProviderError::from_http_status(503, "d").kind, ProviderErrorKind::Server);
        assert_eq!(ProviderError::from_http_status(400, "d").kind, ProviderErrorKind::Api);
        assert!(!ProviderError::from_http_status(400, "d").is_retryable());
        assert_eq!(ProviderError::from_http_status(404, "d").kind, ProviderErrorKind::Api);
    }

    #[test]
    fn display_includes_diagnostics_when_present() {
        let error = ProviderError::server("boom").with_http_status(200).with_code(Some("server_error".into()));
        let text = error.to_string();
        assert!(text.contains("boom"));
        assert!(text.contains("status 200"));
        assert!(text.contains("code server_error"));
        let error = error.with_request_id(Some("req-1".into()));
        assert!(error.to_string().contains("request_id req-1"));
    }

    #[test]
    fn display_omits_suffix_when_no_metadata() {
        let error = ProviderError::api("bad");
        assert_eq!(error.to_string(), "API error: bad");
    }

    #[test]
    fn is_retryable() {
        assert!(LlmError::from(ProviderError::rate_limit("rl")).is_retryable());
        assert!(LlmError::from(ProviderError::server("x").with_http_status(503)).is_retryable());
        assert!(LlmError::from(ProviderError::server("stream-level")).is_retryable());
        assert!(LlmError::from(ProviderError::timeout("t")).is_retryable());
        assert!(LlmError::from(ProviderError::network("n")).is_retryable());
        assert!(LlmError::from(ProviderError::stream_interrupted("s")).is_retryable());

        assert!(!LlmError::from(ProviderError::api("x")).is_retryable());
        assert!(!LlmError::from(ProviderError::authentication("x")).is_retryable());
        assert!(!LlmError::MissingApiKey("x".into()).is_retryable());
        assert!(!LlmError::HttpClientCreation("x".into()).is_retryable());
        assert!(!LlmError::IoError("x".into()).is_retryable());
        assert!(!LlmError::JsonParsing("x".into()).is_retryable());
        assert!(!LlmError::ToolParameterParsing { tool_name: "t".into(), error: "e".into() }.is_retryable());
        assert!(!LlmError::OAuthError("x".into()).is_retryable());
        assert!(!LlmError::UnsupportedContent("x".into()).is_retryable());
        assert!(!LlmError::MissingProviderUrl { provider: "azure-foundry".into() }.is_retryable());
        assert!(!LlmError::UnknownProvider { provider: "foo".into() }.is_retryable());
        assert!(!LlmError::EmptyModelSpec.is_retryable());
        assert!(
            !LlmError::DuplicateProvider { provider: "bedrock".into(), field: "inferenceProfileArn".into() }
                .is_retryable()
        );
        assert!(!LlmError::InvalidModelSpec("x".into()).is_retryable());
        assert!(!LlmError::ProviderRequest("x".into()).is_retryable());
        assert!(!LlmError::InvalidArgument("x".into()).is_retryable());
    }

    #[test]
    fn async_openai_api_error_preserves_status_and_code() {
        use async_openai::error::{ApiError, ApiErrorResponse};
        let response = ApiErrorResponse {
            status_code: reqwest::StatusCode::SERVICE_UNAVAILABLE,
            api_error: ApiError {
                message: "overloaded".to_string(),
                r#type: None,
                param: None,
                code: Some("server_error".to_string()),
                misalignment: None,
            },
        };
        let error = LlmError::from(async_openai::error::OpenAIError::ApiError(response));
        let provider = error.provider().expect("expected provider error");
        assert_eq!(provider.kind, ProviderErrorKind::Server);
        assert_eq!(provider.http_status, Some(503));
        assert_eq!(provider.code.as_deref(), Some("server_error"));
        assert!(error.is_retryable());
    }

    #[test]
    fn async_openai_boxed_provider_error_preserves_classification_and_diagnostics() {
        let provider = ProviderError::rate_limit("slow down")
            .with_http_status(429)
            .with_code(Some("429".into()))
            .with_request_id(Some("request-123".into()));
        let error = LlmError::from(async_openai::error::OpenAIError::Boxed(Box::new(provider.clone())));

        assert_eq!(error.provider(), Some(&provider));
        assert!(error.is_retryable());
    }

    #[test]
    fn async_openai_stream_error_is_interruption() {
        let io = std::io::Error::other("eof");
        let error = LlmError::from(async_openai::error::OpenAIError::StreamError(Box::new(
            async_openai::error::StreamError::EventStream(io.to_string()),
        )));
        let provider = error.provider().expect("expected provider error");
        assert_eq!(provider.kind, ProviderErrorKind::StreamInterrupted);
        assert!(error.is_retryable());
    }

    #[test]
    fn async_openai_non_error_body_stays_a_json_parsing_error() {
        let body = r#"{"id":"chatcmpl-1","object":"chat.completion","choices":[]}"#;
        let parse_error = serde_json::from_str::<String>(body).unwrap_err();

        let error = LlmError::from(async_openai::error::OpenAIError::JSONDeserialize(parse_error, body.to_string()));

        assert!(matches!(error, LlmError::JsonParsing(_)), "got {error:?}");
        assert!(!error.is_retryable());
    }
}