test: stop the zai tests from leaking env and litellm globals into the session - #37834
Merged
yuneng-berri merged 3 commits intoAug 22, 2026
Merged
Conversation
…e session test_zai_provider.py set LITELLM_LOCAL_MODEL_COST_MAP and litellm.model_cost directly and never put them back, so every test that ran after it in the same process saw a local cost map instead of the real one. The two respx tests did the same to litellm.disable_aiohttp_transport with no restore at all. Both now go through monkeypatch, which restores on teardown including when the test fails. The cost-map setup moves into a fixture requested by exactly the five tests that read the cost map.
Contributor
Greptile SummaryThe PR prevents Z.AI tests from leaking selected environment and LiteLLM global state by moving mutations into pytest monkeypatch-managed fixtures.
Confidence Score: 5/5The PR appears safe to merge, with the changed test globals restored at fixture teardown and no actionable regression identified. The five cost-map tests retain their original setup and assertions while pytest now restores the environment and model-cost global, and both transport overrides are similarly restored; the quality-budget changes match the eliminated violations.
|
| Filename | Overview |
|---|---|
| tests/test_litellm/llms/zai/test_zai_provider.py | Replaces persistent environment and module-global assignments with function-scoped monkeypatch restoration without weakening the existing assertions. |
| test-quality-budget.json | Lowers TQ004 by five and TQ005 by seven, exactly matching the direct mutations removed from the Z.AI tests. |
Reviews (1): Last reviewed commit: "test: stop the zai tests from leaking en..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
ryan-crabbe-berri
approved these changes
Aug 22, 2026
yucheng-berri
approved these changes
Aug 22, 2026
…itellm_zai_test_env_isolation # Conflicts: # test-quality-budget.json # tests/test_litellm/llms/zai/test_zai_provider.py
…itellm_zai_test_env_isolation # 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_LOCAL_MODEL_COST_MAPset for the whole sessionlitellm.model_costswapped to the local mapdisable_aiohttp_transportis set with no restore at allHow it solves it:
monkeypatch, which restores on teardownUser Flow
Before: a proxy admin debugging a cost discrepancy asks a maintainer to confirm a price, and the maintainer's local run reports a stale one
litellm.model_costand gets the checked-in local snapshot, not the live mapAfter: the same run reads the real map, so the answer matches the proxy
LITELLM_LOCAL_MODEL_COST_MAPandlitellm.model_costback when they finishRelevant issues
Linear ticket
Pre-Submission checklist
uv run pytest tests/test_litellm/<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 more@greptileaito re-request a review after pushing changes)Delays 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
The leak is only observable from outside the pytest process, so the probe runs the file in-process and reads the environment back afterwards.
LITELLM_LOCAL_MODEL_COST_MAPis not part of the conftest snapshot, so nothing else restores it.Before (ff02d5c)
python leakprobe.pyLITELLM_LOCAL_MODEL_COST_MAP after: 'True'After (b87f1bb)
python leakprobe.pyLITELLM_LOCAL_MODEL_COST_MAP after: NoneType
🧹 Refactoring
✅ Test
Caveats (if any)
litellm.model_costrestores via the conftest snapshot either wayFinal Attestation