Skip to content

[Bug] OpenAIResponses streaming keeps only the last url citation #10588

Open

Description

@BlueX888

Description

When streaming a response from the OpenAI Responses API, only the last url citation survives. The Responses API emits one response.output_text.annotation.added event per annotation, and the stream parser (OpenAIResponses._parse_provider_response_delta) builds a fresh ModelResponse for every event, so each event's model_response.citations holds exactly one annotation. The base stream accumulator then replaces the accumulated citations with each delta's citations (libs/agno/agno/models/base.py:1960), and the final assistant message is populated from that accumulator (libs/agno/agno/models/base.py:1907). With N annotation events, the last one wins and the first N-1 are lost.

The non-streaming parse does not have this problem: it collects every annotation from the response output into one Citations object (libs/agno/agno/models/openai/responses.py:1352-1368), so response() and response_stream() disagree on the same reply.

Steps to Reproduce

import json

import httpx
from agno.agent import Agent
from agno.models.openai.responses import OpenAIResponses
from openai import OpenAI

citations = [
    {"type": "url_citation", "url": "https://a.example", "title": "A", "start_index": 0, "end_index": 5},
    {"type": "url_citation", "url": "https://b.example", "title": "B", "start_index": 0, "end_index": 5},
    {"type": "url_citation", "url": "https://c.example", "title": "C", "start_index": 0, "end_index": 5},
]
response = {
    "id": "resp_citations",
    "object": "response",
    "created_at": 0,
    "model": "gpt-4.1-mini",
    "status": "completed",
    "error": None,
    "usage": None,
    "output": [
        {
            "type": "message",
            "id": "msg_citations",
            "role": "assistant",
            "status": "completed",
            "content": [{"type": "output_text", "text": "Hello world", "annotations": citations}],
        }
    ],
}


def handle(request):
    payload = json.loads(request.content)
    if payload.get("stream"):
        events = [
            {"type": "response.created", "response": response},
            {
                "type": "response.output_text.delta",
                "item_id": "msg_citations",
                "output_index": 0,
                "content_index": 0,
                "delta": "Hello world",
            },
        ]
        events.extend(
            {
                "type": "response.output_text.annotation.added",
                "item_id": "msg_citations",
                "output_index": 0,
                "content_index": 0,
                "annotation_index": index,
                "sequence_number": index + 2,
                "annotation": citation,
            }
            for index, citation in enumerate(citations)
        )
        events.append({"type": "response.completed", "response": response})
        body = "".join(f"data: {json.dumps(event)}\n\n" for event in events)
        return httpx.Response(200, text=body, headers={"content-type": "text/event-stream"})
    return httpx.Response(200, json=response)


transport = httpx.MockTransport(handle)
with OpenAI(api_key="test", http_client=httpx.Client(transport=transport)) as client:
    model = OpenAIResponses(id="gpt-4.1-mini", client=client)
    agent = Agent(model=model)
    for event in agent.run(input="cite three sources", stream=True):
        pass

print([c.url for c in event.citations.urls])

The same asymmetry shows up directly at the Model API: after model.response_stream(messages=messages), messages[-1].citations.urls holds only the last url, while model.response(messages=messages) keeps all three.

Expected Behavior

The streamed run output carries every url citation the model returned: ['https://a.example', 'https://b.example', 'https://c.example'], matching the non-streaming result.

Actual Behavior

The streamed run output only carries the last one: ['https://c.example'].

Screenshots or Logs (if applicable)

# unpatched, streamed:
FINAL_EVENT_CITATION_URLS=['https://c.example']
# non-streaming (same mocked reply):
['https://a.example', 'https://b.example', 'https://c.example']

Environment

  • OS: macOS 26 (arm64)
  • Agno Version: 3.0.11 (main @ 9f697b2)
  • External Dependency Versions: openai 3.19.2
  • Additional Environment Details: Python 3.12.14

Possible Solutions (optional)

Accumulate the annotations on the assistant message across events — the same way tool_calls are already accumulated in _parse_provider_response_delta (libs/agno/agno/models/openai/responses.py:1505-1507) — and emit the cumulative Citations on each yielded delta so the accumulator ends with the complete list.

Additional Context

Any setup that reads citations after a streamed OpenAI Responses run (agent run events, run output citations, assistant message citations) is affected.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions