|
15 | 15 | LiteLLMProxyRequestSetup, |
16 | 16 | _get_dynamic_logging_metadata, |
17 | 17 | _get_enforced_params, |
| 18 | + _update_model_if_key_alias_exists, |
18 | 19 | add_litellm_data_to_request, |
19 | 20 | check_if_token_is_service_account, |
20 | 21 | ) |
@@ -1234,3 +1235,65 @@ async def test_request_guardrails_do_not_override_key_guardrails(): |
1234 | 1235 | assert "guardrails" not in updated_data_empty |
1235 | 1236 | assert "key-guardrail-1" in requested_guardrails |
1236 | 1237 | assert len(requested_guardrails) == 1 |
| 1238 | + |
| 1239 | + |
| 1240 | +def test_update_model_if_key_alias_exists(): |
| 1241 | + """ |
| 1242 | + Test that _update_model_if_key_alias_exists properly updates the model when a key alias exists. |
| 1243 | + """ |
| 1244 | + # Test case 1: Key alias exists and matches model |
| 1245 | + data = {"model": "modelAlias", "messages": [{"role": "user", "content": "Hello"}]} |
| 1246 | + user_api_key_dict = UserAPIKeyAuth( |
| 1247 | + api_key="test-key", |
| 1248 | + aliases={"modelAlias": "xai/grok-4-fast-non-reasoning"}, |
| 1249 | + ) |
| 1250 | + _update_model_if_key_alias_exists(data=data, user_api_key_dict=user_api_key_dict) |
| 1251 | + assert data["model"] == "xai/grok-4-fast-non-reasoning" |
| 1252 | + |
| 1253 | + # Test case 2: Key alias doesn't exist |
| 1254 | + data = {"model": "unknown-model", "messages": [{"role": "user", "content": "Hello"}]} |
| 1255 | + user_api_key_dict = UserAPIKeyAuth( |
| 1256 | + api_key="test-key", |
| 1257 | + aliases={"modelAlias": "xai/grok-4-fast-non-reasoning"}, |
| 1258 | + ) |
| 1259 | + original_model = data["model"] |
| 1260 | + _update_model_if_key_alias_exists(data=data, user_api_key_dict=user_api_key_dict) |
| 1261 | + assert data["model"] == original_model # Should remain unchanged |
| 1262 | + |
| 1263 | + # Test case 3: Model is None |
| 1264 | + data = {"model": None, "messages": [{"role": "user", "content": "Hello"}]} |
| 1265 | + user_api_key_dict = UserAPIKeyAuth( |
| 1266 | + api_key="test-key", |
| 1267 | + aliases={"modelAlias": "xai/grok-4-fast-non-reasoning"}, |
| 1268 | + ) |
| 1269 | + _update_model_if_key_alias_exists(data=data, user_api_key_dict=user_api_key_dict) |
| 1270 | + assert data["model"] is None # Should remain None |
| 1271 | + |
| 1272 | + # Test case 4: Model key doesn't exist in data |
| 1273 | + data = {"messages": [{"role": "user", "content": "Hello"}]} |
| 1274 | + user_api_key_dict = UserAPIKeyAuth( |
| 1275 | + api_key="test-key", |
| 1276 | + aliases={"modelAlias": "xai/grok-4-fast-non-reasoning"}, |
| 1277 | + ) |
| 1278 | + _update_model_if_key_alias_exists(data=data, user_api_key_dict=user_api_key_dict) |
| 1279 | + assert "model" not in data # Should not add model if it doesn't exist |
| 1280 | + |
| 1281 | + # Test case 5: Multiple aliases, matching one |
| 1282 | + data = {"model": "alias1", "messages": [{"role": "user", "content": "Hello"}]} |
| 1283 | + user_api_key_dict = UserAPIKeyAuth( |
| 1284 | + api_key="test-key", |
| 1285 | + aliases={ |
| 1286 | + "alias1": "model1", |
| 1287 | + "alias2": "model2", |
| 1288 | + "alias3": "model3", |
| 1289 | + }, |
| 1290 | + ) |
| 1291 | + _update_model_if_key_alias_exists(data=data, user_api_key_dict=user_api_key_dict) |
| 1292 | + assert data["model"] == "model1" |
| 1293 | + |
| 1294 | + # Test case 6: Empty aliases dict |
| 1295 | + data = {"model": "modelAlias", "messages": [{"role": "user", "content": "Hello"}]} |
| 1296 | + user_api_key_dict = UserAPIKeyAuth(api_key="test-key", aliases={}) |
| 1297 | + original_model = data["model"] |
| 1298 | + _update_model_if_key_alias_exists(data=data, user_api_key_dict=user_api_key_dict) |
| 1299 | + assert data["model"] == original_model # Should remain unchanged |
0 commit comments