Skip to content

Commit 66fb308

Browse files
authored
fix(bedrock): add 'prompt is too long' to context window overflow mes… (#1663)
1 parent 18a349c commit 66fb308

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/strands/models/bedrock.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"Input is too long for requested model",
4545
"input length and `max_tokens` exceed context limit",
4646
"too many total text bytes",
47+
"prompt is too long",
4748
]
4849

4950
# Models that should include tool result status (include_tool_result_status = True)

tests/strands/models/test_bedrock.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
DEFAULT_BEDROCK_REGION,
2222
DEFAULT_READ_TIMEOUT,
2323
)
24-
from strands.types.exceptions import ModelThrottledException
24+
from strands.types.exceptions import ContextWindowOverflowException, ModelThrottledException
2525
from strands.types.tools import ToolSpec
2626

2727
FORMATTED_DEFAULT_MODEL_ID = DEFAULT_BEDROCK_MODEL_ID.format("us")
@@ -1517,6 +1517,31 @@ async def test_add_note_on_validation_exception_throughput(bedrock_client, model
15171517
]
15181518

15191519

1520+
@pytest.mark.parametrize(
1521+
"overflow_message",
1522+
[
1523+
"Input is too long for requested model",
1524+
"input length and `max_tokens` exceed context limit",
1525+
"too many total text bytes",
1526+
"prompt is too long: 903884 tokens > 200000 maximum",
1527+
],
1528+
)
1529+
@pytest.mark.asyncio
1530+
async def test_stream_context_window_overflow(overflow_message, bedrock_client, model, alist, messages):
1531+
"""Test that ClientError with overflow messages raises ContextWindowOverflowException."""
1532+
error_response = {
1533+
"Error": {
1534+
"Code": "ValidationException",
1535+
"Message": f"An error occurred (ValidationException) when calling the ConverseStream operation: "
1536+
f"The model returned the following errors: {overflow_message}",
1537+
}
1538+
}
1539+
bedrock_client.converse_stream.side_effect = ClientError(error_response, "ConverseStream")
1540+
1541+
with pytest.raises(ContextWindowOverflowException):
1542+
await alist(model.stream(messages))
1543+
1544+
15201545
@pytest.mark.asyncio
15211546
async def test_stream_logging(bedrock_client, model, messages, caplog, alist):
15221547
"""Test that stream method logs debug messages at the expected stages."""

0 commit comments

Comments
 (0)