From 09874cc83fb8ce9b73524668f5df617822df4d96 Mon Sep 17 00:00:00 2001 From: ishaan-jaff Date: Tue, 9 Jan 2024 16:33:03 +0530 Subject: [PATCH] (v0) add ContentPolicyViolationError --- litellm/__init__.py | 1 + litellm/exceptions.py | 15 +++++++++++++++ litellm/utils.py | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/litellm/__init__.py b/litellm/__init__.py index f848dd32435..7e96d10aa71 100644 --- a/litellm/__init__.py +++ b/litellm/__init__.py @@ -544,6 +544,7 @@ from .exceptions import ( ServiceUnavailableError, OpenAIError, ContextWindowExceededError, + ContentPolicyViolationError, BudgetExceededError, APIError, Timeout, diff --git a/litellm/exceptions.py b/litellm/exceptions.py index 3898a568301..4f9629e71c8 100644 --- a/litellm/exceptions.py +++ b/litellm/exceptions.py @@ -108,6 +108,21 @@ class ContextWindowExceededError(BadRequestError): # type: ignore ) # Call the base class constructor with the parameters it needs +class ContentPolicyViolationError(BadRequestError): # type: ignore + # Error code: 400 - {'error': {'code': 'content_policy_violation', 'message': 'Your request was rejected as a result of our safety system. Image descriptions generated from your prompt may contain text that is not allowed by our safety system. If you believe this was done in error, your request may succeed if retried, or by adjusting your prompt.', 'param': None, 'type': 'invalid_request_error'}} + def __init__(self, message, model, llm_provider, response: httpx.Response): + self.status_code = 400 + self.message = message + self.model = model + self.llm_provider = llm_provider + super().__init__( + message=self.message, + model=self.model, # type: ignore + llm_provider=self.llm_provider, # type: ignore + response=response, + ) # Call the base class constructor with the parameters it needs + + class ServiceUnavailableError(APIStatusError): # type: ignore def __init__(self, message, llm_provider, model, response: httpx.Response): self.status_code = 503 diff --git a/litellm/utils.py b/litellm/utils.py index 4520bee6219..11e0c028ec1 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -59,6 +59,7 @@ from .exceptions import ( ServiceUnavailableError, OpenAIError, ContextWindowExceededError, + ContentPolicyViolationError, Timeout, APIConnectionError, APIError, @@ -5559,6 +5560,17 @@ def exception_type( model=model, response=original_exception.response, ) + elif ( + "invalid_request_error" in error_str + and "content_policy_violation" in error_str + ): + exception_mapping_worked = True + raise Con( + message=f"OpenAIException - {original_exception.message}", + llm_provider="openai", + model=model, + response=original_exception.response, + ) elif hasattr(original_exception, "status_code"): exception_mapping_worked = True if original_exception.status_code == 401: