Skip to content

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]

[Feat] Add content policy violation error mapping for image editd (#11113)

Ishaan Jaff8d7e234efdb5
diff --git a/litellm/images/main.py b/litellm/images/main.py
index cf62b3a3657..09db4aef20b 100644
--- a/litellm/images/main.py
+++ b/litellm/images/main.py
@@ -650,7 +650,7 @@ def image_edit(
except Exception as e:
raise litellm.exception_type(
- model=None,
+ model=model,
custom_llm_provider=custom_llm_provider,
original_exception=e,
completion_kwargs=local_vars,
@@ -728,7 +728,7 @@ async def aimage_edit(
return response
except Exception as e:
raise litellm.exception_type(
- model=None,
+ model=model,
custom_llm_provider=custom_llm_provider,
original_exception=e,
completion_kwargs=local_vars,
diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py
index e96c73e4272..c514ffd12f9 100644
--- a/litellm/litellm_core_utils/exception_mapping_utils.py
+++ b/litellm/litellm_core_utils/exception_mapping_utils.py
@@ -317,11 +317,18 @@ def exception_type( # type: ignore # noqa: PLR0915
litellm_debug_info=extra_information,
)
elif (
- "invalid_request_error" in error_str
- and "content_policy_violation" in error_str
- ) or (
- "Invalid prompt" in error_str
- and "violating our usage policy" in error_str
+ (
+ "invalid_request_error" in error_str
+ and "content_policy_violation" in error_str
+ )
+ or (
+ "Invalid prompt" in error_str
+ and "violating our usage policy" in error_str
+ )
+ or (
+ "request was rejected as a result of the safety system"
+ in error_str.lower()
+ )
):
exception_mapping_worked = True
raise ContentPolicyViolationError(
diff --git a/tests/image_gen_tests/test_image_edits.py b/tests/image_gen_tests/test_image_edits.py
index c05f4773145..1e16120122f 100644
--- a/tests/image_gen_tests/test_image_edits.py
+++ b/tests/image_gen_tests/test_image_edits.py
@@ -27,32 +27,34 @@ TEST_IMAGES = [
async def test_openai_image_edit_litellm_sdk(sync_mode):
from litellm import image_edit, aimage_edit
litellm._turn_on_debug()
-
- prompt = """
- Create a studio ghibli style image that combines all the reference images. Make sure the person looks like a CTO.
- """
-
- if sync_mode:
- result = image_edit(
- prompt=prompt,
- model="gpt-image-1",
- image=TEST_IMAGES,
- )
- else:
- result = await aimage_edit(
- prompt=prompt,
- model="gpt-image-1",
- image=TEST_IMAGES,
- )
- print("result from image edit", result)
-
- # Validate the response meets expected schema
- ImageResponse.model_validate(result)
-
- if isinstance(result, ImageResponse):
- image_base64 = result.data[0].b64_json
- image_bytes = base64.b64decode(image_base64)
-
- # Save the image to a file
- with open("test_image_edit.png", "wb") as f:
- f.write(image_bytes)
+ try:
+ prompt = """
+ Create a studio ghibli style image that combines all the reference images. Make sure the person looks like a CTO.
+ """
+
+ if sync_mode:
+ result = image_edit(
+ prompt=prompt,
+ model="gpt-image-1",
+ image=TEST_IMAGES,
+ )
+ else:
+ result = await aimage_edit(
+ prompt=prompt,
+ model="gpt-image-1",
+ image=TEST_IMAGES,
+ )
+ print("result from image edit", result)
+
+ # Validate the response meets expected schema
+ ImageResponse.model_validate(result)
+
+ if isinstance(result, ImageResponse):
+ image_base64 = result.data[0].b64_json
+ image_bytes = base64.b64decode(image_base64)
+
+ # Save the image to a file
+ with open("test_image_edit.png", "wb") as f:
+ f.write(image_bytes)
+ except litellm.ContentPolicyViolationError as e:
+ pass