mirror of
https://github.com/BerriAI/litellm.git
synced 2026-08-28 05:25:59 +00:00
* test: drop the cwd-relative sys.path.insert calls from the test suite
TQ003 stands at 1,077 across 1,058 files, and 1,015 of them are the same shape:
sys.path.insert(0, os.path.abspath("../..")) and its deeper siblings. The
argument resolves against the working directory rather than the file, so from
the repo root, where every job runs pytest, it inserts the directory two levels
above the checkout. It has never pointed at litellm. The package is installed
into the environment anyway, which is what actually makes the import work, and
what the rule's message has said all along.
Removing them leaves 1,634 imports of sys and os with no remaining reference,
and those go too, except where another test module imports the name back out of
the file. The rest of TQ003 is 62 call sites that resolve against __file__ or a
variable, which are a different question and are left alone.
Collection is identical either way: 45,871 tests and the same 51 pre-existing
collection errors before and after, and ruff reports no new undefined name.
* test: drop the duplicate imports the sys.path sweep exposed to F811
* test(pre-call-utils): restore the os import the new bedrock tests need
82 lines
3.7 KiB
Python
82 lines
3.7 KiB
Python
"""
|
|
Regression test for default_deployment shallow copy optimization.
|
|
|
|
Tests the critical side effect: ensure modifying returned deployment
|
|
doesn't corrupt the original default_deployment instance.
|
|
"""
|
|
|
|
|
|
|
|
from litellm import Router
|
|
|
|
|
|
def test_default_deployment_isolation():
|
|
"""
|
|
Regression test for shallow copy optimization in _common_checks_available_deployment.
|
|
|
|
When a model is not in model_names and default_deployment is set, the router
|
|
returns a copy of default_deployment with the model name updated. This test
|
|
ensures the optimization (shallow copy instead of deepcopy) properly isolates
|
|
each returned deployment from the original and from each other.
|
|
|
|
The shallow copy optimization copies two levels:
|
|
1. Top-level deployment dict
|
|
2. litellm_params dict
|
|
|
|
Deeper nested objects are intentionally shared for performance (safe because
|
|
the router only modifies the 'model' field at litellm_params level).
|
|
|
|
Critical behavior verified:
|
|
1. Each deployment gets independent model value
|
|
2. Original default_deployment unchanged for litellm_params fields
|
|
3. Shared fields (api_key) accessible in all copies
|
|
4. Adding new litellm_params fields is isolated per deployment
|
|
5. Deep nested objects ARE shared (acceptable trade-off)
|
|
"""
|
|
# Setup: Router with a default deployment (used for unknown models)
|
|
router = Router(model_list=[])
|
|
|
|
router.default_deployment = { # type: ignore
|
|
"model_name": "default-model",
|
|
"litellm_params": {
|
|
"model": "gpt-5-mini", # This will be overwritten per request
|
|
"api_key": "test-key", # This should be shared
|
|
"custom_config": { # Deep nested - will be SHARED
|
|
"nested_setting": "original",
|
|
},
|
|
},
|
|
}
|
|
|
|
# Act: Request two different unknown models (triggers default deployment path)
|
|
_, deployment1 = router._common_checks_available_deployment(
|
|
model="custom-model-1", # Unknown model
|
|
messages=[{"role": "user", "content": "test"}],
|
|
)
|
|
|
|
_, deployment2 = router._common_checks_available_deployment(
|
|
model="custom-model-2", # Different unknown model
|
|
messages=[{"role": "user", "content": "test"}],
|
|
)
|
|
|
|
# Assert: Each deployment should have its own independent model value
|
|
assert deployment1["litellm_params"]["model"] == "custom-model-1" # type: ignore
|
|
assert deployment2["litellm_params"]["model"] == "custom-model-2" # type: ignore
|
|
|
|
# Assert: Original default_deployment must remain unchanged (not mutated by requests)
|
|
assert router.default_deployment["litellm_params"]["model"] == "gpt-5-mini" # type: ignore
|
|
|
|
# Assert: Shared fields should still be accessible in all copies
|
|
assert deployment1["litellm_params"]["api_key"] == "test-key" # type: ignore
|
|
assert deployment2["litellm_params"]["api_key"] == "test-key" # type: ignore
|
|
|
|
# Assert: Modifying litellm_params in one deployment doesn't affect others
|
|
# This tests the shallow copy properly isolated the litellm_params dict level
|
|
deployment1["litellm_params"]["temperature"] = 0.9 # type: ignore
|
|
assert "temperature" not in deployment2["litellm_params"] # type: ignore
|
|
assert "temperature" not in router.default_deployment["litellm_params"] # type: ignore
|
|
|
|
# Assert: Deep nested objects ARE shared (intentional trade-off for 100x perf gain)
|
|
# Safe because router only modifies top-level litellm_params fields
|
|
deployment1["litellm_params"]["custom_config"]["nested_setting"] = "modified" # type: ignore
|
|
assert deployment2["litellm_params"]["custom_config"]["nested_setting"] == "modified" # type: ignore
|
|
assert router.default_deployment["litellm_params"]["custom_config"]["nested_setting"] == "modified" # type: ignore
|