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
use std::collections::BTreeMap;
use std::sync::Arc;

use bytes::Bytes;
use tokio::sync::mpsc;

use crate::core::guardrails::{CheckResult, GuardrailEngine};

const CONTEXT_OVERLAP_CHARS: usize = 4096;
const MAX_PENDING_BYTES: usize = 1024 * 1024;
const MAX_SURFACES: usize = 128;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum StreamGuardrailError {
    Violation,
    Execution,
}

impl StreamGuardrailError {
    pub(super) fn error_type(self) -> &'static str {
        match self {
            Self::Violation => "content_policy_error",
            Self::Execution => "server_error",
        }
    }

    pub(super) fn code(self) -> &'static str {
        match self {
            Self::Violation => "guardrail_violation",
            Self::Execution => "guardrail_error",
        }
    }

    pub(super) fn message(self) -> &'static str {
        match self {
            Self::Violation => "Response blocked by output guardrails",
            Self::Execution => "Output guardrail check failed",
        }
    }
}

#[derive(Default)]
struct SurfaceState {
    context: String,
    chars_since_check: usize,
}

pub(super) struct StreamOutputGuardrail {
    engine: Arc<GuardrailEngine>,
    active: bool,
    check_chars: usize,
    surfaces: BTreeMap<u32, SurfaceState>,
    pending: Vec<Bytes>,
    pending_bytes: usize,
}

impl StreamOutputGuardrail {
    pub(super) fn new(engine: Arc<GuardrailEngine>) -> Self {
        let config = engine.config();
        Self {
            active: engine.is_enabled() && config.check_output,
            check_chars: config.stream_output_check_chars,
            engine,
            surfaces: BTreeMap::new(),
            pending: Vec::new(),
            pending_bytes: 0,
        }
    }

    pub(super) async fn push(
        &mut self,
        text: &str,
        event: Bytes,
    ) -> Result<Vec<Bytes>, StreamGuardrailError> {
        self.push_many(vec![(0, text.to_string())], event).await
    }

    pub(super) async fn push_many(
        &mut self,
        deltas: Vec<(u32, String)>,
        event: Bytes,
    ) -> Result<Vec<Bytes>, StreamGuardrailError> {
        if !self.active {
            return Ok(vec![event]);
        }

        if deltas.iter().all(|(_, text)| text.is_empty()) {
            if self.pending.is_empty() {
                return Ok(vec![event]);
            }
            let mut released = self.finish().await?;
            released.push(event);
            return Ok(released);
        }

        if event.len() > MAX_PENDING_BYTES {
            return Err(StreamGuardrailError::Execution);
        }
        let mut released = if self.pending_bytes.saturating_add(event.len()) > MAX_PENDING_BYTES {
            self.finish().await?
        } else {
            Vec::new()
        };
        let mut crossed_window = false;
        for (surface, text) in deltas {
            crossed_window |= self.append_and_check(surface, &text).await?;
        }
        self.pending_bytes = self.pending_bytes.saturating_add(event.len());
        self.pending.push(event);

        if crossed_window {
            released.extend(self.finish().await?);
            return Ok(released);
        }

        if !self
            .surfaces
            .values()
            .all(|state| state.chars_since_check == 0)
            && self.pending_bytes < MAX_PENDING_BYTES
        {
            return Ok(released);
        }
        if self
            .surfaces
            .values()
            .any(|state| state.chars_since_check > 0)
        {
            released.extend(self.finish().await?);
            return Ok(released);
        }
        released.extend(self.release_pending()?);
        Ok(released)
    }

    pub(super) async fn finish(&mut self) -> Result<Vec<Bytes>, StreamGuardrailError> {
        if self.active {
            let dirty = self
                .surfaces
                .iter()
                .filter_map(|(surface, state)| (state.chars_since_check > 0).then_some(*surface))
                .collect::<Vec<_>>();
            for surface in dirty {
                self.check_surface(surface).await?;
            }
        }
        self.release_pending()
    }

    pub(super) async fn push_many_until_closed(
        &mut self,
        tx: &mpsc::Sender<Bytes>,
        deltas: Vec<(u32, String)>,
        event: Bytes,
    ) -> Result<Option<Vec<Bytes>>, StreamGuardrailError> {
        tokio::select! {
            biased;
            _ = tx.closed() => Ok(None),
            result = self.push_many(deltas, event) => result.map(Some),
        }
    }

    pub(super) async fn push_until_closed(
        &mut self,
        tx: &mpsc::Sender<Bytes>,
        text: &str,
        event: Bytes,
    ) -> Result<Option<Vec<Bytes>>, StreamGuardrailError> {
        tokio::select! {
            biased;
            _ = tx.closed() => Ok(None),
            result = self.push(text, event) => result.map(Some),
        }
    }

