mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-17 23:51:30 +00:00
* fix(bedrock): keep x-amzn-RequestId on chat error responses Bedrock chat error paths built BedrockError from only a status code and a message, so the provider response headers were gone before exception mapping ran and the proxy had nothing to forward. AWS support needs x-amzn-RequestId to investigate a server-side error. - converse and invoke chat handlers pass the real headers and response when they turn an httpx.HTTPStatusError into a BedrockError, and read the body through error_response_text so a streamed body nobody read does not throw - every bedrock chat get_error_class honors the headers it is already handed: invoke, moonshot, bedrock-hosted openai, agentcore and the invoke agent - BedrockError carries those headers into the response it synthesizes when a caller has headers but no response, skipping values httpx cannot carry - the bedrock 500 mapping forwards the provider response like its 4xx and 503 siblings instead of fabricating a blank one The proxy now returns llm_provider-x-amzn-requestid on Bedrock chat errors. * fix(bedrock): keep request-id on text-classified errors The context-window and image branches of _map_bedrock_exception built their litellm exception without the provider response, so a Bedrock 400 classified by its body text lost x-amzn-RequestId while the sibling branches kept it. Also narrows the new BedrockError types and trims its docstrings. * chore(bedrock): drop the docstrings on the new error helpers * fix(bedrock): keep request-id on every error path that has one The ticket's root cause is that every BedrockError raise site under litellm/llms/bedrock/ was built from status and message alone. The first commits covered the chat and invoke handlers; this covers the rest. Embeddings, rerank, image generation, image edit, count tokens, search and the transformation layers now hand on the provider response or its headers, and both bedrock_mantle configs return a BedrockError instead of the OpenAI error that drops them. Two blockers surfaced while verifying the streaming path. The trailing `except Exception` in make_call and make_sync_call swallowed the BedrockError raised a few lines above, relabelling a provider status as a 500, and the non-200 branch read an unread streamed body, which throws. The raise sites left alone have no provider response to carry: timeouts, credential and config errors, and mid-stream event frames. * fix(bedrock): forward provider headers from the count tokens route The count tokens route converts BedrockError into an HTTPException, and dropped the headers the handler had just kept, so that route still lost the request id. get_response_headers now takes a Mapping so an httpx.Headers can be handed to it without a copy. * fix(bedrock): classify every bedrock surface through BedrockError Eleven bedrock configs still inherited a provider-agnostic get_error_class that builds a blank response, so the request id was gone before the proxy read it. Claude platform, bedrock anthropic-messages, both image edit configs, passthrough, realtime, vector stores and agentcore search now return BedrockError, and a parametrized audit drives all 36 configs. * fix(proxy): keep provider headers on the httpx status error branch _handle_llm_api_exception forwards safe_headers on every branch except the httpx.HTTPStatusError one, which the bedrock passthrough route reaches, so the request id was dropped before the client saw the response. * fix(bedrock): keep the request id on the timeout mappings Timeout takes no response argument, so the three bedrock timeout branches dropped the provider headers even when the upstream answered 408 or 504 with an x-amzn-RequestId. They now ride on the exception, already llm_provider-prefixed, which is the form the proxy emits. * fix(bedrock): keep the provider response on mapped timeouts The previous round attached llm_provider-prefixed headers directly to the Timeout. That shadowed the raw upstream headers for _get_response_headers, so router cooldown and fallback cooldown stopped honouring retry-after on bedrock 408/504 replies. Give Timeout an optional response instead, the way every other mapped bedrock exception already carries one. Retry logic reads the raw retry-after off the response, and the proxy prefixes those headers on the way out, so clients still see llm_provider-x-amzn-requestid. * chore(bedrock): drop the explanatory comment on Timeout.response
371 lines
14 KiB
Python
371 lines
14 KiB
Python
import json
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
import litellm
|
|
from litellm.llms.bedrock.chat import BedrockConverseLLM
|
|
from litellm.llms.bedrock.chat.converse_handler import make_sync_call
|
|
from litellm.llms.bedrock.common_utils import _get_all_bedrock_regions
|
|
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
|
|
|
|
|
|
|
|
def test_encode_model_id_with_inference_profile():
|
|
"""
|
|
Test instance profile is properly encoded when used as a model
|
|
"""
|
|
test_model = "arn:aws:bedrock:us-east-1:12345678910:application-inference-profile/ujdtmcirjhevpi"
|
|
expected_model = "arn%3Aaws%3Abedrock%3Aus-east-1%3A12345678910%3Aapplication-inference-profile%2Fujdtmcirjhevpi"
|
|
bedrock_converse_llm = BedrockConverseLLM()
|
|
returned_model = bedrock_converse_llm.encode_model_id(test_model)
|
|
assert expected_model == returned_model
|
|
|
|
|
|
class TestBedrockRegionInModelPath:
|
|
"""
|
|
Tests for region extraction from bedrock/{region}/{model} path format.
|
|
|
|
When a user passes model="bedrock/ap-northeast-1/moonshotai.kimi-k2.5",
|
|
get_llm_provider strips "bedrock/" and passes "ap-northeast-1/moonshotai.kimi-k2.5"
|
|
to the converse handler. The handler must:
|
|
1. Strip the region from modelId (so AWS gets "moonshotai.kimi-k2.5", not "ap-northeast-1%2Fmoonshotai.kimi-k2.5")
|
|
2. Use the extracted region as aws_region_name for the API call
|
|
"""
|
|
|
|
@pytest.mark.parametrize(
|
|
"model,expected_model_id,expected_region",
|
|
[
|
|
# Region embedded in path — both modelId and region must be extracted
|
|
(
|
|
"ap-northeast-1/moonshotai.kimi-k2.5",
|
|
"moonshotai.kimi-k2.5",
|
|
"ap-northeast-1",
|
|
),
|
|
(
|
|
"us-east-1/moonshotai.kimi-k2.5",
|
|
"moonshotai.kimi-k2.5",
|
|
"us-east-1",
|
|
),
|
|
(
|
|
"us-west-2/anthropic.claude-haiku-4-5-20251001-v1:0",
|
|
"anthropic.claude-haiku-4-5-20251001-v1%3A0",
|
|
"us-west-2",
|
|
),
|
|
# No region in path — modelId unchanged, no region injected
|
|
(
|
|
"moonshotai.kimi-k2.5",
|
|
"moonshotai.kimi-k2.5",
|
|
None,
|
|
),
|
|
# Cross-region inference prefix (us., eu., ap.) — not a region path segment
|
|
(
|
|
"us.anthropic.claude-haiku-4-5-20251001-v1:0",
|
|
"us.anthropic.claude-haiku-4-5-20251001-v1%3A0",
|
|
None,
|
|
),
|
|
],
|
|
)
|
|
def test_region_and_model_id_extraction(
|
|
self, model, expected_model_id, expected_region
|
|
):
|
|
"""
|
|
Verify that completion() correctly extracts both modelId and aws_region_name
|
|
from the bedrock/{region}/{model} path format.
|
|
"""
|
|
bedrock_converse_llm = BedrockConverseLLM()
|
|
optional_params: dict = {}
|
|
|
|
# Simulate the modelId + region extraction logic from completion()
|
|
_model_for_id = model
|
|
_stripped = _model_for_id
|
|
for rp in ["bedrock/converse/", "bedrock/", "converse/"]:
|
|
if _stripped.startswith(rp):
|
|
_stripped = _stripped[len(rp) :]
|
|
break
|
|
|
|
_region_from_model = None
|
|
_potential_region = _stripped.split("/", 1)[0]
|
|
if _potential_region in _get_all_bedrock_regions() and "/" in _stripped:
|
|
_region_from_model = _potential_region
|
|
_stripped = _stripped.split("/", 1)[1]
|
|
_model_for_id = _stripped
|
|
|
|
for _nova_prefix in ["nova-2/", "nova/"]:
|
|
if _stripped.startswith(_nova_prefix):
|
|
_model_for_id = _model_for_id.replace(_nova_prefix, "", 1)
|
|
break
|
|
|
|
model_id = bedrock_converse_llm.encode_model_id(model_id=_model_for_id)
|
|
if _region_from_model is not None and "aws_region_name" not in optional_params:
|
|
optional_params["aws_region_name"] = _region_from_model
|
|
|
|
assert (
|
|
model_id == expected_model_id
|
|
), f"modelId mismatch for {model!r}: got {model_id!r}, expected {expected_model_id!r}"
|
|
assert (
|
|
optional_params.get("aws_region_name") == expected_region
|
|
), f"region mismatch for {model!r}: got {optional_params.get('aws_region_name')!r}, expected {expected_region!r}"
|
|
|
|
def test_explicit_aws_region_name_not_overridden(self):
|
|
"""
|
|
If aws_region_name is already set in optional_params, the region in the
|
|
model path must NOT override it.
|
|
"""
|
|
bedrock_converse_llm = BedrockConverseLLM()
|
|
optional_params = {"aws_region_name": "eu-west-1"}
|
|
model = "ap-northeast-1/moonshotai.kimi-k2.5"
|
|
|
|
_model_for_id = model
|
|
_stripped = model
|
|
_region_from_model = None
|
|
_potential_region = _stripped.split("/", 1)[0]
|
|
if _potential_region in _get_all_bedrock_regions() and "/" in _stripped:
|
|
_region_from_model = _potential_region
|
|
_stripped = _stripped.split("/", 1)[1]
|
|
_model_for_id = _stripped
|
|
|
|
model_id = bedrock_converse_llm.encode_model_id(model_id=_model_for_id)
|
|
if _region_from_model is not None and "aws_region_name" not in optional_params:
|
|
optional_params["aws_region_name"] = _region_from_model
|
|
|
|
# modelId is still correctly stripped
|
|
assert model_id == "moonshotai.kimi-k2.5"
|
|
# explicitly set region is preserved
|
|
assert optional_params["aws_region_name"] == "eu-west-1"
|
|
|
|
|
|
def _stream_completion_with_spied_iter_bytes(model: str, **kwargs) -> MagicMock:
|
|
mock_response = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.iter_bytes = MagicMock(return_value=iter([]))
|
|
client = HTTPHandler()
|
|
client.post = MagicMock(return_value=mock_response)
|
|
|
|
litellm.completion(
|
|
model=model,
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
stream=True,
|
|
client=client,
|
|
aws_access_key_id="fake",
|
|
aws_secret_access_key="fake",
|
|
aws_region_name="us-east-1",
|
|
**kwargs,
|
|
)
|
|
return mock_response.iter_bytes
|
|
|
|
|
|
def test_make_sync_call_does_not_rechunk_stream_by_default():
|
|
"""Re-chunking the event stream into fixed 1024-byte blocks holds small
|
|
early events in httpx's ByteChunker until 1024 bytes accumulate, delaying
|
|
time-to-first-chunk by the whole generation when Bedrock trickles bytes
|
|
(e.g. buffered tool-use streams)."""
|
|
response = MagicMock()
|
|
response.status_code = 200
|
|
client = MagicMock()
|
|
client.post = MagicMock(return_value=response)
|
|
|
|
make_sync_call(
|
|
client=client,
|
|
api_base="https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-sonnet-4-6/converse-stream",
|
|
headers={},
|
|
data="{}",
|
|
model="anthropic.claude-sonnet-4-6",
|
|
messages=[],
|
|
logging_obj=MagicMock(),
|
|
)
|
|
|
|
response.iter_bytes.assert_called_once_with(chunk_size=None)
|
|
|
|
|
|
def test_make_sync_call_honors_explicit_stream_chunk_size():
|
|
response = MagicMock()
|
|
response.status_code = 200
|
|
client = MagicMock()
|
|
client.post = MagicMock(return_value=response)
|
|
|
|
make_sync_call(
|
|
client=client,
|
|
api_base="https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-sonnet-4-6/converse-stream",
|
|
headers={},
|
|
data="{}",
|
|
model="anthropic.claude-sonnet-4-6",
|
|
messages=[],
|
|
logging_obj=MagicMock(),
|
|
stream_chunk_size=2048,
|
|
)
|
|
|
|
response.iter_bytes.assert_called_once_with(chunk_size=2048)
|
|
|
|
|
|
def _converse_response_body() -> dict:
|
|
return {
|
|
"output": {"message": {"role": "assistant", "content": [{"text": "hi"}]}},
|
|
"stopReason": "end_turn",
|
|
"usage": {"inputTokens": 1, "outputTokens": 1, "totalTokens": 2},
|
|
}
|
|
|
|
|
|
def test_converse_completion_forwards_bedrock_response_headers():
|
|
mock_response = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.json = MagicMock(return_value=_converse_response_body())
|
|
mock_response.text = json.dumps(_converse_response_body())
|
|
mock_response.headers = httpx.Headers({"x-amzn-requestid": "req-123"})
|
|
client = HTTPHandler()
|
|
client.post = MagicMock(return_value=mock_response)
|
|
|
|
response = litellm.completion(
|
|
model="bedrock/converse/anthropic.claude-haiku-4-5-20251001-v1:0",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
client=client,
|
|
aws_access_key_id="fake",
|
|
aws_secret_access_key="fake",
|
|
aws_region_name="us-east-1",
|
|
)
|
|
|
|
assert response._hidden_params["additional_headers"]["llm_provider-x-amzn-requestid"] == "req-123"
|
|
|
|
|
|
def test_converse_streaming_forwards_bedrock_response_headers():
|
|
mock_response = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.iter_bytes = MagicMock(return_value=iter([]))
|
|
mock_response.headers = httpx.Headers({"x-amzn-requestid": "req-456"})
|
|
client = HTTPHandler()
|
|
client.post = MagicMock(return_value=mock_response)
|
|
|
|
response = litellm.completion(
|
|
model="bedrock/converse/anthropic.claude-haiku-4-5-20251001-v1:0",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
stream=True,
|
|
client=client,
|
|
aws_access_key_id="fake",
|
|
aws_secret_access_key="fake",
|
|
aws_region_name="us-east-1",
|
|
)
|
|
|
|
assert response._hidden_params["additional_headers"]["llm_provider-x-amzn-requestid"] == "req-456"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_converse_completion_forwards_bedrock_response_headers():
|
|
mock_response = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.json = MagicMock(return_value=_converse_response_body())
|
|
mock_response.text = json.dumps(_converse_response_body())
|
|
mock_response.headers = httpx.Headers({"x-amzn-requestid": "req-abc"})
|
|
client = AsyncHTTPHandler()
|
|
client.post = AsyncMock(return_value=mock_response)
|
|
|
|
response = await litellm.acompletion(
|
|
model="bedrock/converse/anthropic.claude-haiku-4-5-20251001-v1:0",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
client=client,
|
|
aws_access_key_id="fake",
|
|
aws_secret_access_key="fake",
|
|
aws_region_name="us-east-1",
|
|
)
|
|
|
|
assert response._hidden_params["additional_headers"]["llm_provider-x-amzn-requestid"] == "req-abc"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_converse_streaming_forwards_bedrock_response_headers():
|
|
async def _no_bytes(chunk_size=None):
|
|
return
|
|
yield b""
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.aiter_bytes = _no_bytes
|
|
mock_response.headers = httpx.Headers({"x-amzn-requestid": "req-def"})
|
|
client = AsyncHTTPHandler()
|
|
client.post = AsyncMock(return_value=mock_response)
|
|
|
|
response = await litellm.acompletion(
|
|
model="bedrock/converse/anthropic.claude-haiku-4-5-20251001-v1:0",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
stream=True,
|
|
client=client,
|
|
aws_access_key_id="fake",
|
|
aws_secret_access_key="fake",
|
|
aws_region_name="us-east-1",
|
|
)
|
|
|
|
assert response._hidden_params["additional_headers"]["llm_provider-x-amzn-requestid"] == "req-def"
|
|
|
|
|
|
def test_completion_plumbs_stream_chunk_size_through_converse():
|
|
iter_bytes_spy = _stream_completion_with_spied_iter_bytes(
|
|
model="bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0"
|
|
)
|
|
iter_bytes_spy.assert_called_once_with(chunk_size=None)
|
|
|
|
iter_bytes_spy = _stream_completion_with_spied_iter_bytes(
|
|
model="bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0",
|
|
stream_chunk_size=2048,
|
|
)
|
|
iter_bytes_spy.assert_called_once_with(chunk_size=2048)
|
|
|
|
|
|
def _bedrock_error_response(status_code: int, request_id: str) -> httpx.Response:
|
|
return httpx.Response(
|
|
status_code=status_code,
|
|
headers={
|
|
"x-amzn-RequestId": request_id,
|
|
"x-amzn-ErrorType": "InternalServerException",
|
|
},
|
|
text=json.dumps({"message": "Amazon Bedrock is unable to process your request."}),
|
|
request=httpx.Request("POST", "https://bedrock-runtime.us-east-1.amazonaws.com/"),
|
|
)
|
|
|
|
|
|
def test_converse_completion_error_forwards_bedrock_response_headers():
|
|
error_response = _bedrock_error_response(500, "req-err-123")
|
|
client = HTTPHandler()
|
|
client.post = MagicMock(
|
|
side_effect=httpx.HTTPStatusError(
|
|
"server error",
|
|
request=error_response.request,
|
|
response=error_response,
|
|
)
|
|
)
|
|
|
|
with pytest.raises(litellm.ServiceUnavailableError) as exc_info:
|
|
litellm.completion(
|
|
model="bedrock/converse/anthropic.claude-haiku-4-5-20251001-v1:0",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
client=client,
|
|
aws_access_key_id="fake",
|
|
aws_secret_access_key="fake",
|
|
aws_region_name="us-east-1",
|
|
)
|
|
|
|
assert exc_info.value.response.headers["x-amzn-requestid"] == "req-err-123"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_converse_completion_error_forwards_bedrock_response_headers():
|
|
error_response = _bedrock_error_response(500, "req-err-456")
|
|
client = AsyncHTTPHandler()
|
|
client.post = AsyncMock(
|
|
side_effect=httpx.HTTPStatusError(
|
|
"server error",
|
|
request=error_response.request,
|
|
response=error_response,
|
|
)
|
|
)
|
|
|
|
with pytest.raises(litellm.ServiceUnavailableError) as exc_info:
|
|
await litellm.acompletion(
|
|
model="bedrock/converse/anthropic.claude-haiku-4-5-20251001-v1:0",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
client=client,
|
|
aws_access_key_id="fake",
|
|
aws_secret_access_key="fake",
|
|
aws_region_name="us-east-1",
|
|
)
|
|
|
|
assert exc_info.value.response.headers["x-amzn-requestid"] == "req-err-456"
|