Skip to content

test(callbacks): unwind the callbacks global the policy engine and realtime tests scaffold around - #37826

Merged
yuneng-berri merged 4 commits into
litellm_internal_stagingfrom
litellm_policy_engine_callback_global
Aug 22, 2026
Merged

test(callbacks): unwind the callbacks global the policy engine and realtime tests scaffold around#37826
yuneng-berri merged 4 commits into
litellm_internal_stagingfrom
litellm_policy_engine_callback_global

Conversation

@yuneng-berri

@yuneng-berri yuneng-berri commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Two files set litellm.callbacks by hand, 41 times
  • Some wrap the body in try/finally, most do not
  • 59 TQ005 violations and the scaffolding around them

How it solves it:

  • One monkeypatch.setattr per test, no try/finally
  • The writes stop sitting outside the block restoring them

User 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

  • I have added meaningful tests
  • The handful of test files covering my change pass locally
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review

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.

uv run python -c '
import litellm, pytest
before = list(litellm.callbacks)
pytest.main(["tests/test_litellm/proxy/policy_engine/test_pipeline_executor.py", "-q", "-p", "no:randomly"])
print("litellm.callbacks after the run:", "restored" if list(litellm.callbacks) == before else "LEAKED")
'
for f in $FILES; do uv run python scripts/check_test_quality.py $f; done | grep -oE "TQ00[0-9]" | sort | uniq -c

Before (ff02d5c)

  1. The first command
16 passed, 2 warnings in 2.10s
litellm.callbacks after the run: restored
  1. The second command
   1 TQ003
  59 TQ005
  1. uv run python scripts/test_quality_gate.py
OK: every TQ rule is within its test-suite ceiling (base origin/litellm_internal_staging)

After (5fc0a0f)

  1. The first command
16 passed, 2 warnings in 1.90s
litellm.callbacks after the run: restored

1b. uv run pytest $FILES -q -p no:randomly

212 passed, 10 warnings in 64.15s (0:01:04)
  1. The second command

  1. uv run python scripts/test_quality_gate.py
OK: every TQ rule is within its test-suite ceiling (base origin/litellm_internal_staging)

Type

🧹 Refactoring

✅ Test

Caveats (if any)

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

…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.
@greptile-apps

greptile-apps Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR refactors policy-engine and realtime-streaming tests to isolate litellm.callbacks through pytest’s monkeypatch fixture instead of manual assignment and cleanup.

  • Removes repetitive callback snapshot and try/finally scaffolding.
  • Removes an unnecessary test-local Python path modification.
  • Ratchets the TQ003 and TQ005 quality budgets to reflect the reduced violations.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

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

@codecov

codecov Bot commented Aug 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…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.
@yuneng-berri yuneng-berri changed the title test(policy-engine): unwind the callback global the pipeline tests scaffold around test(callbacks): unwind the callbacks global the policy engine and realtime tests scaffold around Aug 21, 2026
@yuneng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@yuneng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

…itellm_policy_engine_callback_global

# Conflicts:
#	test-quality-budget.json
@yuneng-berri
yuneng-berri merged commit 6bce3dc into litellm_internal_staging Aug 22, 2026
68 checks passed
@yuneng-berri
yuneng-berri deleted the litellm_policy_engine_callback_global branch August 22, 2026 04:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants