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
use super::*;
use crate::core::providers::provider_type::ProviderType;
use crate::core::providers::registry::types::{ProviderDispatchKind, provider_type_registry};
use std::collections::BTreeSet;
use std::fs;
use std::path::{Path, PathBuf};

const PROVIDER_IMPL_MARKERS: &[&str] = &[
    "LLMProvider for",
    "define_http_provider_with_hooks!(",
    "define_pooled_http_provider_with_hooks!(",
    "define_openai_compatible_provider!(",
    "standard_provider!(",
];

fn lifecycle_module_names() -> BTreeSet<&'static str> {
    PROVIDER_MODULE_LIFECYCLE
        .iter()
        .map(|entry| entry.module_name)
        .collect()
}

fn orphan_baseline_module_names() -> BTreeSet<&'static str> {
    PROVIDER_ORPHAN_BASELINE
        .iter()
        .map(|entry| entry.module_name)
        .collect()
}

fn registry_runtime_module_names() -> BTreeSet<&'static str> {
    let mut modules = provider_type_registry()
        .iter()
        .filter(|entry| entry.dispatch_kind == ProviderDispatchKind::Native)
        .map(|entry| entry.canonical_name)
        .collect::<BTreeSet<_>>();

    assert!(
        provider_type_registry()
            .iter()
            .any(
                |entry| entry.dispatch_kind == ProviderDispatchKind::ExplicitOpenAiLike
                    || entry.dispatch_kind == ProviderDispatchKind::CatalogOpenAiLike
            ),
        "OpenAI-like runtime dispatch entries should be present"
    );
    modules.insert("openai_like");
    modules
}

fn lifecycle_for(module_name: &str) -> ProviderModuleLifecycle {
    PROVIDER_MODULE_LIFECYCLE
        .iter()
        .find(|entry| entry.module_name == module_name)
        .unwrap_or_else(|| panic!("missing lifecycle entry for {module_name}"))
        .lifecycle
}

fn providers_dir() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("src/core/providers")
}

fn provider_directories() -> BTreeSet<String> {
    fs::read_dir(providers_dir())
        .expect("providers directory should be readable")
        .filter_map(|entry| {
            let entry = entry.expect("provider directory entry should be readable");
            if !entry
                .file_type()
                .expect("file type should be readable")
                .is_dir()
            {
                return None;
            }
            entry.file_name().into_string().ok()
        })
        .collect()
}

fn is_registry_runtime_module(module_name: &str) -> bool {
    registry_runtime_module_names().contains(module_name)
}

fn disabled_feature_gated_runtime_module_names() -> BTreeSet<&'static str> {
    let mut modules = BTreeSet::new();

    for provider_type in [ProviderType::Azure, ProviderType::AzureAI] {
        insert_disabled_feature_gated_module(
            &mut modules,
            provider_type,
            ProviderDispatchKind::ExplicitOpenAiLike,
            cfg!(feature = "providers-extra"),
        );
    }
    insert_disabled_feature_gated_module(
        &mut modules,
        ProviderType::VertexAI,
        ProviderDispatchKind::UnsupportedEnum,
        cfg!(feature = "providers-extra"),
    );

    for provider_type in [
        ProviderType::Cohere,
        ProviderType::FalAI,
        ProviderType::Gemini,
        ProviderType::GitHubCopilot,
        ProviderType::Ollama,
        ProviderType::Replicate,
    ] {
        insert_disabled_feature_gated_module(
            &mut modules,
            provider_type,
            ProviderDispatchKind::UnsupportedEnum,
            cfg!(feature = "providers-extended"),
        );
    }

    modules
}

fn insert_disabled_feature_gated_module(
    modules: &mut BTreeSet<&'static str>,
    provider_type: ProviderType,
    expected_disabled_dispatch_kind: ProviderDispatchKind,
    feature_enabled: bool,
) {
    if feature_enabled {
        return;
    }

    let entry = provider_type_registry()
        .iter()
        .find(|entry| entry.provider_type == provider_type)
        .unwrap_or_else(|| panic!("missing provider registry entry for {provider_type:?}"));
    assert_eq!(
        entry.dispatch_kind, expected_disabled_dispatch_kind,
        "{} disabled feature-gated runtime dispatch should come from the registry",
        entry.canonical_name
    );
    assert_eq!(
        lifecycle_for(entry.canonical_name),
        ProviderModuleLifecycle::Stub,
        "{} disabled feature-gated runtime module should be a lifecycle Stub",
        entry.canonical_name
    );
    modules.insert(entry.canonical_name);
}

