litellm-rs 0.6.0

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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//! Tests for provider-specific config builders

use super::builder::*;
#[cfg(feature = "providers-extended")]
use super::cohere_builder::build_cohere_config_from_factory;
#[cfg(feature = "providers-extended")]
use super::gemini_builder::build_gemini_config_from_factory;
use super::{Provider, ProviderType, create_provider};
use crate::core::net::ProviderEndpointAccess;
use std::sync::Mutex;

static ENV_LOCK: Mutex<()> = Mutex::new(());
const AWS_DEFAULT_REGION: &str = "AWS_DEFAULT_REGION";
const BEDROCK_ENV_KEYS_WITH_DEFAULT_REGION: [&str; 5] = [
    "AWS_ACCESS_KEY_ID",
    "AWS_SECRET_ACCESS_KEY",
    "AWS_SESSION_TOKEN",
    "AWS_REGION",
    AWS_DEFAULT_REGION,
];

struct EnvSnapshot {
    values: Vec<(&'static str, Option<String>)>,
}

impl EnvSnapshot {
    fn clear(keys: &[&'static str]) -> Self {
        let values = keys
            .iter()
            .map(|key| {
                let value = std::env::var(key).ok();
                unsafe { std::env::remove_var(key) };
                (*key, value)
            })
            .collect();

        Self { values }
    }
}

impl Drop for EnvSnapshot {
    fn drop(&mut self) {
        for (key, value) in &self.values {
            if let Some(value) = value {
                unsafe { std::env::set_var(key, value) };
            } else {
                unsafe { std::env::remove_var(key) };
            }
        }
    }
}

#[test]
fn test_build_openai_config_from_factory_maps_optional_fields() {
    let config = serde_json::json!({
        "api_key": "sk-test123",
        "base_url": "https://example-openai.test/v1",
        "endpoint_access": "private_network",
        "timeout": 42,
        "max_retries": 7,
        "organization": "org-test",
        "project": "proj-test",
        "headers": {
            "x-team-id": "team-1"
        },
        "custom_headers": {
            "x-request-source": "gateway"
        },
        "model_mappings": {
            "gpt-4": "gpt-4o",
            "ignored": 123
        }
    });

    let openai_config = build_openai_config_from_factory(&config)
        .unwrap_or_else(|err| panic!("openai config should parse: {err}"));
    assert_eq!(openai_config.base.api_key.as_deref(), Some("sk-test123"));
    assert_eq!(
        openai_config.base.api_base.as_deref(),
        Some("https://example-openai.test/v1")
    );
    assert_eq!(openai_config.base.timeout, 42);
    assert_eq!(openai_config.base.max_retries, 7);
    assert_eq!(
        openai_config.base.endpoint_access,
        ProviderEndpointAccess::PrivateNetwork
    );
    assert_eq!(openai_config.organization.as_deref(), Some("org-test"));
    assert_eq!(openai_config.project.as_deref(), Some("proj-test"));
    assert_eq!(
        openai_config
            .base
            .headers
            .get("x-team-id")
            .map(String::as_str),
        Some("team-1")
    );
    assert_eq!(
        openai_config
            .base
            .headers
            .get("x-request-source")
            .map(String::as_str),
        Some("gateway")
    );
    assert_eq!(
        openai_config
            .model_mappings
            .get("gpt-4")
            .map(String::as_str),
        Some("gpt-4o")
    );
    assert!(!openai_config.model_mappings.contains_key("ignored"));
}

#[test]
fn test_build_anthropic_config_from_factory_maps_optional_fields() {
    let config = serde_json::json!({
        "api_key": "sk-ant-test",
        "api_base": "https://example-anthropic.test",
        "endpoint_access": "private_network",
        "api_version": "2024-01-01",
        "timeout": 99,
        "max_retries": 6,
        "retry_delay_base": 250,
        "headers": {
            "x-anthropic-a": "a"
        },
        "custom_headers": {
            "x-anthropic-b": "b"
        },
        "enable_multimodal": false,
        "enable_cache_control": false,
        "enable_computer_use": true,
        "enable_experimental": true,
        "allow_unknown_models": true,
        "models": ["mimo-v2.5", "mimo-v2.5-pro"],
        "multimodal_models": ["mimo-v2.5"]
    });

    let anthropic_config = build_anthropic_config_from_factory(&config)
        .unwrap_or_else(|err| panic!("anthropic config should parse: {err}"));
    assert_eq!(anthropic_config.api_key.as_deref(), Some("sk-ant-test"));
    assert_eq!(anthropic_config.base_url, "https://example-anthropic.test");
    assert_eq!(anthropic_config.api_version, "2024-01-01");
    assert_eq!(anthropic_config.request_timeout, 99);
    assert_eq!(anthropic_config.connect_timeout, 10);
    assert_eq!(anthropic_config.max_retries, 6);
    assert_eq!(anthropic_config.retry_delay_base, 250);
    assert!(anthropic_config.proxy_url.is_none());
    assert_eq!(
        anthropic_config.endpoint_access,
        ProviderEndpointAccess::PrivateNetwork
    );
    assert_eq!(
        anthropic_config
            .custom_headers
            .get("x-anthropic-a")
            .map(String::as_str),
        Some("a")
    );
    assert_eq!(
        anthropic_config
            .custom_headers
            .get("x-anthropic-b")
            .map(String::as_str),
        Some("b")
    );
    assert!(!anthropic_config.enable_multimodal);
    assert!(!anthropic_config.enable_cache_control);
    assert!(anthropic_config.enable_computer_use);
    assert!(anthropic_config.enable_experimental);
    assert!(anthropic_config.allow_unknown_models);
    assert_eq!(
        anthropic_config.configured_models,
        vec!["mimo-v2.5".to_string(), "mimo-v2.5-pro".to_string()]
    );
    assert_eq!(
        anthropic_config.configured_multimodal_models,
        vec!["mimo-v2.5".to_string()]
    );
}

#[test]
fn test_anthropic_factory_rejects_unsafe_client_options() {
    for config in [
        serde_json::json!({
            "api_key": "sk-ant-test",
            "api_base": "https://8.8.8.8",
            "proxy": "http://localhost:8080"
        }),
        serde_json::json!({
            "api_key": "sk-ant-test",
            "api_base": "https://8.8.8.8",
            "connect_timeout": 12
        }),
    ] {
        assert!(build_anthropic_config_from_factory(&config).is_err());
    }
}

#[cfg(feature = "providers-extended")]
#[test]
fn test_gemini_factory_maps_access_and_rejects_unsafe_client_options() {
    let config = serde_json::json!({
        "api_key": "test-api-key-1234567890123456",
        "base_url": "http://127.0.0.1:18080",
        "endpoint_access": "private_network"
    });
    let gemini = build_gemini_config_from_factory(&config)
        .unwrap_or_else(|error| panic!("private Gemini config should build: {error}"));
    assert_eq!(
        gemini.endpoint_access,
        ProviderEndpointAccess::PrivateNetwork
    );

    for invalid in [
        serde_json::json!({
            "api_key": "test-api-key-1234567890123456",
            "base_url": "http://127.0.0.1:18080",
            "endpoint_access": "private_network",
            "proxy_url": "http://localhost:8080"
        }),
        serde_json::json!({
            "api_key": "test-api-key-1234567890123456",
            "base_url": "http://127.0.0.1:18080",
            "endpoint_access": "private_network",
            "connect_timeout": 12
        }),
    ] {
        assert!(build_gemini_config_from_factory(&invalid).is_err());
    }
}

#[test]
fn test_build_anthropic_config_from_factory_validates_compatible_models() {
    let config = serde_json::json!({
        "api_key": "xiaomi-compatible-key",
        "api_base": "https://token-plan-sgp.xiaomimimo.com/anthropic",
        "allow_unknown_models": true
    });

    let err = match build_anthropic_config_from_factory(&config) {
        Ok(_) => panic!("compatible Anthropic config should require models"),
        Err(err) => err,
    };

    assert!(format!("{err}").contains("explicit models allow-list"));
}

#[test]
fn test_build_mistral_config_from_factory_maps_optional_fields() {
    let config = serde_json::json!({
        "api_key": "mistral-key",
        "api_base": "https://example-mistral.test/v1",
        "endpoint_access": "private_network",
        "timeout": 88,
        "max_retries": 4
    });

    let mistral_config = build_mistral_config_from_factory(&config)
        .unwrap_or_else(|err| panic!("mistral config should parse: {err}"));
    assert_eq!(mistral_config.api_key, "mistral-key");
    assert_eq!(mistral_config.api_base, "https://example-mistral.test/v1");
    assert_eq!(mistral_config.timeout_seconds, 88);
    assert_eq!(mistral_config.max_retries, 4);
    assert_eq!(
        mistral_config.endpoint_access,
        ProviderEndpointAccess::PrivateNetwork
    );
}

#[cfg(feature = "providers-extended")]
#[test]
fn test_build_cohere_config_from_factory_maps_endpoint_access() {
    let config = serde_json::json!({
        "api_key": "cohere-key",
        "base_url": "https://example-cohere.test",
        "endpoint_access": "private_network",
        "timeout": 73,
        "max_retries": 4
    });

    let cohere_config = build_cohere_config_from_factory(&config)
        .unwrap_or_else(|error| panic!("Cohere config should parse: {error}"));
    assert_eq!(cohere_config.api_base, "https://example-cohere.test");
    assert_eq!(cohere_config.timeout_seconds, 73);
    assert_eq!(cohere_config.max_retries, 4);
    assert_eq!(
        cohere_config.endpoint_access,
        ProviderEndpointAccess::PrivateNetwork
    );
}

#[test]
fn test_build_cloudflare_config_from_factory_maps_alias_and_optional_fields() {
    let config = serde_json::json!({
        "organization": "acct-xyz",
        "api_key": "token-xyz",
        "base_url": "https://cf.example.test",
        "timeout": 77,
        "max_retries": 5,
        "debug": true
    });

    let cf_config = build_cloudflare_config_from_factory(&config)
        .unwrap_or_else(|err| panic!("cloudflare config should parse: {err}"));
    assert_eq!(cf_config.account_id.as_deref(), Some("acct-xyz"));
    assert_eq!(cf_config.api_token.as_deref(), Some("token-xyz"));
    assert_eq!(
        cf_config.api_base.as_deref(),
        Some("https://cf.example.test")
    );
    assert_eq!(cf_config.timeout, 77);
    assert_eq!(cf_config.max_retries, 5);
    assert!(cf_config.debug);
}

#[test]
fn test_build_openai_like_config_from_factory_maps_optional_fields() {
    let config = serde_json::json!({
        "base_url": "https://openai-like.example.test/v1",
        "api_key": "sk-openai-like",
        "endpoint_access": "private_network",
        "provider_name": "custom-like",
        "timeout": 55,
        "max_retries": 4,
        "model_prefix": "prefix/",
        "default_model": "gpt-4o-mini",
        "pass_through_params": false,
        "skip_api_key": true,
        "organization": "org-like",
        "api_version": "2024-12-01",
        "headers": {
            "x-base-header": "base"
        },
        "custom_headers": {
            "x-custom-header": "custom"
        }
    });

    let oai_like = build_openai_like_config_from_factory(&config)
        .unwrap_or_else(|err| panic!("openai_like config should parse: {err}"));

    assert_eq!(
        oai_like.base.api_base.as_deref(),
        Some("https://openai-like.example.test/v1")
    );
    assert_eq!(oai_like.base.api_key.as_deref(), Some("sk-openai-like"));
    assert_eq!(oai_like.provider_name, "custom-like");
    assert_eq!(oai_like.base.timeout, 55);
    assert_eq!(oai_like.base.max_retries, 4);
    assert_eq!(
        oai_like.base.endpoint_access,
        ProviderEndpointAccess::PrivateNetwork
    );
    assert_eq!(oai_like.model_prefix.as_deref(), Some("prefix/"));
    assert_eq!(oai_like.default_model.as_deref(), Some("gpt-4o-mini"));
    assert!(!oai_like.pass_through_params);
    assert!(oai_like.skip_api_key);
    assert_eq!(oai_like.base.organization.as_deref(), Some("org-like"));
    assert_eq!(oai_like.base.api_version.as_deref(), Some("2024-12-01"));
    assert_eq!(
        oai_like
            .base
            .headers
            .get("x-base-header")
            .map(String::as_str),
        Some("base")
    );
    assert_eq!(
        oai_like
            .custom_headers
            .get("x-custom-header")
            .map(String::as_str),
        Some("custom")
    );
}

#[test]
fn test_build_openai_like_config_from_factory_requires_api_base() {
    let config = serde_json::json!({
        "api_key": "sk-openai-like"
    });

    let err = build_openai_like_config_from_factory(&config)
        .err()
        .unwrap_or_else(|| panic!("missing base_url should return an error"));
    assert!(err.to_string().contains("base_url"));
}

#[test]
fn test_factory_endpoint_access_defaults_and_rejects_invalid_values() {
    let openai = build_openai_config_from_factory(&serde_json::json!({
        "api_key": "sk-test"
    }))
    .unwrap_or_else(|error| panic!("default OpenAI access should parse: {error}"));
    let openai_like = build_openai_like_config_from_factory(&serde_json::json!({
        "base_url": "https://api.example.test/v1",
        "skip_api_key": true
    }))
    .unwrap_or_else(|error| panic!("default OpenAI-like access should parse: {error}"));
    assert_eq!(
        openai.base.endpoint_access,
        ProviderEndpointAccess::PublicOnly
    );
    assert_eq!(
        openai_like.base.endpoint_access,
        ProviderEndpointAccess::PublicOnly
    );

    for invalid in [
        serde_json::json!(""),
        serde_json::json!("private"),
        serde_json::json!(true),
    ] {
        let config = serde_json::json!({"endpoint_access": invalid});
        let error = config_endpoint_access(&config, "test")
            .err()
            .unwrap_or_else(|| panic!("invalid endpoint_access must fail"));
        assert!(error.to_string().contains("invalid endpoint_access"));
    }
}
#[tokio::test]
async fn test_gateway_and_direct_registry_endpoint_access_contract() {
    let mut config = crate::config::models::provider::ProviderConfig {
        name: "test-openai-like".to_string(),
        provider_type: "openai_compatible".to_string(),
        api_key: "test-key".to_string(),
        base_url: Some("https://api.example.com/v1".to_string()),
        ..Default::default()
    };
    let provider = create_provider(config.clone())
        .await
        .unwrap_or_else(|error| panic!("public-only Gateway config should work: {error}"));
    let Provider::OpenAILike(provider) = provider else {
        panic!("expected OpenAILike provider");
    };
    assert_eq!(
        provider.config().base.endpoint_access,
        ProviderEndpointAccess::PublicOnly
    );
    config.endpoint_access = ProviderEndpointAccess::PrivateNetwork;
    config.base_url = None;
    config
        .settings
        .insert("api_base".into(), serde_json::json!("http://127.0.0.1/v1"));
    assert!(crate::config::Validate::validate(&config).is_ok());
    config
        .settings
        .insert("api_base".into(), serde_json::json!("wss://127.0.0.1/v1"));
    assert!(crate::config::Validate::validate(&config).is_err());
    config
        .settings
        .insert("api_base".into(), serde_json::json!("http://127.0.0.1/v1"));
    assert!(create_provider(config).await.is_ok());
    let mut settings_override = crate::config::models::provider::ProviderConfig {
        name: "openai".to_string(),
        provider_type: "openai".to_string(),
        api_key: "sk-test".to_string(),
        ..Default::default()
    };
    settings_override.settings.insert(
        "endpoint_access".to_string(),
        serde_json::json!("private_network"),
    );
    let error = create_provider(settings_override)
        .await
        .err()
        .unwrap_or_else(|| panic!("settings must not override endpoint access"));
    assert!(error.to_string().contains("top-level"));
    let provider = Provider::from_config_async(
        ProviderType::Custom("together".to_string()),
        serde_json::json!({"api_key": "x", "base_url": "http://127.0.0.1/v1",
            "endpoint_access": "private_network"}),
    )
    .await
    .unwrap_or_else(|error| panic!("catalog-backed custom type should activate: {error}"));
    let Provider::OpenAILike(provider) = provider else {
        panic!("expected catalog-backed OpenAILike provider");
    };
    assert_eq!(
        provider.config().base.endpoint_access,
        ProviderEndpointAccess::PrivateNetwork
    );
}

#[cfg(not(feature = "providers-extra"))]
#[test]
fn test_build_azure_openai_like_fallback_configs_map_fields() {
    let azure = serde_json::json!({
        "api_key": "azure-key",
        "azure_endpoint": "https://example-resource.openai.azure.com/openai/deployments/prod",
        "api_version": "2024-03-01",
        "timeout": 66,
        "max_retries": 4,
        "headers": {
            "x-azure-base": "base"
        },
        "custom_headers": {
            "x-azure-custom": "custom"
        }
    });
    let azure_config = build_azure_openai_like_config_from_factory(&azure)
        .unwrap_or_else(|err| panic!("azure fallback config should parse: {err}"));

    assert_eq!(azure_config.provider_name, "azure");
    assert_eq!(azure_config.base.api_key.as_deref(), Some("azure-key"));
    assert_eq!(
        azure_config.base.api_base.as_deref(),
        Some("https://example-resource.openai.azure.com/openai/deployments/prod")
    );
    assert_eq!(azure_config.base.api_version.as_deref(), Some("2024-03-01"));
    assert_eq!(azure_config.base.timeout, 66);
    assert_eq!(azure_config.base.max_retries, 4);
    assert_eq!(
        azure_config
            .base
            .headers
            .get("x-azure-base")
            .map(String::as_str),
        Some("base")
    );
    assert_eq!(
        azure_config
            .custom_headers
            .get("x-azure-custom")
            .map(String::as_str),
        Some("custom")
    );

    let azure_ai = serde_json::json!({
        "api_key": "azure-ai-key",
        "azure_ai_endpoint": "https://example-resource.services.ai.azure.com/models",
        "api_version": "2024-05-01-preview"
    });
    let azure_ai_config = build_azure_ai_openai_like_config_from_factory(&azure_ai)
        .unwrap_or_else(|err| panic!("azure_ai fallback config should parse: {err}"));

    assert_eq!(azure_ai_config.provider_name, "azure_ai");
    assert_eq!(
        azure_ai_config.base.api_key.as_deref(),
        Some("azure-ai-key")
    );
    assert_eq!(
        azure_ai_config.base.api_base.as_deref(),
        Some("https://example-resource.services.ai.azure.com/models")
    );
    assert_eq!(
        azure_ai_config.base.api_version.as_deref(),
        Some("2024-05-01-preview")
    );
}

#[cfg(not(feature = "providers-extra"))]
#[test]
fn test_azure_openai_like_fallback_rejects_bare_resource_endpoints() {
    let azure = serde_json::json!({
        "api_key": "azure-key",
        "azure_endpoint": "https://example-resource.openai.azure.com",
        "deployment_name": "prod"
    });
    let err = build_azure_openai_like_config_from_factory(&azure)
        .err()
        .unwrap_or_else(|| panic!("bare Azure endpoint should be rejected"));
    assert!(err.to_string().contains("providers-extra"));

    let azure_ai = serde_json::json!({
        "api_key": "azure-ai-key",
        "azure_ai_endpoint": "https://example-resource.services.ai.azure.com"
    });
    let err = build_azure_ai_openai_like_config_from_factory(&azure_ai)
        .err()
        .unwrap_or_else(|| panic!("bare Azure AI endpoint should be rejected"));
    assert!(err.to_string().contains("providers-extra"));
}

#[cfg(feature = "providers-extra")]
#[test]
fn test_build_azure_config_from_factory_maps_native_fields() {
    let config = serde_json::json!({
        "api_key": "azure-key",
        "azure_endpoint": "https://example-resource.openai.azure.com",
        "endpoint_access": "private_network",
        "deployment_name": "gpt-4o-prod",
        "api_version": "2024-03-01",
        "timeout": 31,
        "max_retries": 6,
        "headers": {
            "x-azure-base": "base"
        },
        "custom_headers": {
            "x-azure-custom": "custom"
        }
    });

    let azure_config = build_azure_config_from_factory(&config)
        .unwrap_or_else(|err| panic!("azure config should parse: {err}"));

    assert_eq!(azure_config.api_key.as_deref(), Some("azure-key"));
    assert_eq!(
        azure_config.azure_endpoint.as_deref(),
        Some("https://example-resource.openai.azure.com")
    );
    assert_eq!(azure_config.deployment_name.as_deref(), Some("gpt-4o-prod"));
    assert_eq!(azure_config.api_version, "2024-03-01");
    assert_eq!(azure_config.timeout, 31);
    assert_eq!(azure_config.max_retries, 6);
    assert_eq!(
        azure_config.endpoint_access,
        ProviderEndpointAccess::PrivateNetwork
    );
    assert_eq!(
        azure_config
            .custom_headers
            .get("x-azure-base")
            .map(String::as_str),
        Some("base")
    );
    assert_eq!(
        azure_config
            .custom_headers
            .get("x-azure-custom")
            .map(String::as_str),
        Some("custom")
    );
}

#[cfg(feature = "providers-extra")]
#[test]
fn test_build_azure_ai_config_from_factory_maps_native_fields() {
    let config = serde_json::json!({
        "api_key": "azure-ai-key",
        "azure_ai_endpoint": "https://example-resource.services.ai.azure.com",
        "api_version": "2024-05-01-preview",
        "timeout": 44,
        "max_retries": 2,
        "headers": {
            "x-azure-ai-base": "base"
        },
        "custom_headers": {
            "x-azure-ai-custom": "custom"
        }
    });

    let azure_ai_config = build_azure_ai_config_from_factory(&config)
        .unwrap_or_else(|err| panic!("azure_ai config should parse: {err}"));

    assert_eq!(
        azure_ai_config.base.api_key.as_deref(),
        Some("azure-ai-key")
    );
    assert_eq!(
        azure_ai_config.base.api_base.as_deref(),
        Some("https://example-resource.services.ai.azure.com")
    );
    assert_eq!(
        azure_ai_config.base.api_version.as_deref(),
        Some("2024-05-01-preview")
    );
    assert_eq!(azure_ai_config.base.timeout, 44);
    assert_eq!(azure_ai_config.base.max_retries, 2);
    assert_eq!(
        azure_ai_config
            .base
            .headers
            .get("x-azure-ai-base")
            .map(String::as_str),
        Some("base")
    );
    assert_eq!(
        azure_ai_config
            .base
            .headers
            .get("x-azure-ai-custom")
            .map(String::as_str),
        Some("custom")
    );
}

#[cfg(feature = "providers-extra")]
#[test]
fn test_vertex_factory_maps_endpoint_access() {
    let config = serde_json::json!({
        "project_id": "project",
        "location": "us-central1",
        "base_url": "http://127.0.0.1:18080",
        "endpoint_access": "private_network",
        "access_token": "token"
    });
    let vertex = build_vertex_ai_config_from_factory(&config)
        .unwrap_or_else(|error| panic!("private Vertex config should build: {error}"));
    assert_eq!(
        vertex.endpoint_access,
        ProviderEndpointAccess::PrivateNetwork
    );
}

#[test]
fn test_env_str_any_skips_empty_values() {
    let _guard = ENV_LOCK.lock().unwrap();
    let _snapshot = EnvSnapshot::clear(&["AWS_REGION", AWS_DEFAULT_REGION]);

    unsafe {
        std::env::set_var("AWS_REGION", "");
        std::env::set_var(AWS_DEFAULT_REGION, "us-west-2");
    }

    assert_eq!(
        env_str_any(&["AWS_REGION", AWS_DEFAULT_REGION]).as_deref(),
        Some("us-west-2")
    );
}

#[test]
fn test_build_bedrock_config_defaults_region() {
    let _guard = ENV_LOCK.lock().unwrap();
    let _snapshot = EnvSnapshot::clear(&BEDROCK_ENV_KEYS_WITH_DEFAULT_REGION);

    let config = serde_json::json!({
        "aws_access_key_id": "AKIATEST123456789012",
        "aws_secret_access_key": "test-secret-key",
        "endpoint_access": "private_network"
    });

    let bedrock_config = build_bedrock_config_from_factory(&config)
        .unwrap_or_else(|err| panic!("bedrock config should parse: {err}"));

    assert_eq!(bedrock_config.aws_region, "us-east-1");
    assert_eq!(bedrock_config.aws_access_key_id, "AKIATEST123456789012");
    assert_eq!(bedrock_config.aws_secret_access_key, "test-secret-key");
    assert_eq!(
        bedrock_config.endpoint_access,
        ProviderEndpointAccess::PrivateNetwork
    );
}

#[test]
fn test_build_bedrock_config_rejects_missing_credentials() {
    let _guard = ENV_LOCK.lock().unwrap();
    let _snapshot = EnvSnapshot::clear(&BEDROCK_ENV_KEYS_WITH_DEFAULT_REGION);

    let err = build_bedrock_config_from_factory(&serde_json::json!({}))
        .err()
        .unwrap_or_else(|| panic!("missing credentials should return an error"));
    assert!(err.to_string().contains("aws_access_key_id"));

    let err = build_bedrock_config_from_factory(&serde_json::json!({
        "aws_access_key_id": "AKIATEST123456789012"
    }))
    .err()
    .unwrap_or_else(|| panic!("missing secret should return an error"));
    assert!(err.to_string().contains("aws_secret_access_key"));
}

#[test]
fn test_build_bedrock_config_skips_empty_env_values() {
    let _guard = ENV_LOCK.lock().unwrap();
    let _snapshot = EnvSnapshot::clear(&BEDROCK_ENV_KEYS_WITH_DEFAULT_REGION);

    unsafe {
        std::env::set_var("AWS_ACCESS_KEY_ID", "AKIATEST123456789012");
        std::env::set_var("AWS_SECRET_ACCESS_KEY", "test-secret-key");
        std::env::set_var("AWS_SESSION_TOKEN", "");
        std::env::set_var("AWS_REGION", "");
        std::env::set_var(AWS_DEFAULT_REGION, "us-west-2");
    }

    let bedrock_config = build_bedrock_config_from_factory(&serde_json::json!({}))
        .unwrap_or_else(|err| panic!("bedrock config should parse from env: {err}"));

    assert_eq!(bedrock_config.aws_access_key_id, "AKIATEST123456789012");
    assert_eq!(bedrock_config.aws_secret_access_key, "test-secret-key");
    assert_eq!(bedrock_config.aws_region, "us-west-2");
    assert!(bedrock_config.aws_session_token.is_none());
}

#[test]
fn test_build_bedrock_config_skips_empty_config_session_token() {
    let _guard = ENV_LOCK.lock().unwrap();
    let _snapshot = EnvSnapshot::clear(&BEDROCK_ENV_KEYS_WITH_DEFAULT_REGION);

    unsafe {
        std::env::set_var("AWS_SESSION_TOKEN", "env-session-token");
    }

    let bedrock_config = build_bedrock_config_from_factory(&serde_json::json!({
        "aws_access_key_id": "AKIATEST123456789012",
        "aws_secret_access_key": "test-secret-key",
        "aws_session_token": "",
        "aws_region": "us-west-2"
    }))
    .unwrap_or_else(|err| panic!("bedrock config should parse: {err}"));

    assert_eq!(
        bedrock_config.aws_session_token.as_deref(),
        Some("env-session-token")
    );
}