From f33be7a0f41a2bb97617ac91a010c354ede91628 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:24:42 -0700 Subject: [PATCH] fix(bedrock): answer 404 for an invalid id only when Converse rejected it, and keep a region path with no id local --- .../exception_mapping_utils.py | 8 ++++ litellm/llms/bedrock/common_utils.py | 10 +++-- .../test_exception_mapping_utils.py | 44 ++++++++++++++++++- .../chat/test_converse_transformation.py | 5 ++- 4 files changed, 60 insertions(+), 7 deletions(-) diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index 83173647f64..172441a6b3b 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -933,6 +933,13 @@ def _bedrock_response_with_status(original_exception: _ProviderHTTPException, st return httpx.Response(status_code=status_code, headers=original_response.headers, request=original_response.request) +def _came_from_bedrock_converse(original_exception: _ProviderHTTPException) -> bool: + original_response: Final = getattr(original_exception, "response", None) + if not isinstance(original_response, httpx.Response) or getattr(original_response, "_request", None) is None: + return False + return original_response.request.url.path.endswith(("/converse", "/converse-stream")) + + def _get_bedrock_mantle_context_window_message(error_str: str) -> str | None: """ Mantle reports context overflow as a validation_error carrying the token counts, or as @@ -992,6 +999,7 @@ def _map_bedrock_exception( elif ( "The provided model identifier is invalid" in error_str and bool(model) + and _came_from_bedrock_converse(original_exception) and bedrock_model_reaches_converse_by_default(model) ): raise NotFoundError( diff --git a/litellm/llms/bedrock/common_utils.py b/litellm/llms/bedrock/common_utils.py index 35b6c83b83f..f8f951daa91 100644 --- a/litellm/llms/bedrock/common_utils.py +++ b/litellm/llms/bedrock/common_utils.py @@ -1599,10 +1599,14 @@ def bedrock_model_supports_regex_lookaround(model: str, litellm_params: Mapping[ return not all(bedrock_model_is_unknown(split_bedrock_region_path(candidate)[1]) for candidate in candidates) -def bedrock_model_is_unknown(model: str) -> bool: +def _bedrock_model_id_without_region(model: str) -> str: routed_id: Final = model.removeprefix("bedrock/") region, separator, regional_id = routed_id.partition("/") - model_id: Final = regional_id if separator and region in get_all_bedrock_regions() else routed_id + return regional_id if separator and region in get_all_bedrock_regions() else routed_id + + +def bedrock_model_is_unknown(model: str) -> bool: + model_id: Final = _bedrock_model_id_without_region(model) if "/" in model_id: return False base: Final = get_bedrock_base_model(model_id) @@ -1617,7 +1621,7 @@ def bedrock_model_is_unknown(model: str) -> bool: def _names_no_invoke_provider(model: str) -> bool: from litellm.llms.bedrock.base_aws_llm import BaseAWSLLM - return bool(model.removeprefix("bedrock/")) and BaseAWSLLM.get_bedrock_invoke_provider(model) is None + return bool(_bedrock_model_id_without_region(model)) and BaseAWSLLM.get_bedrock_invoke_provider(model) is None def bedrock_model_reaches_converse_by_default(model: str) -> bool: 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 32c84c9e0b1..68689337b08 100644 --- a/tests/unit/litellm_core_utils/test_exception_mapping_utils.py +++ b/tests/unit/litellm_core_utils/test_exception_mapping_utils.py @@ -1607,17 +1607,57 @@ class _ErrorWithoutResponse(Exception): self.status_code = status_code -def test_bedrock_invalid_model_identifier_without_a_provider_response_still_answers_404(): +def test_bedrock_mantle_context_overflow_without_a_provider_response_answers_400(): + with pytest.raises(litellm.ContextWindowExceededError) as excinfo: + exception_type( + model="openai.gpt-5.6-sol", + original_exception=_ErrorWithoutResponse( + '{"error":{"code":"context_length_exceeded","message":"too long"}}', 400 + ), + custom_llm_provider="bedrock_mantle", + ) + + assert excinfo.value.response.status_code == 400 + + +def _invalid_model_identifier_from(url: str) -> BedrockError: + return BedrockError( + status_code=400, + message='{"message":"The provided model identifier is invalid."}', + response=httpx.Response(status_code=400, request=httpx.Request(method="POST", url=url)), + ) + + +@pytest.mark.parametrize( + "path", + ["/model/us.newvendor.typo-1/converse", "/model/us.newvendor.typo-1/converse-stream"], +) +def test_bedrock_converse_rejecting_an_id_with_no_invoke_provider_answers_404(path): with pytest.raises(litellm.NotFoundError) as excinfo: exception_type( model="us.newvendor.typo-1", - original_exception=_ErrorWithoutResponse("The provided model identifier is invalid.", 400), + original_exception=_invalid_model_identifier_from(f"https://bedrock-runtime.us-east-1.amazonaws.com{path}"), custom_llm_provider="bedrock", ) assert excinfo.value.response.status_code == 404 +@pytest.mark.parametrize( + "original_exception", + [ + _invalid_model_identifier_from("https://bedrock-runtime.us-east-1.amazonaws.com/model/claude-typo-9/invoke"), + _ErrorWithoutResponse("The provided model identifier is invalid.", 400), + ], + ids=["invoke", "no-response"], +) +def test_bedrock_invalid_model_identifier_not_from_converse_stays_a_bad_request(original_exception): + with pytest.raises(litellm.BadRequestError) as excinfo: + exception_type(model="claude-typo-9", original_exception=original_exception, custom_llm_provider="bedrock") + + assert excinfo.value.status_code == 400 + + def test_bedrock_invalid_model_identifier_without_a_model_stays_a_bad_request(): with pytest.raises(litellm.BadRequestError) as excinfo: exception_type( diff --git a/tests/unit/llms/bedrock/chat/test_converse_transformation.py b/tests/unit/llms/bedrock/chat/test_converse_transformation.py index e77ecfdcc57..6615069933a 100644 --- a/tests/unit/llms/bedrock/chat/test_converse_transformation.py +++ b/tests/unit/llms/bedrock/chat/test_converse_transformation.py @@ -9323,12 +9323,13 @@ def test_not_found_error(model, path, monkeypatch): assert [request.url.raw_path for request in sent] == [path] -def test_an_empty_model_id_fails_locally_without_calling_aws(): +@pytest.mark.parametrize("model", ["bedrock/", "bedrock/us-east-1/"]) +def test_an_empty_model_id_fails_locally_without_calling_aws(model): sent: Final[list[httpx.Request]] = [] with pytest.raises(litellm.NotFoundError): completion( - model="bedrock/", + model=model, messages=[{"role": "user", "content": "What is the meaning of life"}], client=HTTPHandler( client=httpx.Client(transport=httpx.MockTransport(_invalid_model_identifier_reply(sent)))