fix(providers): preserve first SSE event after UTF-8 BOM

This commit is contained in:
pei711 authored and Xubin Ren committed 2026-10-18 20:10:51 +08:00
1 parent d0a1423ccf
commit 5fd79c25fa
2 files changed
+36

No files matched your search

@@ -309,6 +309,7 @@ def _extract_refusal_text_from_output(output: object) -> tuple[bool, str]:
async def iter_sse(response: httpx.Response) -> AsyncGenerator[dict[str, Any], None]: async def iter_sse(response: httpx.Response) -> AsyncGenerator[dict[str, Any], None]:
"""Yield parsed JSON events from a Responses API SSE stream.""" """Yield parsed JSON events from a Responses API SSE stream."""
buffer: list[str] = [] buffer: list[str] = []
first_line = True
def _flush() -> dict[str, Any] | None: def _flush() -> dict[str, Any] | None:
data_lines = [line[5:].strip() for line in buffer if line.startswith("data:")] data_lines = [line[5:].strip() for line in buffer if line.startswith("data:")]
@@ -325,6 +326,10 @@ async def iter_sse(response: httpx.Response) -> AsyncGenerator[dict[str, Any], N
return None return None
async for line in response.aiter_lines(): async for line in response.aiter_lines():
if first_line:
# SSE permits one UTF-8 BOM at the beginning of the stream.
line = line.removeprefix("\ufeff")
first_line = False
if line == "": if line == "":
if buffer: if buffer:
event = _flush() event = _flush()
+31
View File
@@ -5,6 +5,7 @@ from io import StringIO
from types import SimpleNamespace from types import SimpleNamespace
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
import httpx
import pytest import pytest
from loguru import logger from loguru import logger
from openai.types.responses import ResponseFunctionToolCall from openai.types.responses import ResponseFunctionToolCall
@@ -1071,6 +1072,36 @@ class _SseResponse:
class TestConsumeSse: class TestConsumeSse:
@pytest.mark.asyncio
@pytest.mark.parametrize("prefix", ["", "\ufeff"])
async def test_leading_utf8_bom_preserves_first_content_delta(self, prefix):
events = [
{"type": "response.output_text.delta", "delta": "Hello "},
{"type": "response.output_text.delta", "delta": "\ufeff世界"},
{"type": "response.completed", "response": {"status": "completed"}},
]
wire = prefix + "".join(
f"data: {json.dumps(event, ensure_ascii=False)}\n\n" for event in events
)
response = httpx.Response(
200,
headers={"content-type": "text/event-stream"},
content=wire.encode("utf-8"),
)
deltas = []
async def on_delta(delta):
deltas.append(delta)
content, tool_calls, finish_reason, _, _ = await consume_sse_with_reasoning(
response, on_content_delta=on_delta,
)
assert content == "Hello \ufeff世界"
assert deltas == ["Hello ", "\ufeff世界"]
assert tool_calls == []
assert finish_reason == "stop"
@pytest.mark.asyncio @pytest.mark.asyncio
@pytest.mark.parametrize("event_type", ["response.output_text.delta", "response.refusal.delta"]) @pytest.mark.parametrize("event_type", ["response.output_text.delta", "response.refusal.delta"])
async def test_eof_without_terminal_event_is_a_connection_error(self, event_type): async def test_eof_without_terminal_event_is_a_connection_error(self, event_type):