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
use std::collections::HashMap;
use std::sync::Mutex;

use serde_json::Value;
use tracing::warn;

use super::SSETransformer;
use crate::core::providers::unified_provider::ProviderError;
use crate::core::types::message::MessageRole;
use crate::core::types::responses::{
    ChatChunk, ChatDelta, ChatStreamChoice, FinishReason, FunctionCallDelta, PromptTokensDetails,
    ToolCallDelta, Usage,
};
use crate::core::types::thinking::ThinkingDelta;

/// Anthropic SSE Transformer
///
/// Handles Anthropic's event-based SSE format with message_start, content_block_delta,
/// message_delta, and message_stop events.
#[derive(Debug)]
pub struct AnthropicTransformer {
    model: String,
    tool_name_map: HashMap<String, String>,
    message_id: Mutex<Option<String>>,
}

impl Clone for AnthropicTransformer {
    fn clone(&self) -> Self {
        Self {
            model: self.model.clone(),
            tool_name_map: self.tool_name_map.clone(),
            message_id: Mutex::new(None),
        }
    }
}

impl AnthropicTransformer {
    pub fn new(model: impl Into<String>) -> Self {
        Self {
            model: model.into(),
            tool_name_map: HashMap::new(),
            message_id: Mutex::new(None),
        }
    }

    pub fn with_tool_name_map(mut self, tool_name_map: HashMap<String, String>) -> Self {
        self.tool_name_map = tool_name_map;
        self
    }

    fn restore_tool_name(&self, name: &str) -> String {
        self.tool_name_map
            .get(name)
            .cloned()
            .unwrap_or_else(|| name.to_string())
    }

    fn set_message_id(&self, message_id: String) {
        let mut guard = match self.message_id.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        *guard = Some(message_id);
    }

    fn current_message_id(&self) -> String {
        let guard = match self.message_id.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        guard
            .as_ref()
            .cloned()
            .unwrap_or_else(|| "anthropic-stream".to_string())
    }

    fn clear_message_id(&self) {
        let mut guard = match self.message_id.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        *guard = None;
    }

    fn parse_anthropic_finish_reason(reason: &str) -> FinishReason {
        match reason {
            "end_turn" => FinishReason::Stop,
            "max_tokens" => FinishReason::Length,
            "tool_use" => FinishReason::ToolCalls,
            "stop_sequence" => FinishReason::StopSequence,
            "refusal" => FinishReason::Refusal,
            "pause_turn" => FinishReason::PauseTurn,
            _ => FinishReason::Stop,
        }
    }

    fn empty_delta() -> ChatDelta {
        ChatDelta {
            role: None,
            content: None,
            thinking: None,
            tool_calls: None,
            function_call: None,
            audio: None,
        }
    }

    fn chunk_with_choice(
        &self,
        created: i64,
        delta: ChatDelta,
        finish_reason: Option<FinishReason>,
        usage: Option<Usage>,
    ) -> ChatChunk {
        ChatChunk {
            id: self.current_message_id(),
            object: "chat.completion.chunk".to_string(),
            created,
            model: self.model.clone(),
            choices: vec![ChatStreamChoice {
                index: 0,
                delta,
                finish_reason,
                logprobs: None,
            }],
            usage,
            system_fingerprint: None,
        }
    }
}

