From 9e9f6437be11b74e4336a5df6dff4fec9ef19934 Mon Sep 17 00:00:00 2001 From: Nikhil Soman Date: Wed, 23 Sep 2026 01:59:52 +0530 Subject: [PATCH] fix(azure): map Azure content_filter code and curly-quote policy messages to ContentPolicyViolationError (#42247) --- .../exception_mapping_utils.py | 12 +++--- .../test_exception_mapping_utils.py | 2 + .../azure/test_azure_exception_mapping.py | 40 +++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index da2f11f2593..bae1dbdc27f 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -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 ( diff --git a/tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py b/tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py index 5c5c2c9536b..595ebee7fee 100644 --- a/tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py +++ b/tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py @@ -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: diff --git a/tests/test_litellm/llms/azure/test_azure_exception_mapping.py b/tests/test_litellm/llms/azure/test_azure_exception_mapping.py index 16560c7a1fa..c0438f31ace 100644 --- a/tests/test_litellm/llms/azure/test_azure_exception_mapping.py +++ b/tests/test_litellm/llms/azure/test_azure_exception_mapping.py @@ -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"