Skip to content

Commit 73865d3

Browse files
authored
fix (bug): retry on varying Bedrock throttlingexception cases (#1096)
1 parent 3446938 commit 73865d3

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/strands/models/bedrock.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,10 @@ def _stream(
715715
except ClientError as e:
716716
error_message = str(e)
717717

718-
if e.response["Error"]["Code"] == "ThrottlingException":
718+
if (
719+
e.response["Error"]["Code"] == "ThrottlingException"
720+
or e.response["Error"]["Code"] == "throttlingException"
721+
):
719722
raise ModelThrottledException(error_message) from e
720723

721724
if any(overflow_message in error_message for overflow_message in BEDROCK_CONTEXT_WINDOW_OVERFLOW_MESSAGES):

tests/strands/models/test_bedrock.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,40 @@ async def test_stream_throttling_exception_from_general_exception(bedrock_client
535535
)
536536

537537

538+
@pytest.mark.asyncio
539+
async def test_stream_throttling_exception_lowercase(bedrock_client, model, messages, alist):
540+
"""Test that lowercase throttlingException is converted to ModelThrottledException."""
541+
error_message = "throttlingException: Rate exceeded for ConverseStream"
542+
bedrock_client.converse_stream.side_effect = ClientError(
543+
{"Error": {"Message": error_message, "Code": "throttlingException"}}, "Any"
544+
)
545+
546+
with pytest.raises(ModelThrottledException) as excinfo:
547+
await alist(model.stream(messages))
548+
549+
assert error_message in str(excinfo.value)
550+
bedrock_client.converse_stream.assert_called_once_with(
551+
modelId="m1", messages=messages, system=[], inferenceConfig={}
552+
)
553+
554+
555+
@pytest.mark.asyncio
556+
async def test_stream_throttling_exception_lowercase_non_streaming(bedrock_client, messages, alist):
557+
"""Test that lowercase throttlingException is converted to ModelThrottledException in non-streaming mode."""
558+
error_message = "throttlingException: Rate exceeded for Converse"
559+
bedrock_client.converse.side_effect = ClientError(
560+
{"Error": {"Message": error_message, "Code": "throttlingException"}}, "Any"
561+
)
562+
563+
model = BedrockModel(model_id="test-model", streaming=False)
564+
with pytest.raises(ModelThrottledException) as excinfo:
565+
await alist(model.stream(messages))
566+
567+
assert error_message in str(excinfo.value)
568+
bedrock_client.converse.assert_called_once()
569+
bedrock_client.converse_stream.assert_not_called()
570+
571+
538572
@pytest.mark.asyncio
539573
async def test_general_exception_is_raised(bedrock_client, model, messages, alist):
540574
error_message = "Should be raised up"

0 commit comments

Comments
 (0)