fix(model_management_endpoints.py): add audit logs on model update (#12396)

ensure model updates are tracked
This commit is contained in:
Krish Dholakia 2025-07-07 21:43:34 -07:00 • committed by GitHub
parent 76c9df1f91
commit b08ea835c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 44 additions and 10 deletions

View file

@ -8,3 +8,7 @@ model_list:
model: langfuse/langfuse-model
prompt_id: test-chat-prompt
prompt_version: 4
litellm_settings:
store_audit_logs: true

View file

@ -3,7 +3,7 @@ Allow proxy admin to add/update/delete models in the db
Currently most endpoints are in `proxy_server.py`, but those should be moved here over time.
Endpoints here:
Endpoints here:
model/{model_id}/update - PATCH endpoint for model update.
"""
@ -219,10 +219,24 @@ async def patch_model(
where={"model_id": model_id},
data=update_data,
)
# Clear cache and reload models
await clear_cache()
## CREATE AUDIT LOG ##
asyncio.create_task(
create_object_audit_log(
object_id=model_id,
action="updated",
user_api_key_dict=user_api_key_dict,
table_name=LitellmTableNames.PROXY_MODEL_TABLE_NAME,
before_value=db_model.model_dump_json(exclude_none=True),
after_value=updated_model.model_dump_json(exclude_none=True),
litellm_changed_by=user_api_key_dict.user_id,
litellm_proxy_admin_name=LITELLM_PROXY_ADMIN_NAME,
)
)
return updated_model
except Exception as e:
@ -951,23 +965,30 @@ def _deduplicate_litellm_router_models(models: List[Dict]) -> List[Dict]:
seen_ids.add(model_id)
return unique_models
async def clear_cache():
"""
Clear router caches and reload models.
"""
from litellm.proxy.proxy_server import (
proxy_config,
llm_router,
prisma_client,
proxy_config,
proxy_logging_obj,
verbose_proxy_logger,
)
if llm_router is None or prisma_client is None:
verbose_proxy_logger.debug(
"llm_router or prisma_client is None, skipping cache clear"
)
return
try:
llm_router.model_list.clear()
await proxy_config.add_deployment(
prisma_client=prisma_client,
proxy_logging_obj=proxy_logging_obj
prisma_client=prisma_client, proxy_logging_obj=proxy_logging_obj
)
except Exception as e:
verbose_proxy_logger.exception(

View file

@ -38,7 +38,13 @@ async def create_object_audit_log(
- user_api_key_dict: UserAPIKeyAuth - The user api key dictionary.
- litellm_proxy_admin_name: Optional[str] - The name of the proxy admin.
"""
if not litellm.store_audit_logs:
from litellm.secret_managers.main import get_secret_bool
store_audit_logs = litellm.store_audit_logs or get_secret_bool(
"LITELLM_STORE_AUDIT_LOGS"
)
if store_audit_logs is not True:
return
await create_audit_log_for_update(
@ -62,7 +68,12 @@ async def create_audit_log_for_update(request_data: LiteLLM_AuditLogs):
"""
Create an audit log for an object.
"""
if not litellm.store_audit_logs:
from litellm.secret_managers.main import get_secret_bool
store_audit_logs = litellm.store_audit_logs or get_secret_bool(
"LITELLM_STORE_AUDIT_LOGS"
)
if store_audit_logs is not True:
return
from litellm.proxy.proxy_server import premium_user, prisma_client
@ -70,8 +81,6 @@ async def create_audit_log_for_update(request_data: LiteLLM_AuditLogs):
if premium_user is not True:
return
if litellm.store_audit_logs is not True:
return
if prisma_client is None:
raise Exception("prisma_client is None, no DB connected")