Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/strands/models/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,10 @@ def _stream(
except ClientError as e:
error_message = str(e)

if e.response["Error"]["Code"] == "ThrottlingException":
if (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be cleaner to just .lower() both so we don't need the or

e.response["Error"]["Code"] == "ThrottlingException"
or e.response["Error"]["Code"] == "throttlingException"
):
raise ModelThrottledException(error_message) from e

if any(overflow_message in error_message for overflow_message in BEDROCK_CONTEXT_WINDOW_OVERFLOW_MESSAGES):
Expand Down
34 changes: 34 additions & 0 deletions tests/strands/models/test_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,40 @@ async def test_stream_throttling_exception_from_general_exception(bedrock_client
)


@pytest.mark.asyncio
async def test_stream_throttling_exception_lowercase(bedrock_client, model, messages, alist):
"""Test that lowercase throttlingException is converted to ModelThrottledException."""
error_message = "throttlingException: Rate exceeded for ConverseStream"
bedrock_client.converse_stream.side_effect = ClientError(
{"Error": {"Message": error_message, "Code": "throttlingException"}}, "Any"
)

with pytest.raises(ModelThrottledException) as excinfo:
await alist(model.stream(messages))

assert error_message in str(excinfo.value)
bedrock_client.converse_stream.assert_called_once_with(
modelId="m1", messages=messages, system=[], inferenceConfig={}
)


@pytest.mark.asyncio
async def test_stream_throttling_exception_lowercase_non_streaming(bedrock_client, messages, alist):
"""Test that lowercase throttlingException is converted to ModelThrottledException in non-streaming mode."""
error_message = "throttlingException: Rate exceeded for Converse"
bedrock_client.converse.side_effect = ClientError(
{"Error": {"Message": error_message, "Code": "throttlingException"}}, "Any"
)

model = BedrockModel(model_id="test-model", streaming=False)
with pytest.raises(ModelThrottledException) as excinfo:
await alist(model.stream(messages))

assert error_message in str(excinfo.value)
bedrock_client.converse.assert_called_once()
bedrock_client.converse_stream.assert_not_called()


@pytest.mark.asyncio
async def test_general_exception_is_raised(bedrock_client, model, messages, alist):
error_message = "Should be raised up"
Expand Down