fn directory_contains_provider_impl_marker(module_name: &str) -> bool {
    if matches!(module_name, "macros" | "registry") {
        return false;
    }

    let mut pending_dirs = vec![providers_dir().join(module_name)];
    while let Some(dir) = pending_dirs.pop() {
        for entry in fs::read_dir(&dir)
            .unwrap_or_else(|err| panic!("provider directory {dir:?} should be readable: {err}"))
        {
            let entry = entry.expect("provider directory entry should be readable");
            let path = entry.path();
            let file_type = entry.file_type().expect("file type should be readable");

            if file_type.is_dir() {
                pending_dirs.push(path);
                continue;
            }

            if path.extension().is_some_and(|extension| extension == "rs") {
                let source = fs::read_to_string(&path).unwrap_or_else(|err| {
                    panic!("provider source {path:?} should be readable: {err}")
                });
                if PROVIDER_IMPL_MARKERS
                    .iter()
                    .any(|marker| source.contains(marker))
                {
                    return true;
                }
            }
        }
    }

    false
}

fn directory_declares_chat_capability(module_name: &str) -> bool {
    directory_source_contains(module_name, |source| {
        source.lines().any(|line| {
            let trimmed = line.trim_start();
            trimmed.contains("ProviderCapability::ChatCompletion")
                && !trimmed.starts_with("//")
                && !trimmed.contains("assert")
        })
    })
}

fn directory_source_contains(module_name: &str, contains_marker: impl Fn(&str) -> bool) -> bool {
    if matches!(module_name, "macros" | "registry") {
        return false;
    }

    let mut pending_dirs = vec![providers_dir().join(module_name)];
    while let Some(dir) = pending_dirs.pop() {
        for entry in fs::read_dir(&dir)
            .unwrap_or_else(|err| panic!("provider directory {dir:?} should be readable: {err}"))
        {
            let entry = entry.expect("provider directory entry should be readable");
            let path = entry.path();
            let file_type = entry.file_type().expect("file type should be readable");

            if file_type.is_dir() {
                pending_dirs.push(path);
                continue;
            }

            if path.extension().is_some_and(|extension| extension == "rs") {
                let source = fs::read_to_string(&path).unwrap_or_else(|err| {
                    panic!("provider source {path:?} should be readable: {err}")
                });
                if contains_marker(&source) {
                    return true;
                }
            }
        }
    }

    false
}

#[test]
fn lifecycle_classifies_phase0_key_provider_modules() {
    assert_eq!(lifecycle_for("bedrock"), ProviderModuleLifecycle::Wire);
    for (module_name, feature_enabled) in [
        ("vertex_ai", cfg!(feature = "providers-extra")),
        ("azure", cfg!(feature = "providers-extra")),
        ("azure_ai", cfg!(feature = "providers-extra")),
        ("github_copilot", cfg!(feature = "providers-extended")),
        ("cohere", cfg!(feature = "providers-extended")),
        ("fal_ai", cfg!(feature = "providers-extended")),
        ("replicate", cfg!(feature = "providers-extended")),
        ("gemini", cfg!(feature = "providers-extended")),
        ("ollama", cfg!(feature = "providers-extended")),
    ] {
        let expected = if feature_enabled {
            ProviderModuleLifecycle::Wire
        } else {
            ProviderModuleLifecycle::Stub
        };
        assert_eq!(lifecycle_for(module_name), expected, "{module_name}");
    }
}

#[test]
fn lifecycle_wire_entries_match_registry_runtime_modules() {
    let actual = PROVIDER_MODULE_LIFECYCLE
        .iter()
        .filter(|entry| entry.lifecycle == ProviderModuleLifecycle::Wire)
        .map(|entry| entry.module_name)
        .collect::<BTreeSet<_>>();
    let expected = registry_runtime_module_names();

    assert_eq!(actual, expected);
}

#[test]
fn lifecycle_covers_every_provider_directory() {
    let actual = provider_directories();
    let declared = lifecycle_module_names()
        .into_iter()
        .map(str::to_string)
        .collect::<BTreeSet<_>>();

    assert_eq!(actual, declared);
}

#[test]
fn lifecycle_has_no_delete_decisions_without_owner_confirmation() {
    assert!(
        PROVIDER_MODULE_LIFECYCLE
            .iter()
            .all(|entry| entry.lifecycle != ProviderModuleLifecycle::Delete),
        "Delete lifecycle requires explicit owner confirmation"
    );
}

