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

* feat: add image edit mapping for content policy violations

* test fix
This commit is contained in:
Ishaan Jaff
2025-05-23 21:57:43 -07:00
committed by GitHub
parent af930f22a3
commit 8d7e234efd
3 changed files with 42 additions and 33 deletions
+2 -2
View File
@@ -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,
@@ -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(
+28 -26
View File
@@ -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()
try:
prompt = """
Create a studio ghibli style image that combines all the reference images. Make sure the person looks like a CTO.
"""
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)
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)
# 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)
# 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