mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(bedrock): answer 404 for an invalid id only when Converse rejected it, and keep a region path with no id local
This commit is contained in:
parent
8cd06e0db5
commit
f33be7a0f4
4 changed files with 60 additions and 7 deletions
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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)))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue