|
5 | 5 |
|
6 | 6 | import strands |
7 | 7 | from strands import Agent |
| 8 | +from strands.agent import NullConversationManager |
8 | 9 | from strands.models.anthropic import AnthropicModel |
| 10 | +from strands.types.content import ContentBlock, Message |
| 11 | +from strands.types.exceptions import ContextWindowOverflowException |
9 | 12 |
|
10 | 13 | """ |
11 | 14 | These tests only run if we have the anthropic api key |
@@ -152,3 +155,30 @@ def test_structured_output_multi_modal_input(agent, yellow_img, yellow_color): |
152 | 155 | tru_color = agent.structured_output(type(yellow_color), content) |
153 | 156 | exp_color = yellow_color |
154 | 157 | assert tru_color == exp_color |
| 158 | + |
| 159 | + |
| 160 | +@pytest.mark.asyncio |
| 161 | +def test_input_and_max_tokens_exceed_context_limit(): |
| 162 | + """Test that triggers 'input length and max_tokens exceed context limit' error.""" |
| 163 | + |
| 164 | + # Note that this test is written specifically in a style that allows us to swap out conversation_manager and |
| 165 | + # verify behavior |
| 166 | + |
| 167 | + model = AnthropicModel( |
| 168 | + model_id="claude-sonnet-4-20250514", |
| 169 | + max_tokens=64000, |
| 170 | + ) |
| 171 | + |
| 172 | + large_message = "This is a very long text. " * 10000 |
| 173 | + |
| 174 | + messages = [ |
| 175 | + Message(role="user", content=[ContentBlock(text=large_message)]), |
| 176 | + Message(role="assistant", content=[ContentBlock(text=large_message)]), |
| 177 | + Message(role="user", content=[ContentBlock(text=large_message)]), |
| 178 | + ] |
| 179 | + |
| 180 | + # NullConversationManager will propagate ContextWindowOverflowException directly instead of handling it |
| 181 | + agent = Agent(model=model, conversation_manager=NullConversationManager()) |
| 182 | + |
| 183 | + with pytest.raises(ContextWindowOverflowException): |
| 184 | + agent(messages) |
0 commit comments