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
//! Stability AI Provider Implementation
//!
//! Main provider implementation for Stability AI image generation.

use futures::Stream;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;

use crate::core::providers::base::{GlobalPoolManager, get_pricing_db};
use crate::core::providers::unified_provider::ProviderError;
use crate::core::traits::{
    error_mapper::trait_def::ErrorMapper, provider::ProviderConfig,
    provider::llm_provider::trait_definition::LLMProvider,
};
use crate::core::types::{
    chat::ChatRequest,
    context::RequestContext,
    health::HealthStatus,
    image::ImageGenerationRequest,
    model::ModelInfo,
    model::ProviderCapability,
    responses::{ChatChunk, ChatResponse, ImageData, ImageGenerationResponse},
};

use super::{StabilityConfig, StabilityErrorMapper, get_stability_registry};

/// Stability AI image generation request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StabilityImageRequest {
    pub prompt: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub negative_prompt: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aspect_ratio: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub seed: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_format: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mode: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub strength: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub style_preset: Option<String>,
}

/// Stability AI image generation response
#[derive(Debug, Clone, Deserialize)]
pub struct StabilityImageResponse {
    pub image: Option<String>,
    pub finish_reason: Option<String>,
    pub seed: Option<u64>,
    #[serde(default)]
    pub errors: Option<Vec<String>>,
}

#[derive(Debug, Clone)]
pub struct StabilityProvider {
    config: StabilityConfig,
    supported_models: Vec<ModelInfo>,
}

impl StabilityProvider {
    /// Create a new Stability AI provider
    pub fn new(config: StabilityConfig) -> Result<Self, ProviderError> {
        config
            .validate()
            .map_err(|e| ProviderError::configuration("stability", e))?;

        let _pool_manager = Arc::new(
            GlobalPoolManager::new()
                .map_err(|e| ProviderError::configuration("stability", e.to_string()))?,
        );
        let supported_models = get_stability_registry().models().to_vec();

        Ok(Self {
            config,
            supported_models,
        })
    }

    /// Create provider from environment variables
    pub fn from_env() -> Result<Self, ProviderError> {
        let config = StabilityConfig::from_env();
        Self::new(config)
    }

    /// Create provider with API key
    pub async fn with_api_key(api_key: impl Into<String>) -> Result<Self, ProviderError> {
        let config = StabilityConfig::with_api_key(api_key);
        Self::new(config)
    }

    /// Transform OpenAI-style image request to Stability request
    fn transform_image_request(&self, request: &ImageGenerationRequest) -> StabilityImageRequest {
        let registry = get_stability_registry();

        // Map size to aspect ratio if provided
        let aspect_ratio = request
            .size
            .as_ref()
            .and_then(|size| registry.size_to_aspect_ratio(size).map(|s| s.to_string()));

        StabilityImageRequest {
            prompt: request.prompt.clone(),
            negative_prompt: None,
            aspect_ratio,
            seed: None,
            output_format: Some("png".to_string()),
            model: request.model.clone(),
            mode: None,
            strength: None,
            style_preset: None,
        }
    }

    /// Transform Stability response to OpenAI-compatible response
    fn transform_image_response(
        &self,
        response: StabilityImageResponse,
    ) -> Result<ImageGenerationResponse, ProviderError> {
        // Check for errors
        if let Some(errors) = &response.errors
            && !errors.is_empty()
        {
            return Err(ProviderError::api_error(
                "stability",
                400,
                errors.join(", "),
            ));
        }

        // Check finish reason
        if let Some(ref reason) = response.finish_reason
            && reason == "CONTENT_FILTERED"
        {
            return Err(ProviderError::content_filtered(
                "stability",
                "Content was filtered by Stability AI safety systems",
                None,
                Some(false),
            ));
        }

        let mut data = Vec::new();

        if let Some(image_b64) = response.image {
            data.push(ImageData {
                url: None,
                b64_json: Some(image_b64),
                revised_prompt: None,
            });
        }

        Ok(ImageGenerationResponse {
            created: chrono::Utc::now().timestamp() as u64,
            data,
        })
    }

    /// Get the API endpoint for a model
    fn get_endpoint(&self, model: Option<&str>) -> String {
        let registry = get_stability_registry();
        let model_name = model.unwrap_or("sd3");
        format!(
            "{}{}",
            self.config
                .base
                .api_base
                .as_deref()
                .unwrap_or("https://api.stability.ai"),
            registry.get_endpoint(model_name)
        )
    }
}

