litellm-rs 0.4.16

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Helper functions for creating specific error types

use super::types::GatewayError;

/// Helper functions for creating specific errors
impl GatewayError {
    pub fn auth<S: Into<String>>(message: S) -> Self {
        Self::Auth(message.into())
    }

    pub fn authorization<S: Into<String>>(message: S) -> Self {
        Self::Forbidden(message.into())
    }

    pub fn bad_request<S: Into<String>>(message: S) -> Self {
        Self::BadRequest(message.into())
    }

    pub fn not_found<S: Into<String>>(message: S) -> Self {
        Self::NotFound(message.into())
    }

    pub fn conflict<S: Into<String>>(message: S) -> Self {
        Self::Conflict(message.into())
    }

    pub fn internal<S: Into<String>>(message: S) -> Self {
        Self::Internal(message.into())
    }

    pub fn validation<S: Into<String>>(message: S) -> Self {
        Self::Validation(message.into())
    }

    pub fn rate_limit<S: Into<String>>(message: S) -> Self {
        Self::RateLimit {
            message: message.into(),
            retry_after: None,
            rpm_limit: None,
            tpm_limit: None,
        }
    }

    pub fn timeout<S: Into<String>>(message: S) -> Self {
        Self::Timeout(message.into())
    }

    pub fn service_unavailable<S: Into<String>>(message: S) -> Self {
        Self::Unavailable(message.into())
    }

    pub fn server<S: Into<String>>(message: S) -> Self {
        Self::Internal(message.into())
    }

    pub fn network<S: Into<String>>(message: S) -> Self {
        Self::Network(message.into())
    }

    pub fn external_service<S: Into<String>>(message: S) -> Self {
        Self::Internal(message.into())
    }

    pub fn invalid_request<S: Into<String>>(message: S) -> Self {
        Self::BadRequest(message.into())
    }

    pub fn parsing<S: Into<String>>(message: S) -> Self {
        Self::Validation(message.into())
    }

    pub fn alert<S: Into<String>>(message: S) -> Self {
        Self::Internal(message.into())
    }

    pub fn not_implemented<S: Into<String>>(message: S) -> Self {
        Self::NotImplemented(message.into())
    }

    pub fn unauthorized<S: Into<String>>(message: S) -> Self {
        Self::Auth(message.into())
    }

    pub fn forbidden<S: Into<String>>(message: S) -> Self {
        Self::Forbidden(message.into())
    }

    pub fn external<S: Into<String>>(message: S) -> Self {
        Self::Network(message.into())
    }

    pub fn invalid_request_error<S: Into<String>>(message: S) -> Self {
        Self::BadRequest(message.into())
    }

    pub fn no_providers_available<S: Into<String>>(message: S) -> Self {
        Self::Unavailable(message.into())
    }

    pub fn provider_not_found<S: Into<String>>(message: S) -> Self {
        Self::NotFound(message.into())
    }

    pub fn no_providers_for_model<S: Into<String>>(message: S) -> Self {
        Self::BadRequest(message.into())
    }

    pub fn no_healthy_providers<S: Into<String>>(message: S) -> Self {
        Self::Unavailable(message.into())
    }
}

impl GatewayError {
    pub fn api_error<S: Into<String>>(_status_code: u16, message: S, _provider: S) -> Self {
        // ApiError doesn't exist in unified ProviderError, map to Internal in GatewayError
        Self::Internal(message.into())
    }

