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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! Module
//!
//! SDK client-side configuration models.
//! Canonical gateway/server runtime config models are under `crate::config::models::*`.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ClientConfig {
    /// Default
    pub default_provider: Option<String>,
    /// Configuration
    pub providers: Vec<SdkProviderConfig>,
    /// Settings
    pub settings: ClientSettings,
}

/// Settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClientSettings {
    /// Request
    pub timeout: u64,
    /// Number of retries
    pub max_retries: u32,
    /// Request
    pub max_concurrent_requests: u32,
    /// Request
    pub enable_logging: bool,
    /// Enable metrics collection
    pub enable_metrics: bool,
}

impl Default for ClientSettings {
    fn default() -> Self {
        Self {
            timeout: 30,
            max_retries: 3,
            max_concurrent_requests: 100,
            enable_logging: true,
            enable_metrics: true,
        }
    }
}

/// SDK client-side provider configuration.
///
/// This is the client-facing provider config used by the SDK.
/// For the gateway/server-side YAML config model, see
/// [`crate::config::models::provider::ProviderConfig`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SdkProviderConfig {
    /// Provider unique ID
    pub id: String,
    /// Provider type
    pub provider_type: ProviderType,
    /// Display name
    pub name: String,
    /// API key
    pub api_key: String,
    /// Base URL (optional)
    pub base_url: Option<String>,
    /// Model
    pub models: Vec<String>,
    /// Enabled status
    pub enabled: bool,
    /// Weight (for load balancing)
    pub weight: f32,
    /// Request
    pub rate_limit_rpm: Option<u32>,
    /// Token limit per minute
    pub rate_limit_tpm: Option<u32>,
    /// Settings
    pub settings: HashMap<String, serde_json::Value>,
}

/// Canonical alias for SDK client configuration.
pub type ClientRuntimeConfig = ClientConfig;
/// Canonical alias for SDK provider configuration.
pub type ClientProviderConfig = SdkProviderConfig;
/// Backward-compatible alias.
pub type ProviderConfig = SdkProviderConfig;

/// Provider type enumeration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProviderType {
    /// OpenAI provider (GPT models)
    OpenAI,
    /// Anthropic provider (Claude models)
    Anthropic,
    /// Azure OpenAI provider
    Azure,
    /// Google provider (PaLM, Gemini models)
    Google,
    /// Cohere provider
    Cohere,
    /// Hugging Face provider
    HuggingFace,
    /// Ollama provider (local models)
    Ollama,
    /// AWS Bedrock provider
    AwsBedrock,
    /// Google Vertex AI provider
    GoogleVertex,
    /// Mistral provider
    Mistral,
    /// Custom provider with specified name
    Custom(String),
}

impl From<&str> for ProviderType {
    fn from(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "openai" => ProviderType::OpenAI,
            "anthropic" => ProviderType::Anthropic,
            "azure" => ProviderType::Azure,
            "google" => ProviderType::Google,
            "cohere" => ProviderType::Cohere,
            "huggingface" => ProviderType::HuggingFace,
            "ollama" => ProviderType::Ollama,
            "aws_bedrock" => ProviderType::AwsBedrock,
            "google_vertex" => ProviderType::GoogleVertex,
            "mistral" => ProviderType::Mistral,
            _ => ProviderType::Custom(s.to_string()),
        }
    }
}

/// Configuration
pub struct SdkConfigBuilder {
    config: ClientConfig,
}

impl SdkConfigBuilder {
    /// Create a new configuration builder
    pub fn new() -> Self {
        Self {
            config: ClientConfig::default(),
        }
    }

    /// Default
    pub fn default_provider(mut self, provider_id: &str) -> Self {
        self.config.default_provider = Some(provider_id.to_string());
        self
    }

    /// Add provider
    pub fn add_provider(mut self, provider: SdkProviderConfig) -> Self {
        self.config.providers.push(provider);
        self
    }

