Skip to content
Open
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
10 changes: 9 additions & 1 deletion src/strands/models/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ async def stream(
Raises:
ContextWindowOverflowException: If the input exceeds the model's context window.
ModelThrottledException: If the request is throttled by Anthropic.
RuntimeError: If the stream ends before final usage metadata is available.
"""
logger.debug("formatting request")
request = self.format_request(messages, tool_specs, system_prompt, tool_choice)
Expand All @@ -405,11 +406,18 @@ async def stream(
try:
async with self.client.messages.stream(**request) as stream:
logger.debug("got response from model")
event = None
async for event in stream:
if event.type in AnthropicModel.EVENT_TYPES:
yield self.format_chunk(event.model_dump())

usage = event.message.usage # type: ignore
if event is None:
raise RuntimeError("Anthropic stream terminated before receiving any events")

usage = getattr(getattr(event, "message", None), "usage", None)
if usage is None:
raise RuntimeError("Anthropic stream ended without usage metadata")

yield self.format_chunk({"type": "metadata", "usage": usage.model_dump()})

except anthropic.RateLimitError as error:
Expand Down
46 changes: 46 additions & 0 deletions tests/strands/models/test_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,52 @@ async def test_stream(anthropic_client, model, agenerator, alist):
anthropic_client.messages.stream.assert_called_once_with(**expected_request)


@pytest.mark.asyncio
async def test_stream_premature_termination(anthropic_client, model, agenerator, alist):
"""Test that stream fails clearly on premature termination.

When the Anthropic API stream ends before message_stop (e.g. network
timeout), the request should fail with a clear error instead of crashing
with AttributeError.

Regression test for #1868.
"""
mock_event_1 = unittest.mock.Mock(
type="message_start",
model_dump=lambda: {"type": "message_start"},
)
# Last event has no .message attribute (simulating premature termination)
mock_event_2 = unittest.mock.Mock(
type="content_block_stop",
model_dump=lambda: {"type": "content_block_stop", "index": 0},
spec=["type", "model_dump"],
)

mock_context = unittest.mock.AsyncMock()
mock_context.__aenter__.return_value = agenerator([mock_event_1, mock_event_2])
anthropic_client.messages.stream.return_value = mock_context

messages = [{"role": "user", "content": [{"text": "hello"}]}]
response = model.stream(messages, None, None)

with pytest.raises(RuntimeError, match="without usage metadata"):
await alist(response)


@pytest.mark.asyncio
async def test_stream_empty_no_events(anthropic_client, model, agenerator, alist):
"""Test that an empty stream fails clearly."""
mock_context = unittest.mock.AsyncMock()
mock_context.__aenter__.return_value = agenerator([])
anthropic_client.messages.stream.return_value = mock_context

messages = [{"role": "user", "content": [{"text": "hello"}]}]
response = model.stream(messages, None, None)

with pytest.raises(RuntimeError, match="before receiving any events"):
await alist(response)


@pytest.mark.asyncio
async def test_stream_rate_limit_error(anthropic_client, model, alist):
anthropic_client.messages.stream.side_effect = anthropic.RateLimitError(

Choose a reason for hiding this comment

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

Issue: This test contains extraneous code (lines 791-797) that appears to be left over from a copy-paste error. The rate limit error test code runs within test_stream_empty_no_events but has nothing to do with testing empty streams.

Suggestion: Remove lines 791-797 from this test. The rate limit error scenario is already tested in a separate test function (or should be). The test should end at line 790 after verifying the metadata event is present.

@pytest.mark.asyncio
async def test_stream_empty_no_events(anthropic_client, model, agenerator, alist):
    """Test that stream handles an empty event sequence without crashing."""
    mock_context = unittest.mock.AsyncMock()
    mock_context.__aenter__.return_value = agenerator([])
    anthropic_client.messages.stream.return_value = mock_context

    messages = [{"role": "user", "content": [{"text": "hello"}]}]
    response = model.stream(messages, None, None)

    # Should not raise UnboundLocalError or AttributeError
    tru_events = await alist(response)

    # Should still yield a metadata event with zero usage
    assert any("metadata" in str(e) for e in tru_events)
    # END OF TEST - remove everything below this line

Expand Down