Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(azure-ai-agents): accumulate annotations from multiple text items in streaming

Sameer Kankute6fe3188af048
diff --git a/litellm/llms/azure_ai/agents/handler.py b/litellm/llms/azure_ai/agents/handler.py
index 5b779acb0d1..95c0a4c577c 100644
--- a/litellm/llms/azure_ai/agents/handler.py
+++ b/litellm/llms/azure_ai/agents/handler.py
@@ -700,7 +700,9 @@ class AzureAIAgentsHandler:
raw_annotations
)
if transformed:
- collected_annotations = transformed
+ if collected_annotations is None:
+ collected_annotations = []
+ collected_annotations.extend(transformed)
# Process message deltas - this is where the actual content comes
if current_event == "thread.message.delta":
diff --git a/tests/llm_translation/test_azure_agents.py b/tests/llm_translation/test_azure_agents.py
index 19ce49a3bc9..3e6b1e00a79 100644
--- a/tests/llm_translation/test_azure_agents.py
+++ b/tests/llm_translation/test_azure_agents.py
@@ -23,12 +23,14 @@ Example environment variables:
See: https://learn.microsoft.com/en-us/azure/ai-foundry/agents/quickstart
"""
+import json
import os
import sys
sys.path.insert(0, os.path.abspath("../.."))
import pytest
+from unittest.mock import MagicMock
import litellm
@@ -465,6 +467,166 @@ def test_azure_ai_agents_build_model_response_without_annotations():
assert getattr(result.choices[0].message, "annotations", None) is None
+async def test_azure_ai_agents_streaming_annotations_from_completed_message():
+ """
+ Test that annotations from thread.message.completed SSE events are collected
+ and attached to the final chunk's delta.
+
+ Ref: https://github.com/BerriAI/litellm/issues/19126
+ """
+ from litellm.llms.azure_ai.agents.handler import AzureAIAgentsHandler
+
+ handler = AzureAIAgentsHandler()
+
+ # SSE lines simulating a stream with annotations in thread.message.completed
+ completed_data = {
+ "content": [
+ {
+ "type": "text",
+ "text": {
+ "value": "According to [1], the answer is 42.",
+ "annotations": [
+ {
+ "type": "url_citation",
+ "text": "[1]",
+ "start_index": 12,
+ "end_index": 15,
+ "url_citation": {
+ "url": "https://example.com/citation",
+ "title": "Citation Source",
+ },
+ }
+ ],
+ },
+ }
+ ]
+ }
+
+ sse_lines = [
+ "event: thread.created",
+ "",
+ 'data: {"id": "thread_stream_123"}',
+ "",
+ "event: thread.message.delta",
+ "",
+ 'data: {"delta": {"content": [{"type": "text", "text": {"value": "According to [1], the answer is 42."}}]}}',
+ "",
+ "event: thread.message.completed",
+ "",
+ f"data: {json.dumps(completed_data)}",
+ "",
+ "data: [DONE]",
+ ]
+
+ async def mock_aiter_lines():
+ for line in sse_lines:
+ yield line
+
+ mock_response = MagicMock()
+ mock_response.aiter_lines = MagicMock(return_value=mock_aiter_lines())
+
+ chunks = []
+ async for chunk in handler._process_sse_stream(mock_response, "azure_ai/agents/asst_123"):
+ chunks.append(chunk)
+
+ # Should have content chunks + final [DONE] chunk
+ assert len(chunks) >= 1
+ final_chunk = chunks[-1]
+ assert final_chunk.choices[0].finish_reason == "stop"
+ assert final_chunk.choices[0].delta.annotations is not None
+ assert len(final_chunk.choices[0].delta.annotations) == 1
+ ann = final_chunk.choices[0].delta.annotations[0]
+ assert ann["type"] == "url_citation"
+ assert ann["url_citation"]["url"] == "https://example.com/citation"
+ assert ann["url_citation"]["title"] == "Citation Source"
+
+
+async def test_azure_ai_agents_streaming_accumulates_annotations_from_multiple_text_items():
+ """
+ Test that annotations from multiple text content items in thread.message.completed
+ are accumulated (not overwritten).
+
+ Ref: Greptile review on PR #23849
+ """
+ from litellm.llms.azure_ai.agents.handler import AzureAIAgentsHandler
+
+ handler = AzureAIAgentsHandler()
+
+ # Two text blocks, each with distinct citations
+ completed_data = {
+ "content": [
+ {
+ "type": "text",
+ "text": {
+ "value": "First source [1].",
+ "annotations": [
+ {
+ "type": "url_citation",
+ "text": "[1]",
+ "start_index": 12,
+ "end_index": 15,
+ "url_citation": {
+ "url": "https://example.com/first",
+ "title": "First",
+ },
+ }
+ ],
+ },
+ },
+ {
+ "type": "text",
+ "text": {
+ "value": "Second source [2].",
+ "annotations": [
+ {
+ "type": "url_citation",
+ "text": "[2]",
+ "start_index": 13,
+ "end_index": 16,
+ "url_citation": {
+ "url": "https://example.com/second",
+ "title": "Second",
+ },
+ }
+ ],
+ },
+ },
+ ]
+ }
+
+ sse_lines = [
+ "event: thread.created",
+ "",
+ 'data: {"id": "thread_multi"}',
+ "",
+ "event: thread.message.completed",
+ "",
+ f"data: {json.dumps(completed_data)}",
+ "",
+ "data: [DONE]",
+ ]
+
+ async def mock_aiter_lines():
+ for line in sse_lines:
+ yield line
+
+ mock_response = MagicMock()
+ mock_response.aiter_lines = MagicMock(return_value=mock_aiter_lines())
+
+ chunks = []
+ async for chunk in handler._process_sse_stream(mock_response, "azure_ai/agents/asst_123"):
+ chunks.append(chunk)
+
+ final_chunk = chunks[-1]
+ assert final_chunk.choices[0].delta.annotations is not None
+ assert len(final_chunk.choices[0].delta.annotations) == 2
+ urls = [a["url_citation"]["url"] for a in final_chunk.choices[0].delta.annotations]
+ assert "https://example.com/first" in urls
+ assert "https://example.com/second" in urls
+
+
@pytest.mark.asyncio
async def test_azure_ai_agents_conversation_continuity():
"""