    /// Add OpenAI provider
    pub fn add_openai(self, id: &str, api_key: &str) -> Self {
        self.add_provider(SdkProviderConfig {
            id: id.to_string(),
            provider_type: ProviderType::OpenAI,
            name: "OpenAI".to_string(),
            api_key: api_key.to_string(),
            base_url: None,
            models: vec![
                "gpt-5.2-chat".to_string(),
                "gpt-5.2".to_string(),
                "gpt-5-nano".to_string(),
            ],
            enabled: true,
            weight: 1.0,
            rate_limit_rpm: Some(3000),
            rate_limit_tpm: Some(250000),
            settings: HashMap::new(),
        })
    }

    /// Add Anthropic provider
    pub fn add_anthropic(self, id: &str, api_key: &str) -> Self {
        self.add_provider(SdkProviderConfig {
            id: id.to_string(),
            provider_type: ProviderType::Anthropic,
            name: "Anthropic".to_string(),
            api_key: api_key.to_string(),
            base_url: None,
            models: vec![
                "claude-opus-4-6".to_string(),
                "claude-sonnet-4-5".to_string(),
                "claude-3-5-haiku-20241022".to_string(),
            ],
            enabled: true,
            weight: 1.0,
            rate_limit_rpm: Some(1000),
            rate_limit_tpm: Some(100000),
            settings: HashMap::new(),
        })
    }

    /// Settings
    pub fn timeout(mut self, timeout: u64) -> Self {
        self.config.settings.timeout = timeout;
        self
    }

    /// Settings
    pub fn max_retries(mut self, retries: u32) -> Self {
        self.config.settings.max_retries = retries;
        self
    }

    /// Configuration
    pub fn build(self) -> ClientConfig {
        self.config
    }
}

impl Default for SdkConfigBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Backward-compatible alias.
pub type ConfigBuilder = SdkConfigBuilder;

/// Configuration
impl ClientConfig {
    /// Configuration
    pub fn from_env() -> crate::sdk::errors::Result<Self> {
        let mut builder = SdkConfigBuilder::new();

        // Configuration
        if let Ok(api_key) = std::env::var("OPENAI_API_KEY") {
            builder = builder.add_openai("openai", &api_key);
        }

        // Configuration
        if let Ok(api_key) = std::env::var("ANTHROPIC_API_KEY") {
            builder = builder.add_anthropic("anthropic", &api_key);
        }

        let config = builder.build();

        if config.providers.is_empty() {
            return Err(crate::sdk::errors::SDKError::ConfigError(
                "No providers configured. Please set OPENAI_API_KEY or ANTHROPIC_API_KEY environment variables.".to_string()
            ));
        }

        Ok(config)
    }

    /// Configuration
    pub fn from_file(path: &str) -> crate::sdk::errors::Result<Self> {
        let content = std::fs::read_to_string(path).map_err(|e| {
            crate::sdk::errors::SDKError::ConfigError(format!(
                "Failed to read config file {}: {}",
                path, e
            ))
        })?;

        serde_yml::from_str(&content).map_err(|e| {
            crate::sdk::errors::SDKError::ConfigError(format!(
                "Failed to parse config file {}: {}",
                path, e
            ))
        })
    }
}

