fix: handle non-JSON string values in ConfigRepository.get_param()

Addresses Greptile review: get_param() now catches json.JSONDecodeError
for corrupt/non-JSON string values in the DB instead of crashing config
reload. Restores original test case using 'not_a_dict' string value.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
unknown 2026-07-01 10:29:50 +00:00
parent 3496249d79
commit 238da91154
3 changed files with 19 additions and 2 deletions

View file

@ -54,7 +54,13 @@ class ConfigRepository:
return None
param_value = record.param_value
if isinstance(param_value, str):
param_value = json.loads(param_value)
try:
param_value = json.loads(param_value)
except (json.JSONDecodeError, ValueError):
verbose_proxy_logger.warning(
"config_repository.get_param: param_name=%s has non-JSON string value, returning as-is",
param_name,
)
return ConfigParam(param_name=param_name, param_value=param_value)
async def set_param(self, param_name: str, param_value: Any) -> ConfigParam:

View file

@ -3896,7 +3896,7 @@ async def test_add_router_settings_from_db_config_edge_cases():
# Test Case 6: DB config exists but param_value is not a dict
mock_db_config_invalid = MagicMock()
mock_db_config_invalid.param_value = 42
mock_db_config_invalid.param_value = "not_a_dict"
mock_prisma_client.db.litellm_config.find_unique = AsyncMock(
return_value=mock_db_config_invalid
)

View file

@ -1387,6 +1387,17 @@ class TestConfigRepository:
assert param.param_name == "general_settings"
assert param.param_value["master_key"] == "test"
@pytest.mark.asyncio
async def test_get_param_non_json_string(self, repo):
"""Non-JSON string values in the DB should not crash get_param."""
repo._prisma_client.db.litellm_config._records["router_settings"] = {
"param_name": "router_settings",
"param_value": "not_valid_json",
}
param = await repo.get_param("router_settings")
assert param is not None
assert param.param_value == "not_valid_json"
@pytest.mark.asyncio
async def test_set_param(self, repo):
param = await repo.set_param("test_param", {"key": "value"})