litellm-rs 0.8.2

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
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;

use crate::core::providers::unified_provider::ProviderError;
use crate::core::types::anthropic_continuation::{
    AnthropicRedactedData, AnthropicSignature, AnthropicThinkingBlock,
};

enum ActiveThinkingBlock {
    Thinking { thinking: String, signature: String },
    Redacted { data: AnthropicRedactedData },
}

#[derive(Clone, Copy)]
pub(super) enum ActiveContentBlock {
    Text,
    ToolUse,
    Ignored,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum DeltaDisposition {
    Emit,
    Ignore,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum MessagePhase {
    Open,
    PendingStop,
    Stopped,
}

impl ActiveContentBlock {
    fn kind(self) -> &'static str {
        match self {
            Self::Text => "text",
            Self::ToolUse => "tool_use",
            Self::Ignored => "future",
        }
    }

    fn accepts(self, delta_type: &str) -> bool {
        match self {
            Self::Text => matches!(delta_type, "text_delta" | "citations_delta"),
            Self::ToolUse => delta_type == "input_json_delta",
            Self::Ignored => true,
        }
    }
}

impl ActiveThinkingBlock {
    fn kind(&self) -> &'static str {
        match self {
            Self::Thinking { .. } => "thinking",
            Self::Redacted { .. } => "redacted_thinking",
        }
    }
}

pub(super) struct AnthropicThinkingStreamState {
    active: BTreeMap<u32, ActiveThinkingBlock>,
    active_content: BTreeMap<u32, ActiveContentBlock>,
    pub(super) completed: Vec<(u32, AnthropicThinkingBlock)>,
    completed_indexes: BTreeSet<u32>,
    message_phase: MessagePhase,
}

impl Default for AnthropicThinkingStreamState {
    fn default() -> Self {
        Self {
            active: BTreeMap::new(),
            active_content: BTreeMap::new(),
            completed: Vec::new(),
            completed_indexes: BTreeSet::new(),
            // Some compatible streams omit message_start. Preserve that
            // established first-message behavior while still making an
            // observed message_stop terminal until the next message_start.
            message_phase: MessagePhase::Open,
        }
    }
}

impl fmt::Debug for AnthropicThinkingStreamState {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("AnthropicThinkingStreamState")
            .field(
                "active_thinking_indexes",
                &self.active.keys().collect::<Vec<_>>(),
            )
            .field(
                "active_content_indexes",
                &self.active_content.keys().collect::<Vec<_>>(),
            )
            .field("completed_count", &self.completed.len())
            .field("message_phase", &self.message_phase)
            .finish()
    }
}

impl AnthropicThinkingStreamState {
    pub(super) fn begin_message(&mut self) -> Result<(), ProviderError> {
        if self.message_phase == MessagePhase::PendingStop {
            return Err(lifecycle_error(
                0,
                "message_start after terminal message_delta requires message_stop first",
            ));
        }
        self.ensure_complete("message_start")?;
        self.active_content.clear();
        self.completed.clear();
        self.completed_indexes.clear();
        self.message_phase = MessagePhase::Open;
        Ok(())
    }

    pub(super) fn end_message(&mut self) -> Result<(), ProviderError> {
        if self.message_phase == MessagePhase::Stopped {
            return Err(lifecycle_error(
                0,
                "message_stop after message_stop requires a new message_start",
            ));
        }
        self.ensure_complete("message_stop")?;
        self.message_phase = MessagePhase::Stopped;
        Ok(())
    }

    pub(super) fn begin_pending_stop(&mut self) -> Result<(), ProviderError> {
        self.ensure_message_open(0, "message_delta")?;
        self.ensure_complete("message_delta stop_reason")?;
        self.message_phase = MessagePhase::PendingStop;
        Ok(())
    }

    pub(super) fn begin_content(
        &mut self,
        index: u32,
        block: ActiveContentBlock,
    ) -> Result<(), ProviderError> {
        self.ensure_message_open(index, "content_block_start")?;
        self.ensure_index_available(index)?;
        self.active_content.insert(index, block);
        Ok(())
    }