    pub fn unavailable<S: Into<String>>(message: S) -> Self {
        Self::Unavailable(message.into())
    }
}

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

    // ==================== Auth Error Tests ====================

    #[test]
    fn test_auth_error_from_string() {
        let error = GatewayError::auth("Invalid API key");
        assert!(matches!(error, GatewayError::Auth(msg) if msg == "Invalid API key"));
    }

    #[test]
    fn test_auth_error_from_str() {
        let error = GatewayError::auth("Token expired");
        assert!(matches!(error, GatewayError::Auth(_)));
    }

    #[test]
    fn test_authorization_error() {
        let error = GatewayError::authorization("Insufficient permissions");
        assert!(matches!(error, GatewayError::Forbidden(msg) if msg == "Insufficient permissions"));
    }

    // ==================== Request Error Tests ====================

    #[test]
    fn test_bad_request_error() {
        let error = GatewayError::bad_request("Missing required field");
        assert!(matches!(error, GatewayError::BadRequest(msg) if msg == "Missing required field"));
    }

    #[test]
    fn test_not_found_error() {
        let error = GatewayError::not_found("Resource not found");
        assert!(matches!(error, GatewayError::NotFound(msg) if msg == "Resource not found"));
    }

    #[test]
    fn test_conflict_error() {
        let error = GatewayError::conflict("Resource already exists");
        assert!(matches!(error, GatewayError::Conflict(msg) if msg == "Resource already exists"));
    }

    #[test]
    fn test_validation_error() {
        let error = GatewayError::validation("Invalid input format");
        assert!(matches!(error, GatewayError::Validation(msg) if msg == "Invalid input format"));
    }

    #[test]
    fn test_invalid_request_error() {
        let error = GatewayError::invalid_request("Malformed JSON");
        assert!(matches!(error, GatewayError::BadRequest(msg) if msg == "Malformed JSON"));
    }

    #[test]
    fn test_invalid_request_error_method() {
        let error = GatewayError::invalid_request_error("Invalid format");
        assert!(matches!(error, GatewayError::BadRequest(msg) if msg == "Invalid format"));
    }

    // ==================== Server Error Tests ====================

    #[test]
    fn test_internal_error() {
        let error = GatewayError::internal("Internal server error");
        assert!(matches!(error, GatewayError::Internal(msg) if msg == "Internal server error"));
    }

    #[test]
    fn test_server_error_maps_to_internal() {
        let error = GatewayError::server("Server failure");
        assert!(matches!(error, GatewayError::Internal(msg) if msg == "Server failure"));
    }

    #[test]
    fn test_external_service_maps_to_internal() {
        let error = GatewayError::external_service("External API failed");
        assert!(matches!(error, GatewayError::Internal(msg) if msg == "External API failed"));
    }

    #[test]
    fn test_api_error_maps_to_internal() {
        let error = GatewayError::api_error(500, "Server error", "openai");
        assert!(matches!(error, GatewayError::Internal(msg) if msg == "Server error"));
    }

    // ==================== Rate Limiting Tests ====================

    #[test]
    fn test_rate_limit_error() {
        let error = GatewayError::rate_limit("Rate limit exceeded");
        assert!(
            matches!(error, GatewayError::RateLimit { ref message, .. } if message == "Rate limit exceeded")
        );
    }

    #[test]
    fn test_timeout_error() {
        let error = GatewayError::timeout("Request timed out after 30s");
        assert!(
            matches!(error, GatewayError::Timeout(msg) if msg == "Request timed out after 30s")
        );
    }

    // ==================== Provider Error Tests ====================

    #[test]
    fn test_service_unavailable_error() {
        let error = GatewayError::service_unavailable("Service under maintenance");
        assert!(
            matches!(error, GatewayError::Unavailable(msg) if msg == "Service under maintenance")
        );
    }

    #[test]
    fn test_unavailable_error() {
        let error = GatewayError::unavailable("Provider unavailable");
        assert!(matches!(error, GatewayError::Unavailable(msg) if msg == "Provider unavailable"));
    }

    #[test]
    fn test_no_providers_available() {
        let error = GatewayError::no_providers_available("No providers configured");
        assert!(
            matches!(error, GatewayError::Unavailable(msg) if msg == "No providers configured")
        );
    }

    #[test]
    fn test_provider_not_found() {
        let error = GatewayError::provider_not_found("openai");
        assert!(matches!(error, GatewayError::NotFound(msg) if msg == "openai"));
    }

    #[test]
    fn test_no_providers_for_model() {
        let error = GatewayError::no_providers_for_model("gpt-5");
        assert!(matches!(error, GatewayError::BadRequest(msg) if msg == "gpt-5"));
    }

    #[test]
    fn test_no_healthy_providers() {
        let error = GatewayError::no_healthy_providers("All providers are down");
        assert!(matches!(error, GatewayError::Unavailable(msg) if msg == "All providers are down"));
    }

    // ==================== Network Error Tests ====================

    #[test]
    fn test_network_error() {
        let error = GatewayError::network("Connection refused");
        assert!(matches!(error, GatewayError::Network(msg) if msg == "Connection refused"));
    }

    // ==================== Parsing Error Tests ====================

    #[test]
    fn test_parsing_error() {
        let error = GatewayError::parsing("Invalid JSON syntax");
        assert!(matches!(error, GatewayError::Validation(msg) if msg == "Invalid JSON syntax"));
    }

    // ==================== Alert Error Tests ====================

    #[test]
    fn test_alert_error() {
        let error = GatewayError::alert("Critical threshold exceeded");
        assert!(
            matches!(error, GatewayError::Internal(msg) if msg == "Critical threshold exceeded")
        );
    }

    // ==================== Not Implemented Error Tests ====================

    #[test]
    fn test_not_implemented_error() {
        let error = GatewayError::not_implemented("Feature not yet available");
        assert!(
            matches!(error, GatewayError::NotImplemented(msg) if msg == "Feature not yet available")
        );
    }

    // ==================== Authorization Error Tests ====================

    #[test]
    fn test_unauthorized_error() {
        let error = GatewayError::unauthorized("Invalid credentials");
        assert!(matches!(error, GatewayError::Auth(msg) if msg == "Invalid credentials"));
    }

    #[test]
    fn test_forbidden_error() {
        let error = GatewayError::forbidden("Access denied");
        assert!(matches!(error, GatewayError::Forbidden(msg) if msg == "Access denied"));
    }

    // ==================== External Error Tests ====================

    #[test]
    fn test_external_error() {
        let error = GatewayError::external("Third-party service error");
        assert!(matches!(error, GatewayError::Network(msg) if msg == "Third-party service error"));
    }

    // ==================== String Conversion Tests ====================

    #[test]
    fn test_error_from_owned_string() {
        let message = String::from("Dynamic error message");
        let error = GatewayError::auth(message);
        assert!(matches!(error, GatewayError::Auth(msg) if msg == "Dynamic error message"));
    }

    #[test]
    fn test_error_from_string_slice() {
        let error = GatewayError::auth("Static error message");
        assert!(matches!(error, GatewayError::Auth(msg) if msg == "Static error message"));
    }

    #[test]
    fn test_error_with_format_string() {
        let model = "gpt-4";
        let error = GatewayError::no_providers_for_model(format!("No provider supports {}", model));
        assert!(matches!(error, GatewayError::BadRequest(msg) if msg.contains("gpt-4")));
    }

    // ==================== Edge Case Tests ====================

    #[test]
    fn test_error_with_empty_string() {
        let error = GatewayError::auth("");
        assert!(matches!(error, GatewayError::Auth(msg) if msg.is_empty()));
    }

    #[test]
    fn test_error_with_unicode() {
        let error = GatewayError::auth("认证失败 - Authentication failed");
        assert!(matches!(error, GatewayError::Auth(msg) if msg.contains("认证失败")));
    }

    #[test]
    fn test_error_with_special_characters() {
        let error = GatewayError::parsing("JSON error at line 5: unexpected '}'");
        assert!(matches!(error, GatewayError::Validation(msg) if msg.contains("unexpected '}'")));
    }

    #[test]
    fn test_error_with_newlines() {
        let error = GatewayError::internal("Error details:\n- Issue 1\n- Issue 2");
        assert!(matches!(error, GatewayError::Internal(msg) if msg.contains('\n')));
    }

    // ==================== Consistency Tests ====================

    #[test]
    fn test_service_unavailable_matches_unavailable() {
        let error1 = GatewayError::service_unavailable("test");
        let error2 = GatewayError::unavailable("test");

        // Both should produce ProviderUnavailable
        assert!(matches!(error1, GatewayError::Unavailable(_)));
        assert!(matches!(error2, GatewayError::Unavailable(_)));
    }

    #[test]
    fn test_server_matches_internal() {
        let error1 = GatewayError::server("test");
        let error2 = GatewayError::internal("test");

        // Both should produce Internal
        assert!(matches!(error1, GatewayError::Internal(_)));
        assert!(matches!(error2, GatewayError::Internal(_)));
    }

    #[test]
    fn test_invalid_request_matches_bad_request() {
        let error1 = GatewayError::invalid_request("test");
        let error2 = GatewayError::bad_request("test");

        // Both should produce BadRequest
        assert!(matches!(error1, GatewayError::BadRequest(_)));
        assert!(matches!(error2, GatewayError::BadRequest(_)));
    }

    // ==================== All Helper Methods Coverage ====================

    #[test]
    fn test_all_helper_methods_exist() {
        // This test ensures all helper methods are callable
        let _ = GatewayError::auth("test");
        let _ = GatewayError::authorization("test");
        let _ = GatewayError::bad_request("test");
        let _ = GatewayError::not_found("test");
        let _ = GatewayError::conflict("test");
        let _ = GatewayError::internal("test");
        let _ = GatewayError::validation("test");
        let _ = GatewayError::rate_limit("test");
        let _ = GatewayError::timeout("test");
        let _ = GatewayError::service_unavailable("test");
        let _ = GatewayError::server("test");
        let _ = GatewayError::network("test");
        let _ = GatewayError::external_service("test");
        let _ = GatewayError::invalid_request("test");
        let _ = GatewayError::parsing("test");
        let _ = GatewayError::alert("test");
        let _ = GatewayError::not_implemented("test");
        let _ = GatewayError::unauthorized("test");
        let _ = GatewayError::forbidden("test");
        let _ = GatewayError::external("test");
        let _ = GatewayError::invalid_request_error("test");
        let _ = GatewayError::no_providers_available("test");
        let _ = GatewayError::provider_not_found("test");
        let _ = GatewayError::no_providers_for_model("test");
        let _ = GatewayError::no_healthy_providers("test");
        let _ = GatewayError::api_error(500, "test", "provider");
        let _ = GatewayError::unavailable("test");
    }
}