From 896b29e7d21f4523089dbff1ad47d0d9db95f2c1 Mon Sep 17 00:00:00 2001 From: liracle Date: Sat, 29 Aug 2026 17:11:24 +0800 Subject: [PATCH] test(responses): cover annotations buffered during the reasoning item An annotation that arrives while the message item is still deferred (the stream is inside the reasoning item) is held in the buffer and flushed at the message announcement. The other annotation tests announce the item in the same chunk that carries the annotation, so _drain_pending_annotation _events never ran with a non-empty buffer -- the two uncovered lines codecov flagged. --- .../test_streaming_event_order.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_streaming_event_order.py b/tests/test_litellm/responses/litellm_completion_transformation/test_streaming_event_order.py index 22095d500fb..0ab78b9c7d0 100644 --- a/tests/test_litellm/responses/litellm_completion_transformation/test_streaming_event_order.py +++ b/tests/test_litellm/responses/litellm_completion_transformation/test_streaming_event_order.py @@ -352,6 +352,35 @@ async def test_annotation_only_stream_still_delivers_the_annotation(): ) +@pytest.mark.asyncio +async def test_annotation_during_reasoning_is_drained_at_announcement(): + """ + An annotation arriving while the message item is still deferred (the + stream is inside the reasoning item) is buffered and flushed when the + message item is finally announced, so it still lands after that item's + output_item.added instead of being stranded in the buffer. + """ + events = await _collect_events( + [ + _chunk(reasoning_content="Thinking", annotations=[URL_CITATION_ANNOTATION]), + _chunk(content="Answer"), + _chunk(finish_reason="stop"), + ] + ) + types = [_event_type(event) for event in events] + annotation_added = str(ResponsesAPIStreamEvents.OUTPUT_TEXT_ANNOTATION_ADDED) + assert annotation_added in types, f"buffered annotation was dropped: types={types}" + + message_added_idx = next( + i + for i, event in enumerate(events) + if _event_type(event) == str(ResponsesAPIStreamEvents.OUTPUT_ITEM_ADDED) and _item_type(event) == "message" + ) + assert types.index(annotation_added) > message_added_idx, ( + f"drained annotation must follow the message output_item.added: types={types}" + ) + + @pytest.mark.asyncio async def test_annotation_on_the_final_chunk_is_not_dropped(): """