mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-28 00:00:38 +00:00
* fix(guardrails): classify only the frames each read completes in Presidio's first-frame wait Presidio's wait for the first frame with a JSON object re-split and re-classified its whole buffer on every read, so a long run of leading non-object frames cost CPU quadratic in the number of reads. It now buffers in a bytearray, finds frame ends only in the newly read bytes, and classifies only the frames that read completed * test(guardrails): check Presidio's first-frame wait classifies each byte once instead of timing it The test guide bans checks that depend on execution speed, so the CPU-time limit on the many-leading-reads test is replaced by a test that each read is handed only the frames it completed, including frame ends split across reads * test(guardrails): drive Presidio's real first-frame wait in its per-read classification test The test copied the wait's loop, so it still passed if the wait itself dropped the offset the classifier returns. The wait now takes the classifier as a parameter, defaulting to the one it called before, and the test runs the real wait with a recording classifier. The classifier docstring now says the caller relays the preface * test(benchmarks): benchmark Presidio's first-frame wait over many leading reads and a frame split into many reads Unit tests can't check the wait's cost without timing it, so CodSpeed now times the real wait over 6000 leading non-object reads and over one 48 KB frame in 16-byte reads. Rescanning every classified frame for a JSON object, or searching the whole unclassified tail for a frame end, makes these runs many times slower * test(proxy): check the SSE frame-end search directly --------- Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
29 lines
1.2 KiB
Python
29 lines
1.2 KiB
Python
import asyncio
|
|
from collections.abc import AsyncIterator, Sequence
|
|
from typing import Final
|
|
|
|
import pytest
|
|
|
|
from litellm.proxy.guardrails.guardrail_hooks.presidio import _coalesce_first_sse_frame
|
|
|
|
LEADING_NON_OBJECT_READS: Final = tuple(b"data: 5\n\n" for _ in range(6000)) + (b'data: {"type": "ping"}\n\n',)
|
|
LARGE_FIRST_FRAME: Final = b'data: {"type": "message_start", "pad": "' + b"x" * 48_000 + b'"}\n\n'
|
|
LARGE_FIRST_FRAME_READS: Final = tuple(LARGE_FIRST_FRAME[i : i + 16] for i in range(0, len(LARGE_FIRST_FRAME), 16))
|
|
|
|
|
|
async def _first_sse_frame(reads: Sequence[bytes]) -> list[object]:
|
|
async def upstream() -> AsyncIterator[object]:
|
|
for chunk in reads:
|
|
yield chunk
|
|
|
|
return [chunk async for chunk in _coalesce_first_sse_frame(upstream())]
|
|
|
|
|
|
@pytest.mark.benchmark
|
|
def test_presidio_first_frame_wait_over_many_leading_non_object_reads() -> None:
|
|
assert asyncio.run(_first_sse_frame(LEADING_NON_OBJECT_READS)) == [b"".join(LEADING_NON_OBJECT_READS)]
|
|
|
|
|
|
@pytest.mark.benchmark
|
|
def test_presidio_first_frame_wait_over_a_frame_split_into_many_reads() -> None:
|
|
assert asyncio.run(_first_sse_frame(LARGE_FIRST_FRAME_READS)) == [LARGE_FIRST_FRAME]
|