From 78e66e2a8bcabe27d0216515377424f7a1c2c74e Mon Sep 17 00:00:00 2001 From: ramonpaolo Date: Thu, 17 Sep 2026 18:41:34 -0300 Subject: [PATCH] fix(dashscope): classify data inspection errors --- .../exception_mapping_utils.py | 1 + .../test_exception_mapping_utils.py | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index 70675966dfc..5a7e21b6388 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -335,6 +335,7 @@ def _map_openai_exception( ("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()) + or (custom_llm_provider == "dashscope" and "datainspectionfailed" in error_str.lower()) ): raise ContentPolicyViolationError( message=f"ContentPolicyViolationError: {exception_provider} - {message}", 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 acc6248bf3e..a053191ec05 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 @@ -342,6 +342,29 @@ def test_openai_compatible_400_with_bare_429_in_body_maps_to_bad_request(): assert excinfo.value.llm_provider == "deepinfra" +def test_dashscope_data_inspection_failure_maps_to_content_policy_violation(): + error_message = ( + '{"error":{"message":"InternalError.Algo.DataInspectionFailed: ' + 'Input text data may contain inappropriate content.",' + '"type":"invalid_request_error","code":"invalid_request_error"}}' + ) + original_exception = OpenAIError( + status_code=400, + message=error_message, + headers={}, + ) + + with pytest.raises(litellm.ContentPolicyViolationError) as excinfo: + exception_type( + model="dashscope/qwen-plus", + original_exception=original_exception, + custom_llm_provider="dashscope", + ) + + assert excinfo.value.status_code == 400 + assert excinfo.value.llm_provider == "dashscope" + + def test_openai_compatible_429_still_maps_to_rate_limit(): """A real 429 still maps to RateLimitError.""" original_exception = OpenAIError(