impl LLMProvider for StabilityProvider {
    fn name(&self) -> &'static str {
        "stability"
    }

    fn capabilities(&self) -> &'static [ProviderCapability] {
        &[ProviderCapability::ImageGeneration]
    }

    fn models(&self) -> &[ModelInfo] {
        &self.supported_models
    }

    fn get_supported_openai_params(&self, _model: &str) -> &'static [&'static str] {
        &["n", "size", "response_format", "model"]
    }

    async fn map_openai_params(
        &self,
        params: HashMap<String, Value>,
        _model: &str,
    ) -> Result<HashMap<String, Value>, ProviderError> {
        let mut mapped = HashMap::new();
        let registry = get_stability_registry();

        for (key, value) in params {
            match key.as_str() {
                "size" => {
                    if let Some(size_str) = value.as_str()
                        && let Some(ratio) = registry.size_to_aspect_ratio(size_str)
                    {
                        mapped.insert("aspect_ratio".to_string(), Value::String(ratio.to_string()));
                    }
                }
                "n" => {
                    // Store n for later (Stability returns 1 image per request)
                    mapped.insert("_n".to_string(), value);
                }
                "response_format" => {
                    // Store for response handling
                    mapped.insert("_response_format".to_string(), value);
                }
                _ => {
                    mapped.insert(key, value);
                }
            }
        }

        Ok(mapped)
    }

    async fn transform_request(
        &self,
        _request: ChatRequest,
        _context: RequestContext,
    ) -> Result<Value, ProviderError> {
        // Stability AI is primarily for image generation, not chat
        Err(ProviderError::not_supported(
            "stability",
            "Chat completion is not supported by Stability AI. Use image_generation instead.",
        ))
    }

    async fn transform_response(
        &self,
        _raw_response: &[u8],
        _model: &str,
        _request_id: &str,
    ) -> Result<ChatResponse, ProviderError> {
        Err(ProviderError::not_supported(
            "stability",
            "Chat completion is not supported by Stability AI",
        ))
    }

    fn get_error_mapper(&self) -> Box<dyn ErrorMapper<ProviderError>> {
        Box::new(StabilityErrorMapper)
    }

    async fn chat_completion(
        &self,
        _request: ChatRequest,
        _context: RequestContext,
    ) -> Result<ChatResponse, ProviderError> {
        Err(ProviderError::not_supported(
            "stability",
            "Chat completion is not supported by Stability AI. Use image_generation instead.",
        ))
    }

    async fn chat_completion_stream(
        &self,
        _request: ChatRequest,
        _context: RequestContext,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<ChatChunk, ProviderError>> + Send>>, ProviderError>
    {
        Err(ProviderError::not_supported(
            "stability",
            "Streaming is not supported by Stability AI",
        ))
    }

    async fn image_generation(
        &self,
        request: ImageGenerationRequest,
        _context: RequestContext,
    ) -> Result<ImageGenerationResponse, ProviderError> {
        let url = self.get_endpoint(request.model.as_deref());
        let stability_request = self.transform_image_request(&request);

        // Stability AI uses multipart/form-data for image generation
        // Build form data
        let api_key = self
            .config
            .base
            .api_key
            .as_ref()
            .ok_or_else(|| ProviderError::authentication("stability", "API key is required"))?;

        let client = reqwest::Client::new();
        let mut form = reqwest::multipart::Form::new()
            .text("prompt", stability_request.prompt.clone())
            .text(
                "output_format",
                stability_request
                    .output_format
                    .unwrap_or_else(|| "png".to_string()),
            );

        if let Some(aspect_ratio) = &stability_request.aspect_ratio {
            form = form.text("aspect_ratio", aspect_ratio.clone());
        }
        if let Some(negative_prompt) = &stability_request.negative_prompt {
            form = form.text("negative_prompt", negative_prompt.clone());
        }
        if let Some(seed) = stability_request.seed {
            form = form.text("seed", seed.to_string());
        }
        if let Some(model) = &stability_request.model {
            form = form.text("model", model.clone());
        }
        if let Some(style_preset) = &stability_request.style_preset {
            form = form.text("style_preset", style_preset.clone());
        }

        let response = client
            .post(&url)
            .header("Authorization", format!("Bearer {}", api_key))
            .header("Accept", "application/json")
            .multipart(form)
            .send()
            .await
            .map_err(|e| ProviderError::network("stability", e.to_string()))?;

        let status = response.status();
        if !status.is_success() {
            let error_text = response
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            let mapper = self.get_error_mapper();
            return Err(mapper.map_http_error(status.as_u16(), &error_text));
        }

        let response_body: StabilityImageResponse = response
            .json()
            .await
            .map_err(|e| ProviderError::response_parsing("stability", e.to_string()))?;

        self.transform_image_response(response_body)
    }

    async fn health_check(&self) -> HealthStatus {
        if self.config.base.api_key.is_some() {
            HealthStatus::Healthy
        } else {
            HealthStatus::Unhealthy
        }
    }

    async fn calculate_cost(
        &self,
        model: &str,
        input_tokens: u32,
        output_tokens: u32,
    ) -> Result<f64, ProviderError> {
        // Stability AI pricing is per image, not per token
        // Use the pricing database for estimation
        let usage = crate::core::providers::base::pricing::Usage {
            prompt_tokens: input_tokens,
            completion_tokens: output_tokens,
            total_tokens: input_tokens + output_tokens,
            reasoning_tokens: None,
        };

        Ok(get_pricing_db().calculate(model, &usage))
    }
}

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

    #[test]
    fn test_stability_provider_name() {
        let config = StabilityConfig::with_api_key("test-key");
        let provider = StabilityProvider::new(config).unwrap();
        assert_eq!(provider.name(), "stability");
    }

    #[test]
    fn test_stability_provider_capabilities() {
        let config = StabilityConfig::with_api_key("test-key");
        let provider = StabilityProvider::new(config).unwrap();
        let caps = provider.capabilities();
        assert!(caps.contains(&ProviderCapability::ImageGeneration));
    }

    #[test]
    fn test_stability_provider_models() {
        let config = StabilityConfig::with_api_key("test-key");
        let provider = StabilityProvider::new(config).unwrap();
        let models = provider.models();
        assert!(!models.is_empty());
    }

    #[test]
    fn test_transform_image_request() {
        let config = StabilityConfig::with_api_key("test-key");
        let provider = StabilityProvider::new(config).unwrap();

        let request = ImageGenerationRequest {
            prompt: "A beautiful sunset".to_string(),
            model: Some("sd3".to_string()),
            n: Some(1),
            size: Some("1024x1024".to_string()),
            quality: None,
            response_format: None,
            style: None,
            user: None,
        };

        let stability_request = provider.transform_image_request(&request);
        assert_eq!(stability_request.prompt, "A beautiful sunset");
        assert_eq!(stability_request.aspect_ratio, Some("1:1".to_string()));
    }

    #[test]
    fn test_transform_image_request_landscape() {
        let config = StabilityConfig::with_api_key("test-key");
        let provider = StabilityProvider::new(config).unwrap();

        let request = ImageGenerationRequest {
            prompt: "A mountain landscape".to_string(),
            model: None,
            n: None,
            size: Some("1792x1024".to_string()),
            quality: None,
            response_format: None,
            style: None,
            user: None,
        };

        let stability_request = provider.transform_image_request(&request);
        assert_eq!(stability_request.aspect_ratio, Some("16:9".to_string()));
    }

    #[test]
    fn test_transform_image_response_success() {
        let config = StabilityConfig::with_api_key("test-key");
        let provider = StabilityProvider::new(config).unwrap();

        let response = StabilityImageResponse {
            image: Some("base64encodedimage".to_string()),
            finish_reason: Some("SUCCESS".to_string()),
            seed: Some(12345),
            errors: None,
        };

        let result = provider.transform_image_response(response);
        assert!(result.is_ok());

        let gen_response = result.unwrap();
        assert_eq!(gen_response.data.len(), 1);
        assert!(gen_response.data[0].b64_json.is_some());
    }

    #[test]
    fn test_transform_image_response_content_filtered() {
        let config = StabilityConfig::with_api_key("test-key");
        let provider = StabilityProvider::new(config).unwrap();

        let response = StabilityImageResponse {
            image: None,
            finish_reason: Some("CONTENT_FILTERED".to_string()),
            seed: None,
            errors: None,
        };

        let result = provider.transform_image_response(response);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            ProviderError::ContentFiltered { .. }
        ));
    }

    #[test]
    fn test_transform_image_response_with_errors() {
        let config = StabilityConfig::with_api_key("test-key");
        let provider = StabilityProvider::new(config).unwrap();

        let response = StabilityImageResponse {
            image: None,
            finish_reason: None,
            seed: None,
            errors: Some(vec!["Invalid prompt".to_string()]),
        };

        let result = provider.transform_image_response(response);
        assert!(result.is_err());
    }

    #[test]
    fn test_get_endpoint_sd3() {
        let config = StabilityConfig::with_api_key("test-key");
        let provider = StabilityProvider::new(config).unwrap();

        let endpoint = provider.get_endpoint(Some("sd3"));
        assert!(endpoint.contains("/v2beta/stable-image/generate/sd3"));
    }

    #[test]
    fn test_get_endpoint_ultra() {
        let config = StabilityConfig::with_api_key("test-key");
        let provider = StabilityProvider::new(config).unwrap();

        let endpoint = provider.get_endpoint(Some("stable-image-ultra"));
        assert!(endpoint.contains("/v2beta/stable-image/generate/ultra"));
    }

    #[test]
    fn test_get_supported_openai_params() {
        let config = StabilityConfig::with_api_key("test-key");
        let provider = StabilityProvider::new(config).unwrap();

        let params = provider.get_supported_openai_params("sd3");
        assert!(params.contains(&"size"));
        assert!(params.contains(&"n"));
    }

    #[tokio::test]
    async fn test_chat_completion_not_supported() {
        let config = StabilityConfig::with_api_key("test-key");
        let provider = StabilityProvider::new(config).unwrap();

        let request = ChatRequest {
            model: "sd3".to_string(),
            messages: vec![],
            ..Default::default()
        };

        let context = RequestContext::default();
        let result = provider.chat_completion(request, context).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            ProviderError::NotSupported { .. }
        ));
    }

    #[test]
    fn test_health_check_with_api_key() {
        let config = StabilityConfig::with_api_key("test-key");
        let provider = StabilityProvider::new(config).unwrap();

        // Create a runtime for the async test
        let rt = tokio::runtime::Runtime::new().unwrap();
        let health = rt.block_on(provider.health_check());
        assert_eq!(health, HealthStatus::Healthy);
    }
}