@@ -1624,3 +1624,33 @@ async def test_cache_point_filtering():
16241624 # CachePoint should be filtered out, message should still be valid
16251625 assert len (messages ) == 1
16261626 assert messages [0 ]['role' ] == 'user'
1627+
1628+
1629+ async def test_bedrock_empty_model_response_skipped (bedrock_provider : BedrockProvider ):
1630+ """Test that ModelResponse with empty parts (e.g. content_filtered) is skipped in message mapping."""
1631+ model = BedrockConverseModel ('us.amazon.nova-micro-v1:0' , provider = bedrock_provider )
1632+
1633+ # Create a message history that includes a ModelResponse with empty parts
1634+ req = [
1635+ ModelRequest (parts = [UserPromptPart (content = 'Hello' )]),
1636+ ModelResponse (
1637+ parts = [],
1638+ usage = RequestUsage (input_tokens = 100 , output_tokens = 1 ),
1639+ model_name = 'us.amazon.nova-micro-v1:0' ,
1640+ provider_name = 'bedrock' ,
1641+ provider_details = {'finish_reason' : 'content_filtered' },
1642+ finish_reason = 'content_filter' ,
1643+ ),
1644+ ModelRequest (parts = [UserPromptPart (content = 'Follow up question' )]),
1645+ ]
1646+
1647+ # Call the mapping function directly
1648+ _ , bedrock_messages = await model ._map_messages (req , ModelRequestParameters ()) # type: ignore[reportPrivateUsage]
1649+
1650+ # The empty ModelResponse should be skipped, so we should only have 2 user messages
1651+ # that get merged into one since they're consecutive after the empty response is skipped
1652+ assert bedrock_messages == snapshot (
1653+ [
1654+ {'role' : 'user' , 'content' : [{'text' : 'Hello' }, {'text' : 'Follow up question' }]},
1655+ ]
1656+ )
0 commit comments