|
21 | 21 | DEFAULT_BEDROCK_REGION, |
22 | 22 | DEFAULT_READ_TIMEOUT, |
23 | 23 | ) |
24 | | -from strands.types.exceptions import ModelThrottledException |
| 24 | +from strands.types.exceptions import ContextWindowOverflowException, ModelThrottledException |
25 | 25 | from strands.types.tools import ToolSpec |
26 | 26 |
|
27 | 27 | 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 |
1517 | 1517 | ] |
1518 | 1518 |
|
1519 | 1519 |
|
| 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 | + |
1520 | 1545 | @pytest.mark.asyncio |
1521 | 1546 | async def test_stream_logging(bedrock_client, model, messages, caplog, alist): |
1522 | 1547 | """Test that stream method logs debug messages at the expected stages.""" |
|
0 commit comments