    pub(super) fn begin_thinking(
        &mut self,
        index: u32,
        thinking: &str,
        signature: &str,
    ) -> Result<(), ProviderError> {
        self.insert(
            index,
            ActiveThinkingBlock::Thinking {
                thinking: thinking.to_string(),
                signature: signature.to_string(),
            },
        )
    }

    pub(super) fn begin_redacted(&mut self, index: u32, data: &str) -> Result<(), ProviderError> {
        let data = AnthropicRedactedData::try_from(data).map_err(|_| {
            lifecycle_error(
                index,
                format!("redacted_thinking block at index {index} has empty data"),
            )
        })?;
        self.insert(index, ActiveThinkingBlock::Redacted { data })
    }

    pub(super) fn append_thinking(
        &mut self,
        index: u32,
        fragment: &str,
    ) -> Result<(), ProviderError> {
        match self.active.get_mut(&index) {
            Some(ActiveThinkingBlock::Thinking {
                thinking,
                signature,
            }) if signature.is_empty() => {
                thinking.push_str(fragment);
                Ok(())
            }
            Some(ActiveThinkingBlock::Thinking { .. }) => Err(lifecycle_error(
                index,
                format!("thinking delta for block at index {index} arrived after its signature"),
            )),
            Some(block) => Err(lifecycle_error(
                index,
                format!("thinking delta for {} block at index {index}", block.kind()),
            )),
            None => Err(lifecycle_error(
                index,
                format!("thinking delta for inactive block at index {index}"),
            )),
        }
    }

    pub(super) fn append_signature(
        &mut self,
        index: u32,
        fragment: &str,
    ) -> Result<(), ProviderError> {
        if fragment.is_empty() {
            return Err(lifecycle_error(
                index,
                format!("thinking block at index {index} received an empty signature"),
            ));
        }
        match self.active.get_mut(&index) {
            Some(ActiveThinkingBlock::Thinking { signature, .. }) => {
                signature.push_str(fragment);
                Ok(())
            }
            Some(block) => Err(lifecycle_error(
                index,
                format!(
                    "signature delta for {} block at index {index}",
                    block.kind()
                ),
            )),
            None => Err(lifecycle_error(
                index,
                format!("signature delta for inactive block at index {index}"),
            )),
        }
    }

    pub(super) fn complete(&mut self, index: u32) -> Result<bool, ProviderError> {
        self.ensure_message_open(index, "content_block_stop")?;
        let Some(active) = self.active.get(&index) else {
            if self.active_content.remove(&index).is_some() {
                self.completed_indexes.insert(index);
                return Ok(false);
            }
            let status = if self.completed_indexes.contains(&index) {
                "completed"
            } else {
                "inactive"
            };
            return Err(lifecycle_error(
                index,
                format!("content_block_stop for {status} index {index}"),
            ));
        };
        let completed = match active {
            ActiveThinkingBlock::Thinking {
                thinking,
                signature,
            } => AnthropicThinkingBlock::Thinking {
                thinking: thinking.clone(),
                signature: AnthropicSignature::try_from(signature.as_str()).map_err(|_| {
                    lifecycle_error(
                        index,
                        format!("thinking block at index {index} is missing its signature"),
                    )
                })?,
            },
            ActiveThinkingBlock::Redacted { data } => {
                AnthropicThinkingBlock::RedactedThinking { data: data.clone() }
            }
        };
        self.active.remove(&index);
        self.completed.push((index, completed));
        self.completed_indexes.insert(index);
        Ok(true)
    }

    fn insert(&mut self, index: u32, block: ActiveThinkingBlock) -> Result<(), ProviderError> {
        self.ensure_message_open(index, "content_block_start")?;
        self.ensure_index_available(index)?;
        self.active.insert(index, block);
        Ok(())
    }