impl SSETransformer for AnthropicTransformer {
    fn provider_name(&self) -> &'static str {
        "anthropic"
    }

    fn transform_chunk(&self, data: &str) -> Result<Option<ChatChunk>, ProviderError> {
        let json: Value = serde_json::from_str(data).map_err(|e| {
            ProviderError::response_parsing(
                "anthropic",
                format!("Failed to parse Anthropic SSE: {}", e),
            )
        })?;

        let event_type = json.get("type").and_then(|v| v.as_str()).unwrap_or("");

        let created = chrono::Utc::now().timestamp();

        match event_type {
            "message_start" => {
                let message_id = json
                    .get("message")
                    .and_then(|m| m.get("id"))
                    .and_then(|v| v.as_str())
                    .unwrap_or("anthropic-stream")
                    .to_string();
                self.set_message_id(message_id.clone());

                Ok(Some(ChatChunk {
                    id: message_id,
                    object: "chat.completion.chunk".to_string(),
                    created,
                    model: self.model.clone(),
                    choices: vec![ChatStreamChoice {
                        index: 0,
                        delta: ChatDelta {
                            role: Some(MessageRole::Assistant),
                            content: None,
                            thinking: None,
                            tool_calls: None,
                            function_call: None,
                            audio: None,
                        },
                        finish_reason: None,
                        logprobs: None,
                    }],
                    usage: None,
                    system_fingerprint: None,
                }))
            }
            "content_block_start" => {
                let index = json.get("index").and_then(|v| v.as_u64()).unwrap_or(0) as u32;
                let content_block = json.get("content_block").ok_or_else(|| {
                    ProviderError::response_parsing(
                        "anthropic",
                        "No content_block in content_block_start".to_string(),
                    )
                })?;
                let block_type = content_block
                    .get("type")
                    .and_then(|v| v.as_str())
                    .unwrap_or("");

                match block_type {
                    "tool_use" => {
                        let id = content_block
                            .get("id")
                            .and_then(|v| v.as_str())
                            .map(str::to_string);
                        let name = content_block
                            .get("name")
                            .and_then(|v| v.as_str())
                            .map(|name| self.restore_tool_name(name));
                        let arguments = content_block.get("input").and_then(|input| {
                            if input.is_null()
                                || input
                                    .as_object()
                                    .map(|object| object.is_empty())
                                    .unwrap_or(false)
                            {
                                None
                            } else {
                                Some(input.to_string())
                            }
                        });

                        let mut delta = Self::empty_delta();
                        delta.tool_calls = Some(vec![ToolCallDelta {
                            index,
                            id,
                            tool_type: Some("function".to_string()),
                            function: Some(FunctionCallDelta { name, arguments }),
                        }]);

                        Ok(Some(self.chunk_with_choice(created, delta, None, None)))
                    }
                    "thinking" | "redacted_thinking" => {
                        let mut delta = Self::empty_delta();
                        delta.thinking = Some(ThinkingDelta::start());
                        Ok(Some(self.chunk_with_choice(created, delta, None, None)))
                    }
                    "text" => Ok(None),
                    _ => {
                        warn!(
                            provider = "anthropic",
                            block_type, "Ignoring unknown Anthropic content block start"
                        );
                        Ok(None)
                    }
                }
            }
            "content_block_delta" => {
                let index = json.get("index").and_then(|v| v.as_u64()).unwrap_or(0) as u32;
                let delta_json = json.get("delta").ok_or_else(|| {
                    ProviderError::response_parsing(
                        "anthropic",
                        "No delta in content_block_delta".to_string(),
                    )
                })?;
                let delta_type = delta_json
                    .get("type")
                    .and_then(|v| v.as_str())
                    .unwrap_or("");

                match delta_type {
                    "text_delta" => {
                        let text = delta_json
                            .get("text")
                            .and_then(|t| t.as_str())
                            .unwrap_or("");
                        let mut delta = Self::empty_delta();
                        delta.content = Some(text.to_string());
                        Ok(Some(self.chunk_with_choice(created, delta, None, None)))
                    }
                    "input_json_delta" => {
                        let partial_json = delta_json
                            .get("partial_json")
                            .and_then(|value| value.as_str())
                            .unwrap_or("");
                        let mut delta = Self::empty_delta();
                        delta.tool_calls = Some(vec![ToolCallDelta {
                            index,
                            id: None,
                            tool_type: Some("function".to_string()),
                            function: Some(FunctionCallDelta {
                                name: None,
                                arguments: Some(partial_json.to_string()),
                            }),
                        }]);
                        Ok(Some(self.chunk_with_choice(created, delta, None, None)))
                    }
                    "thinking_delta" => {
                        let thinking = delta_json
                            .get("thinking")
                            .and_then(|value| value.as_str())
                            .unwrap_or("");
                        let mut delta = Self::empty_delta();
                        delta.thinking = Some(ThinkingDelta::new(thinking));
                        Ok(Some(self.chunk_with_choice(created, delta, None, None)))
                    }
                    "signature_delta" => {
                        let signature = delta_json
                            .get("signature")
                            .and_then(|value| value.as_str())
                            .unwrap_or("");
                        let mut delta = Self::empty_delta();
                        delta.thinking = Some(ThinkingDelta {
                            signature: Some(signature.to_string()),
                            ..Default::default()
                        });
                        Ok(Some(self.chunk_with_choice(created, delta, None, None)))
                    }
                    _ => {
                        warn!(
                            provider = "anthropic",
                            delta_type, "Ignoring unknown Anthropic content block delta"
                        );
                        Ok(None)
                    }
                }
            }
            "message_delta" => {
                let finish_reason = json
                    .get("delta")
                    .and_then(|d| d.get("stop_reason"))
                    .and_then(|r| r.as_str())
                    .map(Self::parse_anthropic_finish_reason);

                let usage = json.get("usage").map(|u| {
                    let input = u.get("input_tokens").and_then(|v| v.as_u64()).unwrap_or(0) as u32;
                    let output =
                        u.get("output_tokens").and_then(|v| v.as_u64()).unwrap_or(0) as u32;
                    let cache_creation_tokens = u
                        .get("cache_creation_input_tokens")
                        .and_then(|v| v.as_u64())
                        .map(|t| t as u32);
                    let cache_read_tokens = u
                        .get("cache_read_input_tokens")
                        .and_then(|v| v.as_u64())
                        .map(|t| t as u32);
                    let prompt_tokens_details =
                        if cache_creation_tokens.is_some() || cache_read_tokens.is_some() {
                            Some(PromptTokensDetails {
                                cached_tokens: cache_read_tokens,
                                cache_creation_tokens,
                                cache_read_tokens,
                                audio_tokens: None,
                            })
                        } else {
                            None
                        };
                    Usage {
                        prompt_tokens: input,
                        completion_tokens: output,
                        total_tokens: input + output,
                        completion_tokens_details: None,
                        prompt_tokens_details,
                        thinking_usage: None,
                    }
                });

                Ok(Some(self.chunk_with_choice(
                    created,
                    Self::empty_delta(),
                    finish_reason,
                    usage,
                )))
            }
            "message_stop" => {
                let message_id = self.current_message_id();
                self.clear_message_id();
                Ok(Some(ChatChunk {
                    id: message_id,
                    object: "chat.completion.chunk".to_string(),
                    created,
                    model: self.model.clone(),
                    choices: vec![],
                    usage: None,
                    system_fingerprint: None,
                }))
            }
            "error" => {
                let msg = json
                    .get("error")
                    .and_then(|e| e.get("message"))
                    .and_then(|m| m.as_str())
                    .unwrap_or("Unknown streaming error");
                Err(ProviderError::streaming_error(
                    "anthropic",
                    "chat",
                    None,
                    None,
                    msg.to_string(),
                ))
            }
            "content_block_stop" | "ping" => Ok(None),
            _ => {
                warn!(
                    provider = "anthropic",
                    event_type, "Ignoring unknown Anthropic SSE event type"
                );
                Ok(None)
            }
        }
    }
}

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

    fn chunk_from_event(t: &AnthropicTransformer, event: Value) -> ChatChunk {
        match t.transform_chunk(&event.to_string()) {
            Ok(Some(chunk)) => chunk,
            Ok(None) => panic!("expected Anthropic SSE event to produce a chunk"),
            Err(error) => panic!("unexpected Anthropic SSE error: {}", error),
        }
    }

    #[test]
    fn test_message_delta_extracts_cache_tokens() {
        let t = AnthropicTransformer::new("claude-3-5-sonnet");
        let event = serde_json::json!({
            "type": "message_delta",
            "delta": {"stop_reason": "end_turn"},
            "usage": {
                "input_tokens": 12,
                "output_tokens": 50,
                "cache_creation_input_tokens": 1000,
                "cache_read_input_tokens": 2000
            }
        });
        let chunk = t.transform_chunk(&event.to_string()).unwrap().unwrap();
        let usage = chunk.usage.as_ref().expect("usage must be present");
        assert_eq!(usage.prompt_tokens, 12);
        assert_eq!(usage.completion_tokens, 50);
        let details = usage
            .prompt_tokens_details
            .as_ref()
            .expect("cache token details must be present");
        assert_eq!(details.cache_creation_tokens, Some(1000));
        assert_eq!(details.cache_read_tokens, Some(2000));
        assert_eq!(details.cached_tokens, Some(2000));
    }

    #[test]
    fn test_message_delta_no_cache_tokens_yields_none_details() {
        let t = AnthropicTransformer::new("claude-3-5-sonnet");
        let event = serde_json::json!({
            "type": "message_delta",
            "delta": {"stop_reason": "end_turn"},
            "usage": {"input_tokens": 12, "output_tokens": 50}
        });
        let chunk = t.transform_chunk(&event.to_string()).unwrap().unwrap();
        let usage = chunk.usage.as_ref().unwrap();
        assert!(usage.prompt_tokens_details.is_none());
    }

    #[test]
    fn test_chunks_after_message_start_keep_message_id() {
        let t = AnthropicTransformer::new("claude-3-5-sonnet");
        let start = serde_json::json!({
            "type": "message_start",
            "message": {"id": "msg_123"}
        });
        let chunk = chunk_from_event(&t, start);
        assert_eq!(chunk.id, "msg_123");

        let delta = serde_json::json!({
            "type": "content_block_delta",
            "delta": {"type": "text_delta", "text": "hello"}
        });
        let chunk = chunk_from_event(&t, delta);
        assert_eq!(chunk.id, "msg_123");

        let message_delta = serde_json::json!({
            "type": "message_delta",
            "delta": {"stop_reason": "end_turn"}
        });
        let chunk = chunk_from_event(&t, message_delta);
        assert_eq!(chunk.id, "msg_123");

        let stop = serde_json::json!({"type": "message_stop"});
        let chunk = chunk_from_event(&t, stop);
        assert_eq!(chunk.id, "msg_123");
    }

    #[test]
    fn test_cloned_transformers_keep_independent_message_ids() {
        let base = AnthropicTransformer::new("claude-3-5-sonnet");
        let stream_a = base.clone();
        let stream_b = base.clone();

        let chunk = chunk_from_event(
            &stream_a,
            serde_json::json!({
                "type": "message_start",
                "message": {"id": "msg_a"}
            }),
        );
        assert_eq!(chunk.id, "msg_a");

        let chunk = chunk_from_event(
            &stream_b,
            serde_json::json!({
                "type": "message_start",
                "message": {"id": "msg_b"}
            }),
        );
        assert_eq!(chunk.id, "msg_b");

        let chunk = chunk_from_event(
            &stream_a,
            serde_json::json!({
                "type": "content_block_delta",
                "delta": {"type": "text_delta", "text": "hello"}
            }),
        );
        assert_eq!(chunk.id, "msg_a");

        let chunk = chunk_from_event(&stream_b, serde_json::json!({"type": "message_stop"}));
        assert_eq!(chunk.id, "msg_b");

        let chunk = chunk_from_event(
            &stream_a,
            serde_json::json!({
                "type": "message_delta",
                "delta": {"stop_reason": "end_turn"}
            }),
        );
        assert_eq!(chunk.id, "msg_a");
    }

    #[test]
    fn issue_761_stream_restores_original_tool_names()
    -> Result<(), crate::core::providers::unified_provider::ProviderError> {
        let t =
            AnthropicTransformer::new("claude-3-5-sonnet").with_tool_name_map(HashMap::from([(
                "weather_lookup".to_string(),
                "weather.lookup".to_string(),
            )]));
        let event = serde_json::json!({
            "type": "content_block_start",
            "index": 0,
            "content_block": {
                "type": "tool_use",
                "id": "toolu_123",
                "name": "weather_lookup",
                "input": {}
            }
        });

        let chunk = t.transform_chunk(&event.to_string())?;
        let name = chunk
            .as_ref()
            .and_then(|chunk| chunk.choices.first())
            .and_then(|choice| choice.delta.tool_calls.as_ref())
            .and_then(|tool_calls| tool_calls.first())
            .and_then(|tool_call| tool_call.function.as_ref())
            .and_then(|function| function.name.as_deref());

        assert_eq!(name, Some("weather.lookup"));
        Ok(())
    }
}