Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(bedrock): keep the provider status code on unprocessable image errors (#43416)

devin-ai-integration[bot]b831e9b4ac8a
diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py
index f09dd9fe75a..0fdfb301291 100644
--- a/litellm/litellm_core_utils/exception_mapping_utils.py
+++ b/litellm/litellm_core_utils/exception_mapping_utils.py
@@ -954,7 +954,7 @@ def _map_bedrock_exception(
llm_provider="bedrock",
response=getattr(original_exception, "response", None),
)
- elif "Could not process image" in error_str:
+ elif "Could not process image" in error_str and getattr(original_exception, "status_code", 500) == 500:
raise litellm.InternalServerError(
message=f"BedrockException - {error_str}",
model=model,
diff --git a/tests/unit/litellm_core_utils/test_exception_mapping_utils.py b/tests/unit/litellm_core_utils/test_exception_mapping_utils.py
index 9fce0441a58..9de768ea47b 100644
--- a/tests/unit/litellm_core_utils/test_exception_mapping_utils.py
+++ b/tests/unit/litellm_core_utils/test_exception_mapping_utils.py
@@ -1280,7 +1280,7 @@ def test_bedrock_500_preserves_provider_response_headers():
"bedrock",
400,
'{"message":"Could not process image"}',
- litellm.InternalServerError,
+ litellm.BadRequestError,
),
],
)
@@ -1313,6 +1313,41 @@ def test_bedrock_classified_errors_preserve_provider_response_headers(
assert exc_info.value.response.headers["x-amzn-requestid"] == "req-classified"
+ "status_code, expected_exception",
+ [
+ (400, litellm.BadRequestError),
+ (503, litellm.ServiceUnavailableError),
+ (500, litellm.InternalServerError),
+ ],
+)
+def test_bedrock_unprocessable_image_keeps_provider_status_code(status_code, expected_exception):
+ """An unprocessable image maps to the status Bedrock sent, so the 400 it returns stays a client error."""
+ provider_message = '{"message":"The model returned the following errors: Could not process image"}'
+ provider_response = httpx.Response(
+ status_code=status_code,
+ text=provider_message,
+ request=httpx.Request("POST", "https://bedrock-runtime.us-east-1.amazonaws.com/"),
+ )
+ original_exception = BedrockError(
+ status_code=status_code,
+ message=provider_message,
+ headers=provider_response.headers,
+ response=provider_response,
+ )
+
+ with pytest.raises(expected_exception) as exc_info:
+ exception_type(
+ model="anthropic.claude-haiku-4-5-20251001-v1:0",
+ original_exception=original_exception,
+ custom_llm_provider="bedrock",
+ completion_kwargs={},
+ extra_kwargs={},
+ )
+
+ assert exc_info.value.status_code == status_code
+
+
@pytest.mark.parametrize(
"status_code, provider_message",
[