fix(proxy): resolve type errors in _add_config_models instead of raising budget

- Revert basedpyright-code-budget.json to match origin/main; the prior
  commit raised reportOptionalMemberAccess (0->59) and
  reportUnnecessaryIsInstance (816->818) instead of fixing the
  underlying issues.
- Remove redundant isinstance(config_state, dict) check:
  get_config_state() only ever returns a dict (deepcopy of self.config,
  or {} on error), so the check was unreachable-false and flagged by
  reportUnnecessaryIsInstance.
- Guard _add_config_models against llm_router being None before calling
  llm_router.generate_model_id()/upsert_deployment(), matching the
  existing guard in the sibling _add_deployment method. This closes
  both reportOptionalMemberAccess hits and fixes a latent
  AttributeError if the method runs before the router is initialized.
- Add regression tests covering the config_models=None fallback to
  user_config_file_path, the llm_router-is-None guard, and the
  preexisting model_info.id branch.
This commit is contained in:
Ayush Arora 2026-09-13 20:03:52 +05:30
parent 76e66982d9
commit d6376bf0e7
3 changed files with 92 additions and 5 deletions

View file

@ -72,7 +72,7 @@
"limit": 0
},
"reportOptionalMemberAccess": {
"limit": 59
"limit": 0
},
"reportOptionalOperand": {
"limit": 0
@ -123,7 +123,7 @@
"limit": 4
},
"reportUnnecessaryIsInstance": {
"limit": 818
"limit": 816
},
"reportUntypedBaseClass": {
"limit": 0
@ -143,4 +143,4 @@
"reportUnusedVariable": {
"limit": 137
}
}
}

View file

@ -6498,8 +6498,7 @@ class ProxyConfig:
def _add_config_models(self, config_models: list | None = None) -> int:
if config_models is None:
config_state = self.get_config_state()
if isinstance(config_state, dict):
config_models = config_state.get("model_list", None)
config_models = config_state.get("model_list", None)
if not config_models and user_config_file_path and os.path.exists(user_config_file_path):
try:
with open(user_config_file_path, "r") as f:
@ -6512,6 +6511,9 @@ class ProxyConfig:
if not config_models:
return 0
if llm_router is None:
return 0
added_count = 0
for model in config_models:
try:

View file

@ -351,3 +351,88 @@ class TestDeleteDeploymentKeepsPluginConfigModels:
reconciled_deployments = [d for d in router.model_list if d.get("model_name") == "gpt-4-test"]
assert len(reconciled_deployments) == 1
assert reconciled_deployments[0]["litellm_params"]["timeout"] == 60
class TestAddConfigModels:
"""Test _add_config_models directly: the config_models=None fallback path
(config_state -> user_config_file_path -> give up) and the pre-set
model_info.id branch. The resilience tests above never exercise these,
because they either mock _add_deployment entirely or always pass
config_models pre-populated with no id."""
@pytest.mark.asyncio
async def test_falls_back_to_user_config_file_when_config_state_has_no_models(self, tmp_path):
import yaml
proxy_config = ProxyConfig()
config_file_path = tmp_path / "config.yaml"
config_file_path.write_text(
yaml.safe_dump(
{
"model_list": [
{
"model_name": "file-fallback-model",
"litellm_params": {"model": "gpt-4o-mini"},
}
]
}
)
)
mock_router = MagicMock()
mock_router.generate_model_id.return_value = "generated-id"
mock_router.upsert_deployment.return_value = True
with (
patch.object(proxy_config, "get_config_state", return_value={}),
patch("litellm.proxy.proxy_server.user_config_file_path", str(config_file_path)),
patch("litellm.proxy.proxy_server.llm_router", mock_router),
):
result = proxy_config._add_config_models(config_models=None)
assert result == 1
mock_router.generate_model_id.assert_called_once()
mock_router.upsert_deployment.assert_called_once()
def test_returns_zero_when_router_missing_or_no_models_found(self, tmp_path):
proxy_config = ProxyConfig()
# No config_state models and no readable config file -> config_models stays empty
with (
patch.object(proxy_config, "get_config_state", return_value={}),
patch("litellm.proxy.proxy_server.user_config_file_path", str(tmp_path / "missing.yaml")),
patch("litellm.proxy.proxy_server.llm_router", MagicMock()),
):
assert proxy_config._add_config_models(config_models=None) == 0
# Models are present but llm_router hasn't been initialized yet
with patch("litellm.proxy.proxy_server.llm_router", None):
assert (
proxy_config._add_config_models(
config_models=[{"model_name": "m", "litellm_params": {"model": "gpt-4o-mini"}}]
)
== 0
)
def test_respects_a_preexisting_model_info_id(self):
proxy_config = ProxyConfig()
mock_router = MagicMock()
mock_router.upsert_deployment.return_value = True
with patch("litellm.proxy.proxy_server.llm_router", mock_router):
result = proxy_config._add_config_models(
config_models=[
{
"model_name": "pinned-model",
"litellm_params": {"model": "gpt-4o-mini"},
"model_info": {"id": "operator-pinned-id"},
}
]
)
assert result == 1
mock_router.generate_model_id.assert_not_called()
upserted_deployment = mock_router.upsert_deployment.call_args.kwargs["deployment"]
assert upserted_deployment.model_info.id == "operator-pinned-id"