The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(model_management_endpoints.py): add audit logs on model update (#12396)
Krish Dholakiab08ea835c4ac
diff --git a/litellm/proxy/_new_secret_config.yaml b/litellm/proxy/_new_secret_config.yamlindex 949116beaa9..8a18ff49c2a 100644--- a/litellm/proxy/_new_secret_config.yaml+++ b/litellm/proxy/_new_secret_config.yaml@@ -8,3 +8,7 @@ model_list:model: langfuse/langfuse-modelprompt_id: test-chat-promptprompt_version: 4+++litellm_settings:+ store_audit_logs: true\ No newline at end of filediff --git a/litellm/proxy/management_endpoints/model_management_endpoints.py b/litellm/proxy/management_endpoints/model_management_endpoints.pyindex b6a4465cabf..e4189c6ce3d 100644--- a/litellm/proxy/management_endpoints/model_management_endpoints.py+++ b/litellm/proxy/management_endpoints/model_management_endpoints.py@@ -3,7 +3,7 @@ Allow proxy admin to add/update/delete models in the dbCurrently 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 modelsawait 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_modelexcept 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(diff --git a/litellm/proxy/management_helpers/audit_logs.py b/litellm/proxy/management_helpers/audit_logs.pyindex d6c83c38561..b1b2fdf1ca9 100644--- a/litellm/proxy/management_helpers/audit_logs.py+++ b/litellm/proxy/management_helpers/audit_logs.py@@ -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:returnawait 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:returnfrom 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:- returnif prisma_client is None:raise Exception("prisma_client is None, no DB connected")