    fn ensure_index_available(&self, index: u32) -> Result<(), ProviderError> {
        if let Some(existing) = self.active.get(&index) {
            return Err(lifecycle_error(
                index,
                format!(
                    "duplicate {} block at active index {index}",
                    existing.kind()
                ),
            ));
        }
        if let Some(existing) = self.active_content.get(&index) {
            return Err(lifecycle_error(
                index,
                format!(
                    "duplicate {} block at active index {index}",
                    existing.kind()
                ),
            ));
        }
        if self.completed_indexes.contains(&index) {
            return Err(lifecycle_error(
                index,
                format!("content block index {index} was already completed in this message"),
            ));
        }
        Ok(())
    }

    pub(super) fn validate_delta_kind(
        &self,
        index: u32,
        delta_type: &str,
    ) -> Result<DeltaDisposition, ProviderError> {
        self.ensure_message_open(index, "content_block_delta")?;
        if let Some(block) = self.active.get(&index) {
            if matches!(block, ActiveThinkingBlock::Thinking { .. })
                && matches!(delta_type, "thinking_delta" | "signature_delta")
            {
                return Ok(DeltaDisposition::Emit);
            }
            return Err(lifecycle_error(
                index,
                format!(
                    "{delta_type} delta is invalid for {} block at index {index}",
                    block.kind()
                ),
            ));
        }
        if let Some(block) = self.active_content.get(&index) {
            if block.accepts(delta_type) {
                return Ok(if matches!(block, ActiveContentBlock::Ignored) {
                    DeltaDisposition::Ignore
                } else {
                    DeltaDisposition::Emit
                });
            }
            return Err(lifecycle_error(
                index,
                format!(
                    "{delta_type} delta is invalid for {} block at index {index}",
                    block.kind()
                ),
            ));
        }
        if self.completed_indexes.contains(&index) {
            return Err(lifecycle_error(
                index,
                format!(
                    "{delta_type} delta for content block index {index} arrived after it completed"
                ),
            ));
        }
        Err(lifecycle_error(
            index,
            format!("{delta_type} delta for inactive block index {index}"),
        ))
    }

    pub(super) fn ensure_message_open(&self, index: u32, event: &str) -> Result<(), ProviderError> {
        match self.message_phase {
            MessagePhase::Open => Ok(()),
            MessagePhase::PendingStop => Err(lifecycle_error(
                index,
                format!(
                    "{event} after terminal message_delta requires message_stop before content resumes"
                ),
            )),
            MessagePhase::Stopped => Err(lifecycle_error(
                index,
                format!(
                    "{event} after message_stop requires a new message_start before content resumes"
                ),
            )),
        }
    }

    pub(super) fn ensure_complete(&self, boundary: &str) -> Result<(), ProviderError> {
        if let Some((index, block)) = self.active_content.first_key_value() {
            return Err(lifecycle_error(
                *index,
                format!(
                    "{} block at index {index} is incomplete at {boundary}: missing content_block_stop",
                    block.kind()
                ),
            ));
        }
        let Some((index, block)) = self.active.first_key_value() else {
            return Ok(());
        };
        let detail = match block {
            ActiveThinkingBlock::Thinking { signature, .. } if signature.is_empty() => {
                "missing its signature"
            }
            _ => "missing content_block_stop",
        };
        Err(lifecycle_error(
            *index,
            format!(
                "{} block at index {index} is incomplete at {boundary}: {detail}",
                block.kind()
            ),
        ))
    }

    pub(super) fn ensure_stream_complete(&self) -> Result<(), ProviderError> {
        self.ensure_complete("stream termination")?;
        if self.message_phase == MessagePhase::PendingStop {
            return Err(lifecycle_error(
                0,
                "stream ended after terminal message_delta without message_stop",
            ));
        }
        Ok(())
    }
}

fn lifecycle_error(index: u32, message: impl Into<String>) -> ProviderError {
    ProviderError::streaming_error(
        "anthropic",
        "chat.thinking",
        Some(u64::from(index)),
        None,
        message,
    )
}