    pub(super) async fn finish_until_closed(
        &mut self,
        tx: &mpsc::Sender<Bytes>,
    ) -> Result<Option<Vec<Bytes>>, StreamGuardrailError> {
        tokio::select! {
            biased;
            _ = tx.closed() => Ok(None),
            result = self.finish() => result.map(Some),
        }
    }

    pub(super) async fn flush_to_until_closed(
        &mut self,
        tx: &mpsc::Sender<Bytes>,
    ) -> Result<Option<()>, StreamGuardrailError> {
        let Some(pending) = self.finish_until_closed(tx).await? else {
            return Ok(None);
        };
        for event in pending {
            let sent = tokio::select! {
                biased;
                _ = tx.closed() => return Ok(None),
                sent = tx.send(event) => sent,
            };
            if sent.is_err() {
                return Ok(None);
            }
        }
        Ok(Some(()))
    }

    async fn append_and_check(
        &mut self,
        surface: u32,
        text: &str,
    ) -> Result<bool, StreamGuardrailError> {
        if !text.is_empty() && !self.surfaces.contains_key(&surface) {
            if self.surfaces.len() >= MAX_SURFACES {
                return Err(StreamGuardrailError::Execution);
            }
            self.surfaces.insert(surface, SurfaceState::default());
        }
        let mut crossed_window = false;
        let mut start = 0;
        while start < text.len() {
            let chars_since_check = self
                .surfaces
                .get(&surface)
                .ok_or(StreamGuardrailError::Execution)?
                .chars_since_check;
            let remaining = self.check_chars.saturating_sub(chars_since_check).max(1);
            let end = prefix_end(text, start, remaining);
            let should_check = {
                let state = self
                    .surfaces
                    .get_mut(&surface)
                    .ok_or(StreamGuardrailError::Execution)?;
                state.context.push_str(&text[start..end]);
                state.chars_since_check += text[start..end].chars().count();
                state.chars_since_check >= self.check_chars
            };
            start = end;
            if should_check {
                self.check_surface(surface).await?;
                crossed_window = true;
            }
        }
        Ok(crossed_window)
    }

    async fn check_surface(&mut self, surface: u32) -> Result<(), StreamGuardrailError> {
        let content = self
            .surfaces
            .get(&surface)
            .map(|state| state.context.as_str())
            .unwrap_or_default();
        let result = self
            .engine
            .check_output(content)
            .await
            .map_err(|_| StreamGuardrailError::Execution)?;
        enforce(result)?;

        if let Some(state) = self.surfaces.get_mut(&surface) {
            state.chars_since_check = 0;
            retain_tail_chars(&mut state.context, CONTEXT_OVERLAP_CHARS);
        }
        Ok(())
    }

    fn release_pending(&mut self) -> Result<Vec<Bytes>, StreamGuardrailError> {
        self.pending_bytes = 0;
        Ok(std::mem::take(&mut self.pending))
    }
}

fn retain_tail_chars(text: &mut String, max_chars: usize) {
    let count = text.chars().count();
    if count > max_chars {
        let keep_from = text
            .char_indices()
            .nth(count - max_chars)
            .map_or(text.len(), |(offset, _)| offset);
        text.drain(..keep_from);
    }
}

fn prefix_end(text: &str, start: usize, max_chars: usize) -> usize {
    text[start..]
        .char_indices()
        .nth(max_chars)
        .map_or(text.len(), |(offset, _)| start + offset)
}

