Skip to content

Fix incorrectly parsing event fieldnames in yielding responses #2363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/openai/_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __stream__(self) -> Iterator[_T]:
if sse.data.startswith("[DONE]"):
break

if sse.event is None or sse.event.startswith("response.") or sse.event.startswith('transcript.'):
if is_valid_event(sse.event):
data = sse.json()
if is_mapping(data) and data.get("error"):
message = None
Expand Down Expand Up @@ -161,7 +161,7 @@ async def __stream__(self) -> AsyncIterator[_T]:
if sse.data.startswith("[DONE]"):
break

if sse.event is None or sse.event.startswith("response.") or sse.event.startswith('transcript.'):
if is_valid_event(sse.event):
data = sse.json()
if is_mapping(data) and data.get("error"):
message = None
Expand Down Expand Up @@ -385,6 +385,15 @@ def is_stream_class_type(typ: type) -> TypeGuard[type[Stream[object]] | type[Asy
return inspect.isclass(origin) and issubclass(origin, (Stream, AsyncStream))


def is_valid_event(event: str | None) -> bool:
"""Given an event fieldname, checks if it is a response, transcript, or None"""
if event is None:
return True
if event in ("response", "transcript") or event.startswith("response.") or event.startswith("transcript."):
return True
return False


def extract_stream_chunk_type(
stream_cls: type,
*,
Expand Down