Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion litellm/litellm_core_utils/exception_mapping_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2005,7 +2005,7 @@ def exception_type( # type: ignore # noqa: PLR0915
):
exception_mapping_worked = True
raise ContentPolicyViolationError(
message=f"litellm.ContentPolicyViolationError: AzureException - {message}",
message=f"litellm.ContentPolicyViolationError: AzureException - {error_str}",
llm_provider="azure",
model=model,
litellm_debug_info=extra_information,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import pytest
import httpx

from litellm.litellm_core_utils.exception_mapping_utils import ExceptionCheckers, exception_type
from litellm import ContentPolicyViolationError
from openai import BadRequestError

from litellm.litellm_core_utils.exception_mapping_utils import ExceptionCheckers

# Test cases for is_error_str_context_window_exceeded
# Tuple format: (error_message, expected_result)
Expand Down Expand Up @@ -28,4 +32,58 @@ def test_is_error_str_context_window_exceeded(error_str, expected):
"""
Tests the is_error_str_context_window_exceeded function with various error strings.
"""
assert ExceptionCheckers.is_error_str_context_window_exceeded(error_str) == expected
assert ExceptionCheckers.is_error_str_context_window_exceeded(error_str) == expected

def test_mapping_azure_content_policy_exception_contains_inner_error():
"""
Test mapping of Azure content policy violation error that contains 'innererror' and 'content_filter_result'
to the correct LiteLLM exception type.
"""

dummy_request = httpx.Request("POST", "https://example.com/v1/completions")

response = httpx.Response(
status_code=400,
json={
"error": {
"message": "The response was filtered due to the prompt triggering Azure OpenAI's content management policy.",
"type": None,
"param": "prompt",
"code": "content_filter",
"status": 400,
"innererror": {
"code": "ResponsibleAIPolicyViolation",
"content_filter_result": {
"hate": {"filtered": True, "severity": "high"},
"jailbreak": {"filtered": False, "detected": False},
"self_harm": {"filtered": False, "severity": "safe"},
"sexual": {"filtered": False, "severity": "safe"},
"violence": {"filtered": False, "severity": "medium"},
},
},
}
},
request=dummy_request,
)

bad_request_error = BadRequestError(
message=f"Error code: 400 - {response.json()}",
response=response,
body=response.json(),
)

with pytest.raises(ContentPolicyViolationError) as excinfo:
exception_type('gpt-4o', Exception(bad_request_error), 'azure')

mapped_exception = excinfo.value

assert mapped_exception.__class__.__name__ == "ContentPolicyViolationError"

message = str(mapped_exception)
assert message.startswith("litellm.BadRequestError:")
assert "litellm.ContentPolicyViolationError" in message
assert "AzureException" in message
assert "The response was filtered due to the prompt triggering Azure OpenAI's content management policy." in message
assert "innererror" in message
assert "content_filter_result" in message
assert "ResponsibleAIPolicyViolation" in message
Loading