litellm/tests/router_unit_tests/test_completion_no_copy.py
yuneng-jiang 6a0d03914c
test: drop the cwd-relative sys.path.insert calls from the test suite (#37802)
* 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
2026-08-22 09:25:58 -07:00

109 lines
3.6 KiB
Python

"""
Regression test for removing unnecessary dict.copy() in completion hot paths.
Verifies that spreading deployment["litellm_params"] directly (without copy)
doesn't cause side effects that mutate the deployment in router.model_list.
"""
import pytest
from litellm import Router
from unittest.mock import AsyncMock, Mock, patch
@pytest.mark.asyncio
async def test_acompletion_deployment_not_mutated():
"""
Test async completion doesn't mutate deployment when .copy() is removed.
Optimization: Remove deployment["litellm_params"].copy() in _acompletion
since data is only read and spread into input_kwargs dict.
"""
router = Router(
model_list=[
{
"model_name": "gpt-3.5",
"litellm_params": {
"model": "gpt-5-mini",
"api_key": "test-key",
"temperature": 0.7,
},
}
]
)
deployment_before = router.get_deployment_by_model_group_name("gpt-3.5")
assert deployment_before is not None
original_params = deployment_before.litellm_params.model_dump()
with patch("litellm.acompletion", new_callable=AsyncMock) as mock_acompletion:
from litellm import ModelResponse
mock_acompletion.return_value = ModelResponse(
id="test",
choices=[{"message": {"role": "assistant", "content": "test"}, "index": 0}],
model="gpt-5-mini",
usage={"prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30},
)
try:
await router.acompletion(
model="gpt-3.5",
messages=[{"role": "user", "content": "test"}],
)
except Exception:
pass
# Critical: Deployment params must be unchanged
deployment_after = router.get_deployment_by_model_group_name("gpt-3.5")
assert deployment_after is not None
assert deployment_after.litellm_params.model_dump() == original_params
def test_completion_deployment_not_mutated():
"""
Test sync completion doesn't mutate deployment when .copy() is removed.
Optimization: Remove deployment["litellm_params"].copy() in _completion
since data is only read and spread into input_kwargs dict.
"""
router = Router(
model_list=[
{
"model_name": "gpt-3.5",
"litellm_params": {
"model": "gpt-5-mini",
"api_key": "test-key",
"max_tokens": 100,
},
}
]
)
deployment_before = router.get_deployment_by_model_group_name("gpt-3.5")
assert deployment_before is not None
original_params = deployment_before.litellm_params.model_dump()
with patch("litellm.completion", new_callable=Mock) as mock_completion:
from litellm import ModelResponse
mock_completion.return_value = ModelResponse(
id="test",
choices=[{"message": {"role": "assistant", "content": "test"}, "index": 0}],
model="gpt-5-mini",
usage={"prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30},
)
try:
router.completion(
model="gpt-3.5",
messages=[{"role": "user", "content": "test"}],
)
except Exception:
pass
# Critical: Deployment params must be unchanged
deployment_after = router.get_deployment_by_model_group_name("gpt-3.5")
assert deployment_after is not None
assert deployment_after.litellm_params.model_dump() == original_params