diff --git a/litellm/proxy/guardrails/guardrail_endpoints.py b/litellm/proxy/guardrails/guardrail_endpoints.py index a8e960d1b18..c08a7070f90 100644 --- a/litellm/proxy/guardrails/guardrail_endpoints.py +++ b/litellm/proxy/guardrails/guardrail_endpoints.py @@ -556,11 +556,12 @@ async def delete_guardrail( verbose_proxy_logger.error( f"Immediate sync: Failed to remove guardrail '{guardrail_name}' (ID: {guardrail_id}) from memory: {delete_error}" ) - # Rollback: re-create the DB entry so state stays consistent + # Rollback: re-create the DB entry with the ORIGINAL guardrail_id try: await GUARDRAIL_REGISTRY.add_guardrail_to_db( guardrail=cast(Guardrail, existing_guardrail), prisma_client=prisma_client, + guardrail_id=guardrail_id, ) except Exception: verbose_proxy_logger.error( diff --git a/litellm/proxy/guardrails/guardrail_registry.py b/litellm/proxy/guardrails/guardrail_registry.py index 0a9d715fa53..f945547c7c7 100644 --- a/litellm/proxy/guardrails/guardrail_registry.py +++ b/litellm/proxy/guardrails/guardrail_registry.py @@ -234,10 +234,18 @@ class GuardrailRegistry: ########### DB management helpers for guardrails ########### ############################################################ async def add_guardrail_to_db( - self, guardrail: Guardrail, prisma_client: PrismaClient + self, + guardrail: Guardrail, + prisma_client: PrismaClient, + guardrail_id: Optional[str] = None, ): """ - Add a guardrail to the database + Add a guardrail to the database. + + Args: + guardrail_id: If provided, the row is created with this specific ID + (used by rollback paths to restore a deleted row with + its original ID). """ try: guardrail_name = guardrail.get("guardrail_name") @@ -252,15 +260,19 @@ class GuardrailRegistry: litellm_params: str = safe_dumps(litellm_params_dict) guardrail_info: str = safe_dumps(guardrail.get("guardrail_info", {})) + create_data: Dict[str, Any] = { + "guardrail_name": guardrail_name, + "litellm_params": litellm_params, + "guardrail_info": guardrail_info, + "created_at": datetime.now(timezone.utc), + "updated_at": datetime.now(timezone.utc), + } + if guardrail_id is not None: + create_data["guardrail_id"] = guardrail_id + # Create guardrail in DB created_guardrail = await prisma_client.db.litellm_guardrailstable.create( - data={ - "guardrail_name": guardrail_name, - "litellm_params": litellm_params, - "guardrail_info": guardrail_info, - "created_at": datetime.now(timezone.utc), - "updated_at": datetime.now(timezone.utc), - } + data=create_data ) # Add guardrail_id to the returned guardrail object