fn enforce(result: CheckResult) -> Result<(), StreamGuardrailError> {
    if result.is_blocked() {
        Err(StreamGuardrailError::Violation)
    } else if result.is_modified() {
        Err(StreamGuardrailError::Execution)
    } else {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::models::gateway::GatewayConfig;

    fn guardrail(check_chars: usize, enabled: bool) -> StreamOutputGuardrail {
        let mut config = GatewayConfig::default().guardrails;
        config.enabled = enabled;
        config.stream_output_check_chars = check_chars;
        let engine = GuardrailEngine::new(config).expect("test guardrails should compile");
        StreamOutputGuardrail::new(Arc::new(engine))
    }

    #[tokio::test]
    async fn safe_event_is_released_after_reaching_the_window() {
        let mut guardrail = guardrail(4, true);
        let event = Bytes::from_static(b"safe-event");

        let released = guardrail
            .push("safe", event.clone())
            .await
            .expect("safe output should pass");

        assert_eq!(released, vec![event]);
        assert!(guardrail.finish().await.unwrap().is_empty());
    }

    #[tokio::test]
    async fn event_is_released_when_a_window_is_crossed_with_a_remainder() {
        let mut guardrail = guardrail(4, true);
        let event = Bytes::from_static(b"safe-event");

        assert_eq!(
            guardrail.push("safe!", event.clone()).await.unwrap(),
            vec![event]
        );
        assert!(guardrail.finish().await.unwrap().is_empty());
    }

    #[tokio::test]
    async fn split_violation_is_blocked_before_pending_events_are_released() {
        let mut guardrail = guardrail(256, true);

        assert!(
            guardrail
                .push("System ", Bytes::from_static(b"first"))
                .await
                .unwrap()
                .is_empty()
        );
        assert!(
            guardrail
                .push("prompt: hidden policy", Bytes::from_static(b"second"))
                .await
                .unwrap()
                .is_empty()
        );
        assert_eq!(
            guardrail.finish().await.unwrap_err(),
            StreamGuardrailError::Violation
        );
    }

    #[tokio::test]
    async fn disabled_guardrails_do_not_buffer_events() {
        let mut guardrail = guardrail(256, false);
        let event = Bytes::from_static(b"event");

        assert_eq!(
            guardrail
                .push("System prompt: hidden policy", event.clone())
                .await
                .unwrap(),
            vec![event]
        );
    }

    #[tokio::test]
    async fn non_text_event_cannot_overtake_pending_text() {
        let mut guardrail = guardrail(256, true);
        let text = Bytes::from_static(b"text");
        let terminal = Bytes::from_static(b"terminal");

        assert!(
            guardrail
                .push("safe", text.clone())
                .await
                .unwrap()
                .is_empty()
        );
        assert_eq!(
            guardrail.push("", terminal.clone()).await.unwrap(),
            vec![text, terminal]
        );
        assert!(guardrail.finish().await.unwrap().is_empty());
    }

    #[tokio::test]
    async fn interleaved_choices_keep_independent_detection_contexts() {
        let mut guardrail = guardrail(256, true);

        assert!(
            guardrail
                .push_many(
                    vec![(0, "System ".to_string()), (1, "safe ".to_string())],
                    Bytes::from_static(b"first"),
                )
                .await
                .unwrap()
                .is_empty()
        );
        assert!(
            guardrail
                .push_many(
                    vec![(1, "text".to_string()), (0, "prompt: hidden".to_string())],
                    Bytes::from_static(b"second"),
                )
                .await
                .unwrap()
                .is_empty()
        );
        assert_eq!(
            guardrail.finish().await.unwrap_err(),
            StreamGuardrailError::Violation
        );
    }

    #[tokio::test]
    async fn rolling_context_is_bounded_and_oversized_events_fail_closed() {
        let mut guardrail = guardrail(4096, true);
        let oversized_event = Bytes::from(vec![b'x'; MAX_PENDING_BYTES + 1]);

        assert_eq!(
            guardrail
                .push("safe", oversized_event.clone())
                .await
                .unwrap_err(),
            StreamGuardrailError::Execution
        );
        assert!(guardrail.pending.is_empty());

        guardrail
            .push(&"a".repeat(9000), Bytes::from_static(b"long"))
            .await
            .unwrap();
        guardrail.finish().await.unwrap();
        assert!(
            guardrail
                .surfaces
                .values()
                .all(|state| state.context.chars().count() <= CONTEXT_OVERLAP_CHARS)
        );
    }

    #[tokio::test]
    async fn surface_count_is_bounded() {
        let mut guardrail = guardrail(4096, true);
        let deltas = (0..=MAX_SURFACES as u32)
            .map(|surface| (surface, "x".to_string()))
            .collect();

        assert_eq!(
            guardrail
                .push_many(deltas, Bytes::from_static(b"event"))
                .await
                .unwrap_err(),
            StreamGuardrailError::Execution
        );
        assert_eq!(guardrail.surfaces.len(), MAX_SURFACES);
    }

    #[tokio::test]
    async fn closed_client_cancels_guardrail_work() {
        let mut guardrail = guardrail(1, true);
        let (tx, rx) = mpsc::channel(1);
        drop(rx);

        let result = guardrail
            .push_many_until_closed(
                &tx,
                vec![(0, "System prompt: hidden".to_string())],
                Bytes::from_static(b"event"),
            )
            .await
            .unwrap();

        assert!(result.is_none());
        assert!(guardrail.surfaces.is_empty());
    }

    #[test]
    fn result_mapping_fails_closed_for_block_and_mask() {
        assert_eq!(
            enforce(CheckResult::block(Vec::new())).unwrap_err(),
            StreamGuardrailError::Violation
        );
        assert_eq!(
            enforce(CheckResult::mask("masked".to_string(), Vec::new())).unwrap_err(),
            StreamGuardrailError::Execution
        );
        assert!(enforce(CheckResult::pass()).is_ok());
    }
}