From a3f39d16723a39a856ebb243754ab7874129f3c5 Mon Sep 17 00:00:00 2001 From: Twiink Date: Fri, 27 Nov 2026 13:33:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(stream):=20=E6=8C=89=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E5=86=99=E6=97=A0=E5=85=B3=E7=9A=84=E8=A7=84=E5=88=99=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E4=BA=8B=E4=BB=B6=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Twiink --- replicate/stream.py | 2 +- tests/test_stream.py | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/replicate/stream.py b/replicate/stream.py index e837abdc..96782829 100644 --- a/replicate/stream.py +++ b/replicate/stream.py @@ -78,7 +78,7 @@ def __init__( self.response = response self.use_file_output = use_file_output or True content_type, _, _ = response.headers["content-type"].partition(";") - if content_type != "text/event-stream": + if content_type.strip().lower() != "text/event-stream": raise ValueError( "Expected response Content-Type to be 'text/event-stream', " f"got {content_type!r}" diff --git a/tests/test_stream.py b/tests/test_stream.py index 561fa6ec..6b879fdb 100644 --- a/tests/test_stream.py +++ b/tests/test_stream.py @@ -1,8 +1,9 @@ +import httpx import pytest import replicate from replicate.exceptions import ReplicateError -from replicate.stream import ServerSentEvent +from replicate.stream import EventSource, ServerSentEvent from .conftest import skip_if_no_token @@ -71,3 +72,21 @@ async def test_stream_prediction(async_flag, record_mode): pytest.skip("Skipping test due to authentication error") else: raise e + + +@pytest.mark.parametrize("async_flag", [True, False]) +async def test_event_stream_media_type_is_case_insensitive(async_flag): + response = httpx.Response( + 200, + headers={"Content-Type": "Text/Event-Stream; charset=utf-8"}, + content=b"id: 1\nevent: output\ndata: hello\n\n", + ) + source = EventSource(replicate.Client(), response) + events = [event async for event in source] if async_flag else list(source) + assert [str(event) for event in events] == ["hello"] + + +def test_event_stream_rejects_other_media_types(): + response = httpx.Response(200, headers={"Content-Type": "text/plain"}) + with pytest.raises(ValueError, match="text/plain"): + EventSource(replicate.Client(), response)