#[test]
fn lifecycle_entries_have_reasons() {
    for entry in PROVIDER_MODULE_LIFECYCLE {
        assert!(
            !entry.reason.trim().is_empty(),
            "{} lifecycle entry must include a reason",
            entry.module_name
        );
    }
}

fn lifecycle_requires_orphan_baseline(lifecycle: ProviderModuleLifecycle) -> bool {
    matches!(
        lifecycle,
        ProviderModuleLifecycle::Stub | ProviderModuleLifecycle::CatalogOnly
    )
}

#[test]
fn lifecycle_blocks_unapproved_orphan_provider_modules() {
    let baseline = orphan_baseline_module_names();
    let disabled_feature_gated_runtime_modules = disabled_feature_gated_runtime_module_names();
    let mut unapproved = Vec::new();

    for entry in PROVIDER_MODULE_LIFECYCLE
        .iter()
        .filter(|entry| lifecycle_requires_orphan_baseline(entry.lifecycle))
    {
        let module_name = entry.module_name;
        if is_registry_runtime_module(module_name) {
            continue;
        }
        if disabled_feature_gated_runtime_modules.contains(module_name) {
            continue;
        }
        if baseline.contains(module_name) {
            continue;
        }
        unapproved.push(module_name.to_string());
    }

    assert!(
        unapproved.is_empty(),
        "unapproved Stub/CatalogOnly provider modules must be wired, deleted, demoted, explicitly gated, or added to the GH837 baseline: {unapproved:?}"
    );
}

#[test]
fn internal_lifecycle_entries_are_fixed_infrastructure_modules() {
    let actual = PROVIDER_MODULE_LIFECYCLE
        .iter()
        .filter(|entry| entry.lifecycle == ProviderModuleLifecycle::Internal)
        .map(|entry| entry.module_name)
        .collect::<BTreeSet<_>>();

    assert_eq!(
        actual,
        BTreeSet::from(["base", "factory", "macros", "registry", "thinking"])
    );
    let internal_provider_impls = actual
        .iter()
        .filter(|module_name| directory_contains_provider_impl_marker(module_name))
        .collect::<Vec<_>>();
    assert!(
        internal_provider_impls.is_empty(),
        "internal lifecycle entries must not contain provider implementation markers: {internal_provider_impls:?}"
    );
}

#[test]
fn orphan_baseline_entries_are_live_and_bounded() {
    let provider_dirs = provider_directories();
    let mut seen = BTreeSet::new();

    for entry in PROVIDER_ORPHAN_BASELINE {
        assert!(
            seen.insert(entry.module_name),
            "{} appears more than once in the orphan baseline",
            entry.module_name
        );
        assert!(
            provider_dirs.contains(entry.module_name),
            "{} baseline entry must reference an existing provider directory",
            entry.module_name
        );
        assert!(
            lifecycle_requires_orphan_baseline(lifecycle_for(entry.module_name)),
            "{} baseline entry must reference a Stub/CatalogOnly provider module",
            entry.module_name
        );
        assert!(
            !is_registry_runtime_module(entry.module_name),
            "{} is natively reachable and should not be in the orphan baseline",
            entry.module_name
        );
        if !directory_contains_provider_impl_marker(entry.module_name) {
            assert_eq!(
                entry.lane, "non-llm-lane",
                "{} markerless provider module must be tracked in the non-LLM lane",
                entry.module_name
            );
        }
        assert!(
            !(entry.lane == "non-llm-lane"
                && directory_declares_chat_capability(entry.module_name)),
            "{} declares ChatCompletion and cannot use the non-LLM lane",
            entry.module_name
        );
        assert!(
            matches!(
                entry.lane,
                "delete-native" | "demote-to-catalog" | "non-llm-lane" | "exempt"
            ),
            "{} baseline entry has unsupported lane {}",
            entry.module_name,
            entry.lane
        );
        assert_eq!(entry.issue, "GH837");
        assert!(
            !entry.owner.trim().is_empty(),
            "{} baseline entry must include an owner",
            entry.module_name
        );
        assert!(
            !entry.expires.trim().is_empty(),
            "{} baseline entry must include an expiry condition",
            entry.module_name
        );
        assert!(
            !entry.reason.trim().is_empty(),
            "{} baseline entry must include a reason",
            entry.module_name
        );
    }
}