// ==================== Unit Tests ====================

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

    // ==================== ClientConfig Tests ====================

    #[test]
    fn test_client_config_default() {
        let config = ClientConfig::default();
        assert!(config.default_provider.is_none());
        assert!(config.providers.is_empty());
        assert_eq!(config.settings.timeout, 30);
    }

    #[test]
    fn test_client_config_clone() {
        let config = ClientConfig {
            default_provider: Some("openai".to_string()),
            providers: vec![],
            settings: ClientSettings::default(),
        };
        let cloned = config.clone();
        assert_eq!(config.default_provider, cloned.default_provider);
    }

    #[test]
    fn test_client_config_serialization() {
        let config = ClientConfig::default();
        let json = serde_json::to_string(&config).unwrap();
        assert!(json.contains("\"settings\""));
        assert!(json.contains("\"providers\""));
    }

    #[test]
    fn test_client_config_deserialization() {
        let json = r#"{
            "default_provider": "openai",
            "providers": [],
            "settings": {
                "timeout": 60,
                "max_retries": 5,
                "max_concurrent_requests": 50,
                "enable_logging": false,
                "enable_metrics": true
            }
        }"#;
        let config: ClientConfig = serde_json::from_str(json).unwrap();
        assert_eq!(config.default_provider, Some("openai".to_string()));
        assert_eq!(config.settings.timeout, 60);
        assert_eq!(config.settings.max_retries, 5);
        assert!(!config.settings.enable_logging);
    }

    // ==================== ClientSettings Tests ====================

    #[test]
    fn test_client_settings_default() {
        let settings = ClientSettings::default();
        assert_eq!(settings.timeout, 30);
        assert_eq!(settings.max_retries, 3);
        assert_eq!(settings.max_concurrent_requests, 100);
        assert!(settings.enable_logging);
        assert!(settings.enable_metrics);
    }

    #[test]
    fn test_client_settings_clone() {
        let settings = ClientSettings {
            timeout: 60,
            max_retries: 5,
            max_concurrent_requests: 200,
            enable_logging: false,
            enable_metrics: false,
        };
        let cloned = settings.clone();
        assert_eq!(settings.timeout, cloned.timeout);
        assert_eq!(settings.max_retries, cloned.max_retries);
        assert_eq!(settings.enable_logging, cloned.enable_logging);
    }

    #[test]
    fn test_client_settings_serialization() {
        let settings = ClientSettings::default();
        let json = serde_json::to_string(&settings).unwrap();
        assert!(json.contains("\"timeout\":30"));
        assert!(json.contains("\"max_retries\":3"));
    }

    // ==================== ProviderConfig Tests ====================

    #[test]
    fn test_provider_config_creation() {
        let config = ProviderConfig {
            id: "openai-1".to_string(),
            provider_type: ProviderType::OpenAI,
            name: "OpenAI Production".to_string(),
            api_key: "sk-test".to_string(),
            base_url: None,
            models: vec!["gpt-4".to_string()],
            enabled: true,
            weight: 1.0,
            rate_limit_rpm: Some(3000),
            rate_limit_tpm: Some(250000),
            settings: HashMap::new(),
        };
        assert_eq!(config.id, "openai-1");
        assert!(config.enabled);
        assert_eq!(config.weight, 1.0);
    }

    #[test]
    fn test_provider_config_with_base_url() {
        let config = ProviderConfig {
            id: "custom".to_string(),
            provider_type: ProviderType::Custom("local".to_string()),
            name: "Local LLM".to_string(),
            api_key: "".to_string(),
            base_url: Some("http://localhost:8000".to_string()),
            models: vec!["llama-2".to_string()],
            enabled: true,
            weight: 0.5,
            rate_limit_rpm: None,
            rate_limit_tpm: None,
            settings: HashMap::new(),
        };
        assert_eq!(config.base_url, Some("http://localhost:8000".to_string()));
    }

    #[test]
    fn test_provider_config_with_settings() {
        let mut settings = HashMap::new();
        settings.insert("temperature".to_string(), serde_json::json!(0.7));
        settings.insert("max_tokens".to_string(), serde_json::json!(1000));

        let config = ProviderConfig {
            id: "openai".to_string(),
            provider_type: ProviderType::OpenAI,
            name: "OpenAI".to_string(),
            api_key: "sk-test".to_string(),
            base_url: None,
            models: vec![],
            enabled: true,
            weight: 1.0,
            rate_limit_rpm: None,
            rate_limit_tpm: None,
            settings,
        };
        assert_eq!(config.settings.len(), 2);
        assert_eq!(
            config.settings.get("temperature").unwrap(),
            &serde_json::json!(0.7)
        );
    }

    #[test]
    fn test_provider_config_serialization() {
        let config = ProviderConfig {
            id: "test".to_string(),
            provider_type: ProviderType::OpenAI,
            name: "Test".to_string(),
            api_key: "key".to_string(),
            base_url: None,
            models: vec!["gpt-4".to_string()],
            enabled: true,
            weight: 1.0,
            rate_limit_rpm: Some(1000),
            rate_limit_tpm: None,
            settings: HashMap::new(),
        };
        let json = serde_json::to_string(&config).unwrap();
        assert!(json.contains("\"id\":\"test\""));
        assert!(json.contains("\"enabled\":true"));
    }

    // ==================== ProviderType Tests ====================

    #[test]
    fn test_provider_type_from_str_known() {
        assert!(matches!(ProviderType::from("openai"), ProviderType::OpenAI));
        assert!(matches!(
            ProviderType::from("anthropic"),
            ProviderType::Anthropic
        ));
        assert!(matches!(ProviderType::from("azure"), ProviderType::Azure));
        assert!(matches!(ProviderType::from("google"), ProviderType::Google));
        assert!(matches!(ProviderType::from("cohere"), ProviderType::Cohere));
        assert!(matches!(
            ProviderType::from("huggingface"),
            ProviderType::HuggingFace
        ));
        assert!(matches!(ProviderType::from("ollama"), ProviderType::Ollama));
        assert!(matches!(
            ProviderType::from("aws_bedrock"),
            ProviderType::AwsBedrock
        ));
        assert!(matches!(
            ProviderType::from("google_vertex"),
            ProviderType::GoogleVertex
        ));
        assert!(matches!(
            ProviderType::from("mistral"),
            ProviderType::Mistral
        ));
    }

    #[test]
    fn test_provider_type_from_str_case_insensitive() {
        assert!(matches!(ProviderType::from("OpenAI"), ProviderType::OpenAI));
        assert!(matches!(ProviderType::from("OPENAI"), ProviderType::OpenAI));
        assert!(matches!(
            ProviderType::from("Anthropic"),
            ProviderType::Anthropic
        ));
        assert!(matches!(ProviderType::from("AZURE"), ProviderType::Azure));
    }

    #[test]
    fn test_provider_type_from_str_custom() {
        let custom = ProviderType::from("my-custom-provider");
        assert!(matches!(custom, ProviderType::Custom(s) if s == "my-custom-provider"));
    }

    #[test]
    fn test_provider_type_clone() {
        let provider = ProviderType::OpenAI;
        let cloned = provider.clone();
        assert!(matches!(cloned, ProviderType::OpenAI));

        let custom = ProviderType::Custom("test".to_string());
        let custom_cloned = custom.clone();
        assert!(matches!(custom_cloned, ProviderType::Custom(s) if s == "test"));
    }

    #[test]
    fn test_provider_type_serialization() {
        let openai = ProviderType::OpenAI;
        let json = serde_json::to_string(&openai).unwrap();
        assert_eq!(json, "\"open_a_i\"");

        let anthropic = ProviderType::Anthropic;
        let json = serde_json::to_string(&anthropic).unwrap();
        assert_eq!(json, "\"anthropic\"");
    }

    #[test]
    fn test_provider_type_deserialization() {
        let openai: ProviderType = serde_json::from_str("\"open_a_i\"").unwrap();
        assert!(matches!(openai, ProviderType::OpenAI));

        let anthropic: ProviderType = serde_json::from_str("\"anthropic\"").unwrap();
        assert!(matches!(anthropic, ProviderType::Anthropic));
    }

    // ==================== ConfigBuilder Tests ====================

    #[test]
    fn test_config_builder_new() {
        let builder = ConfigBuilder::new();
        let config = builder.build();
        assert!(config.default_provider.is_none());
        assert!(config.providers.is_empty());
    }

    #[test]
    fn test_config_builder_default() {
        let builder = ConfigBuilder::default();
        let config = builder.build();
        assert!(config.providers.is_empty());
    }

    #[test]
    fn test_config_builder_default_provider() {
        let config = ConfigBuilder::new().default_provider("openai").build();
        assert_eq!(config.default_provider, Some("openai".to_string()));
    }

    #[test]
    fn test_config_builder_add_provider() {
        let provider = ProviderConfig {
            id: "test".to_string(),
            provider_type: ProviderType::OpenAI,
            name: "Test".to_string(),
            api_key: "key".to_string(),
            base_url: None,
            models: vec![],
            enabled: true,
            weight: 1.0,
            rate_limit_rpm: None,
            rate_limit_tpm: None,
            settings: HashMap::new(),
        };
        let config = ConfigBuilder::new().add_provider(provider).build();
        assert_eq!(config.providers.len(), 1);
        assert_eq!(config.providers[0].id, "test");
    }

    #[test]
    fn test_config_builder_add_openai() {
        let config = ConfigBuilder::new()
            .add_openai("openai-prod", "sk-test-key")
            .build();
        assert_eq!(config.providers.len(), 1);
        assert_eq!(config.providers[0].id, "openai-prod");
        assert_eq!(config.providers[0].api_key, "sk-test-key");
        assert!(matches!(
            config.providers[0].provider_type,
            ProviderType::OpenAI
        ));
        assert!(!config.providers[0].models.is_empty());
        assert!(
            config.providers[0]
                .models
                .iter()
                .any(|model| model.starts_with("gpt-"))
        );
    }

    #[test]
    fn test_config_builder_add_anthropic() {
        let config = ConfigBuilder::new()
            .add_anthropic("anthropic-prod", "sk-ant-test")
            .build();
        assert_eq!(config.providers.len(), 1);
        assert_eq!(config.providers[0].id, "anthropic-prod");
        assert!(matches!(
            config.providers[0].provider_type,
            ProviderType::Anthropic
        ));
        assert!(
            config.providers[0]
                .models
                .iter()
                .any(|m| m.contains("claude"))
        );
    }

    #[test]
    fn test_config_builder_timeout() {
        let config = ConfigBuilder::new().timeout(120).build();
        assert_eq!(config.settings.timeout, 120);
    }

    #[test]
    fn test_config_builder_max_retries() {
        let config = ConfigBuilder::new().max_retries(5).build();
        assert_eq!(config.settings.max_retries, 5);
    }

    #[test]
    fn test_config_builder_chaining() {
        let config = ConfigBuilder::new()
            .default_provider("openai")
            .add_openai("openai", "sk-key1")
            .add_anthropic("anthropic", "sk-ant-key")
            .timeout(60)
            .max_retries(5)
            .build();

        assert_eq!(config.default_provider, Some("openai".to_string()));
        assert_eq!(config.providers.len(), 2);
        assert_eq!(config.settings.timeout, 60);
        assert_eq!(config.settings.max_retries, 5);
    }

    #[test]
    fn test_config_builder_multiple_providers() {
        let config = ConfigBuilder::new()
            .add_openai("openai-1", "key1")
            .add_openai("openai-2", "key2")
            .add_anthropic("anthropic-1", "ant-key")
            .build();

        assert_eq!(config.providers.len(), 3);
    }

    // ==================== ClientConfig Methods Tests ====================

    #[test]
    fn test_client_config_from_file_not_found() {
        let result = ClientConfig::from_file("/nonexistent/path/config.yaml");
        assert!(result.is_err());
    }

    // ==================== Integration Tests ====================

    #[test]
    fn test_full_config_roundtrip() {
        let config = ConfigBuilder::new()
            .default_provider("openai")
            .add_openai("openai", "sk-test")
            .timeout(45)
            .build();

        // Serialize to JSON
        let json = serde_json::to_string(&config).unwrap();

        // Deserialize back
        let restored: ClientConfig = serde_json::from_str(&json).unwrap();

        assert_eq!(config.default_provider, restored.default_provider);
        assert_eq!(config.providers.len(), restored.providers.len());
        assert_eq!(config.settings.timeout, restored.settings.timeout);
    }

    #[test]
    fn test_yaml_serialization() {
        let config = ConfigBuilder::new().add_openai("openai", "sk-test").build();

        let yaml = serde_yml::to_string(&config).unwrap();
        assert!(yaml.contains("providers"));
        assert!(yaml.contains("settings"));

        let restored: ClientConfig = serde_yml::from_str(&yaml).unwrap();
        assert_eq!(config.providers.len(), restored.providers.len());
    }
}