fix(azure): map Azure content_filter code and curly-quote policy messages to ContentPolicyViolationError (#42247)

This commit is contained in:
Nikhil Soman 2026-09-23 01:59:52 +05:30
parent b673ee61e7
commit 9e9f6437be
3 changed files with 49 additions and 5 deletions

View file

@ -118,11 +118,13 @@ class ExceptionCheckers:
"""
Check if an error string indicates a content policy violation error.
"""
_lower: Final = error_str.lower()
_lower: Final = error_str.lower().replace("’", "'").replace("‘", "'")
known_exception_substrings: Final = [
"content_policy_violation",
"responsibleaipolicyviolation",
"the response was filtered due to the prompt triggering azure openai's content management",
"contentfiltered",
"content_filter",
"the response was filtered due to the prompt triggering azure openai",
"your task failed as a result of our safety system",
"the model produced invalid content",
"content_filter_policy",
@ -1953,9 +1955,9 @@ def _map_azure_exception(
# ResponsibleAIPolicyViolation which indicates a
# content policy violation even when the top-level
# code is generic (e.g. "invalid_request_error").
if azure_error_code != "content_policy_violation":
if azure_error_code not in ("content_policy_violation", "content_filter"):
_inner: Final = body_dict["error"].get("inner_error") or body_dict["error"].get("innererror")
if isinstance(_inner, dict) and _inner.get("code") == "ResponsibleAIPolicyViolation":
if isinstance(_inner, dict) and _inner.get("code") in ("ResponsibleAIPolicyViolation", "ContentFiltered"):
azure_error_code = "content_policy_violation"
else:
azure_error_code = body_dict.get("code")
@ -1986,7 +1988,7 @@ def _map_azure_exception(
litellm_debug_info=extra_information,
response=getattr(original_exception, "response", None),
)
elif azure_error_code == "content_policy_violation" or ExceptionCheckers.is_azure_content_policy_violation_error(
elif azure_error_code in ("content_policy_violation", "content_filter") or ExceptionCheckers.is_azure_content_policy_violation_error(
error_str
):
from litellm.llms.azure.exception_mapping import (

View file

@ -169,9 +169,11 @@ class TestExceptionCheckers:
error_strings = [
"invalid_request_error content_policy_violation occurred",
"The response was filtered due to the prompt triggering Azure OpenAI's content management policy",
"The response was filtered due to the prompt triggering Azure OpenAI’s content management policy",
"Your task failed as a result of our safety system detecting harmful content",
"The model produced invalid content that violates our policy",
"Request blocked due to content_filter_policy restrictions",
"Error code: 400 - {'error': {'message': 'content_filter error', 'code': 'content_filter', 'innererror': {'code': 'ContentFiltered'}}}",
]
for error_str in error_strings:

View file

@ -422,3 +422,43 @@ class TestAzureExceptionMapping:
error = exc_info.value
assert "encrypted_content_affinity" in error.message
assert "enable_pre_call_checks" in error.message
def test_azure_content_policy_violation_curly_apostrophe_and_content_filter_code(self):
"""Test that Azure content policy violation with unicode right single quotation mark (curly apostrophe U+2019)
and code='content_filter' is mapped to ContentPolicyViolationError (#42247)."""
import openai
msg = (
"The response was filtered due to the prompt triggering Azure OpenAI’s "
"content management policy. Please modify your prompt and retry."
)
mock_response = MagicMock()
mock_response.status_code = 400
mock_response.headers = {}
mock_exception = openai.BadRequestError(
message=f"Error code: 400 - {{'error': {{'message': '{msg}', 'type': 'invalid_request_error', 'param': 'prompt', 'code': 'content_filter', 'innererror': {{'code': 'ContentFiltered'}}}}}}",
response=mock_response,
body={
"error": {
"message": msg,
"type": "invalid_request_error",
"param": "prompt",
"code": "content_filter",
"innererror": {"code": "ContentFiltered"},
}
},
)
with pytest.raises(ContentPolicyViolationError) as exc_info:
exception_type(
model="azure/gpt-4o",
original_exception=mock_exception,
custom_llm_provider="azure",
)
e = exc_info.value
assert "content management policy" in str(e).lower()
assert e.provider_specific_fields is not None
assert e.provider_specific_fields["inner_error"]["code"] == "ContentFiltered"
assert e.body["code"] == "content_filter"