Repository navigation
fix(guardrails): classify only the frames each read completes in Presidio's first-frame wait - #54379
Merged
mateo-berri merged 6 commits intoNov 25, 2026
Conversation
…idio'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
# Conflicts: # litellm/proxy/guardrails/guardrail_hooks/presidio.py # tests/unit/proxy/guardrails/guardrail_hooks/test_presidio.py
Contributor
Contributor
Contributor
[Medium impact] The PR appears safe to merge, with no outstanding findings.
|
…yte 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
Contributor
Contributor
|
bugbot run |
…ead 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
Contributor
Contributor
|
bugbot run |
…ding 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
Contributor
Contributor
|
bugbot run |
Contributor
Contributor
|
bugbot run |
Contributor
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 67faf6d. Configure here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Handwritten 1-3 sentence description
When a streamed messages request goes through the Presidio guardrail and the upstream sends lots of small frames before its first real event, Presidio rescanned everything it had buffered on every read, so its CPU cost grew with the square of the number of reads. Now it only looks at the frames each read finishes, so the CPU the guardrail adds to that stream drops from about 12.7 s to about 8.6 s. This is the main copy of the same fix in #53403 for 1.108.x
Details
TLDR
Problem this solves:
data: 5frames cost the proxy's Python threads a median 18.6 s of CPU against 8.7 s with no guardrailHow it solves it:
bytearray, so a read no longer copies the bufferUser Flow
No user reported this. Veria found it in review on #53403
Before: one guarded stream with thousands of tiny frames before the reply keeps the proxy's Python threads busy for seconds
"stream": trueand"guardrails": ["pii"], asking to repeat a test carddata: 5frames, 2 ms apart, then the real reply<CREDIT_CARD>After: the guardrail adds about 8.6 s of CPU to that stream instead of about 12.7 s, and Python's share drops from about 10 s to about 2 s
"stream": trueand"guardrails": ["pii"], asking to repeat a test carddata: 5frames, 2 ms apart, then the real reply<CREDIT_CARD>Decisions
\r\n\r\nsplit across two reads is still foundtests/unit/AGENTS.mdbans tests that depend on speed. A CodSpeed benchmark watches the cost instead.Relevant issues
The wait came from #49088. #53403 fixes the same cost on
lts/1.108.xin1faa9404e8Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Real Anthropic
claude-haiku-5-5sits behind a relay that sends 7000data: 5frames 2 ms apart, then the real reply. Presidio masksCREDIT_CARDin output. Both proxies runpython litellm/proxy/proxy_cli.py --config config.yaml --port <port> --num_workers 2from their own tree on one 8-core host. The scripts and every raw line are in round 9, and the relay andconfig.yamlin round 7case9.shadds up CPU by thread name across the supervisor and both workers and sendsGET /v1/modelsevery 100 ms during the stream.run9.shruns 5 rounds per side, alternating. The live rows ran ate491e144e5. Since then the commits up to67faf6d401only fix a docstring, give the wait aclassifyparameter defaulting to_classify_completed_frames, which it called before, and change tests and benchmarks, so the rows still standBefore (
c74b785396, the merge base)./run9.sh, then this view ofsummary.txtBefore rows
After (
e491e144e5, carried to the tip)after-After rows
uv run --no-sync pytest -q tests/unit/proxy/guardrails/guardrail_hooks/test_presidio.py tests/unit/proxy/common_utils/test_sse_keepalive.pyprints506 passedat67faf6d401What the tests and benchmarks catch
The per-read test fails when the wait drops the classifier's offset or skips it, when the classifier slices from 0 as the old code did, or without the split-terminator lookback. A direct test of
last_sse_frame_endfails on the three frame-end mutants the older tests missed. Locally the new benchmarks take 8.3 s instead of 19 ms when the JSON check rescans every frame, and 84 ms instead of 9 ms when the frame-end search starts at the last classified bytepythonCPUpythonCPU, no guardrailtokio-rt-workerCPU/v1/modelsp50No-guardrail rows are 3 rounds per side from
run9-noguard.sh. In every round pair Python drops 7.0 to 8.1 s and all threads 3.9 to 5.7 s, while Rust rises 2.2 to 3.1 s because Python now takes each frame from Rust on its own (round 10). After round 5 hitmax_tokensmid-number, and a partial card fails the checksum, so Presidio leaves it on either sideWhen the relay sends three
: keepalivecomments ahead of the real reply instead (round 11), both sides pass all three through ahead ofmessage_startand mask the card in all 8 replies that finishedType
🐛 Bug Fix
Caveats (if any)
Low
GET /v1/modelshad the same p50 on both sidesFinal Attestation
Bugbot summary