test(callbacks): unwind the callbacks global the policy engine and realtime tests scaffold around - #37826
Merged
yuneng-berri merged 4 commits intoAug 22, 2026
Conversation
…affold around Every one of the 16 tests in this file set litellm.callbacks by hand, each wrapping its body in a try/finally to put the old value back, and each capturing that old value with a .copy() first. That is 32 TQ005 violations and about 70 lines of scaffolding to say what monkeypatch.setattr says in one. The write also sat outside the try, so the block that restores it did not cover the statement that changed it. 16 tests pass either way, and litellm.callbacks reads restored on both sides, because the conftest snapshot already lists it. The point is that these tests stop depending on that snapshot to clean up after them.
Contributor
Greptile SummaryThe PR refactors policy-engine and realtime-streaming tests to isolate
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains.
|
| Filename | Overview |
|---|---|
| test-quality-budget.json | Lowers TQ003 and TQ005 ceilings to match the test-quality improvements reported and verified in the PR evidence. |
| tests/test_litellm/litellm_core_utils/test_realtime_streaming.py | Replaces direct callback writes and manual cleanup with function-scoped monkeypatch restoration without changing test assertions. |
| tests/test_litellm/proxy/policy_engine/test_pipeline_executor.py | Removes callback snapshot and try/finally scaffolding while preserving both pipeline scenarios and their behavioral assertions. |
Reviews (3): Last reviewed commit: "style(realtime): wrap the one signature ..." | Re-trigger Greptile
6 tasks
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
6 tasks
…ming tests Same global, same shape as the previous commit. 25 writes to litellm.callbacks, 2 of them wrapped in a try/finally that resets to [] rather than to the old value, and 12 tests that write it with no protection at all. monkeypatch.setattr replaces all of them, and the sys.path.insert with its now-unused os and sys imports goes too. Both sides read restored here as well, for the same reason as the previous commit: litellm.callbacks is in the conftest snapshot. What changes is that these tests no longer lean on it. 101 tests pass in this file, 16 in the policy engine one.
Contributor
Author
6 tasks
Contributor
Author
ryan-crabbe-berri
approved these changes
Aug 22, 2026
yucheng-berri
approved these changes
Aug 22, 2026
…itellm_policy_engine_callback_global # Conflicts: # test-quality-budget.json
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.
TLDR
Problem this solves:
litellm.callbacksby hand, 41 timesHow it solves it:
monkeypatch.setattrper test, no try/finallyUser Flow
No end-user behavior changes. A proxy admin running a guardrail policy through
https://litellm-domain/v1/chat/completions, or a realtime session through
https://litellm-domain/v1/realtime, gets the same steps executed in the same
order; these are the test files around that code, not the code
Relevant issues
Linear ticket
Pre-Submission checklist
Screenshots / Proof of Fix
Shared setup: run the file, read the global back in the same interpreter, then
count what the quality rules see in it.
Before (ff02d5c)
uv run python scripts/test_quality_gate.pyAfter (5fc0a0f)
1b.
uv run pytest $FILES -q -p no:randomlyuv run python scripts/test_quality_gate.pyType
🧹 Refactoring
✅ Test
Caveats (if any)
litellm.callbacksis already in the conftest snapshot$FILESis the two test files this PR touchestest-quality-budget.json, as do test: drop the cwd-relative sys.path.insert calls from the test suite #37802, test: unwind environment writes in tests/test_litellm with monkeypatch #37806, test(cost-calc): stop 182 global writes leaking out of the cost-calc suites #37815 and test(mcp): retire the last file of the dead tests/litellm mirror #37824Final Attestation