test(guardrails): stop the onyx and prompt security tests leaking env vars on failure - #37828
Merged
Merged
Conversation
Contributor
Greptile SummaryThis PR improves guardrail-test isolation by replacing direct environment and global-state mutations with pytest monkeypatch cleanup.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains.
|
| Filename | Overview |
|---|---|
| test-quality-budget.json | Lowers test-quality limits to account for violations removed by the test cleanup. |
| tests/test_litellm/proxy/guardrails/guardrail_hooks/test_deepkeep.py | Uses monkeypatch for environment and global-state isolation. |
| tests/test_litellm/proxy/guardrails/guardrail_hooks/test_hiddenlayer.py | Replaces direct environment mutations with failure-safe monkeypatch operations. |
| tests/test_litellm/proxy/guardrails/guardrail_hooks/test_onyx.py | Adds failure-safe environment isolation and verifies the configured guardrail registration; the previously reported line-length issue is resolved. |
| tests/test_litellm/proxy/guardrails/guardrail_hooks/test_repelloai.py | Isolates environment and global-state changes through monkeypatch. |
| tests/test_litellm/proxy/guardrails/test_prompt_security_guardrails.py | Makes environment cleanup failure-safe and adds concrete registration assertions. |
Reviews (3): Last reviewed commit: "test(guardrails): stop five guardrail te..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
Author
This was referenced Aug 21, 2026
…failure Onyx, prompt security, hiddenlayer, repelloai and deepkeep all write straight to os.environ and unset again at the bottom of each test. None of the five has a try/finally, so the moment a test fails it returns to the runner with the keys still set and whatever runs next in that worker inherits them. Raising inside test_onyx_guard_with_custom_timeout_from_kwargs on the current files leaves ONYX_API_BASE and ONYX_API_KEY behind; doing the same in test_hiddenlayer_config_saas leaves HIDDENLAYER_API_BASE. Both come back clean after this. 89 raw writes and the hand-rolled deletes become monkeypatch calls. The class-level setup_method and teardown_method pair in the onyx file, sweeping the same three keys twice, becomes one autouse fixture. The sys.path.insert lines and their now-unused imports go too, and litellm.set_verbose = True, which only turned global debug logging on for whatever ran next, is dropped rather than restored. test_onyx_guard_config and test_prompt_security_guard_config asserted nothing at all, so they could only fail by raising. Each now pins what init_guardrails_v2 produces: exactly one guardrail of the right class on litellm.callbacks, carrying the configured name, default_on and hook. The zero-assert tests in the other three are left alone; those are a judgement about each guardrail rather than a mechanical sweep. tests/test_litellm/proxy/guardrails passes at 2873.
yuneng-berri
force-pushed
the
litellm_onyx_env_leak
branch
from
August 21, 2026 12:35
79bafce to
15138fd
Compare
Contributor
Author
ryan-crabbe-berri
approved these changes
Aug 22, 2026
yucheng-berri
approved these changes
Aug 22, 2026
…itellm_onyx_env_leak # Conflicts: # test-quality-budget.json # tests/test_litellm/proxy/guardrails/guardrail_hooks/test_deepkeep.py # tests/test_litellm/proxy/guardrails/guardrail_hooks/test_hiddenlayer.py # tests/test_litellm/proxy/guardrails/guardrail_hooks/test_onyx.py # tests/test_litellm/proxy/guardrails/guardrail_hooks/test_repelloai.py # tests/test_litellm/proxy/guardrails/test_prompt_security_guardrails.py
…itellm_onyx_env_leak # 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:
os.environHow it solves it:
monkeypatch.setenvanddelenvunset on failure tooUser Flow
No end-user behavior changes. A proxy admin running any of these five guardrails
gets the same request checked the same way; these are the test files around
those guardrails, not the guardrails
Relevant issues
Linear ticket
Pre-Submission checklist
Screenshots / Proof of Fix
Shared setup: clear the keys, run one test with one line forced to fail, read
the environment back in the same interpreter. A passing run is clean on both
sides, so the forced failure is the only state where they differ.
probeisthis, parameterised by test id and keys:
$FILESbelow is the five test files this PR touches.Before (ff02d5c)
1. onyx
assert False, "forced"underassert timeout_param.read == 45.0, thenprobe tests/test_litellm/proxy/guardrails/guardrail_hooks/test_onyx.py::test_onyx_guard_with_custom_timeout_from_kwargs ONYX_API_BASE ONYX_API_KEY ONYX_TIMEOUT2. hiddenlayer
raise AssertionError("forced")above theinit_guardrails_v2(call intest_hiddenlayer_config_saas, thenprobe tests/test_litellm/proxy/guardrails/guardrail_hooks/test_hiddenlayer.py::test_hiddenlayer_config_saas HIDDENLAYER_API_BASE HIDDENLAYER_API_KEY3. prompt security
assert False, "forced"under the first assert oftest_apply_guardrail_block_request, thenprobe tests/test_litellm/proxy/guardrails/test_prompt_security_guardrails.py::test_apply_guardrail_block_request PROMPT_SECURITY_API_KEY PROMPT_SECURITY_API_BASE4. what the quality rules see
for f in $FILES; do uv run python scripts/check_test_quality.py $f; done | grep -oE "TQ00[0-9]" | sort | uniq -cAfter (15138fd)
1. onyx
2. hiddenlayer
3. prompt security
4. what the quality rules see
for f in $FILES; do uv run python scripts/check_test_quality.py $f; done | grep -oE "TQ00[0-9]" | sort | uniq -cuv run pytest tests/test_litellm/proxy/guardrails/ -q -p no:randomly -n 4Type
🐛 Bug Fix
✅ Test
Caveats (if any)
proxy/guardrailsrelied on it, all 2873 passproxy/guardrailshave the same shapetest-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, test(mcp): retire the last file of the dead tests/litellm mirror #37824 and test(callbacks): unwind the callbacks global the policy engine and realtime tests scaffold around #37826Final Attestation