Skip to content

fix(streaming): invert logic for assistant stream parsing #2373

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

Draft
wants to merge 2 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ async def main():
stream=True,
)

for event in stream:
async for event in stream:
print(event)


Expand Down
22 changes: 12 additions & 10 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 sse.event == "error":
data = sse.json()
if is_mapping(data) and data.get("error"):
message = None
Expand All @@ -75,12 +75,13 @@ def __stream__(self) -> Iterator[_T]:
body=data["error"],
)

yield process_data(data=data, cast_to=cast_to, response=response)

if sse.event and sse.event.startswith('thread.'):
# the assistants API uses a different event shape structure
yield process_data(data={"data": sse.json(), "event": sse.event}, cast_to=cast_to, response=response)
else:
data = sse.json()

if sse.event == "error" and is_mapping(data) and data.get("error"):
if is_mapping(data) and data.get("error"):
message = None
error = data.get("error")
if is_mapping(error):
Expand All @@ -94,7 +95,7 @@ def __stream__(self) -> Iterator[_T]:
body=data["error"],
)

yield process_data(data={"data": data, "event": sse.event}, cast_to=cast_to, response=response)
yield process_data(data=data, cast_to=cast_to, response=response)

# Ensure the entire stream is consumed
for _sse in iterator:
Expand Down Expand Up @@ -161,7 +162,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 sse.event == "error":
data = sse.json()
if is_mapping(data) and data.get("error"):
message = None
Expand All @@ -177,12 +178,13 @@ async def __stream__(self) -> AsyncIterator[_T]:
body=data["error"],
)

yield process_data(data=data, cast_to=cast_to, response=response)

if sse.event and sse.event.startswith('thread.'):
# the assistants API uses a different event shape structure
yield process_data(data={"data": sse.json(), "event": sse.event}, cast_to=cast_to, response=response)
else:
data = sse.json()

if sse.event == "error" and is_mapping(data) and data.get("error"):
if is_mapping(data) and data.get("error"):
message = None
error = data.get("error")
if is_mapping(error):
Expand All @@ -196,7 +198,7 @@ async def __stream__(self) -> AsyncIterator[_T]:
body=data["error"],
)

yield process_data(data={"data": data, "event": sse.event}, cast_to=cast_to, response=response)
yield process_data(data=data, cast_to=cast_to, response=response)

# Ensure the entire stream is consumed
async for _sse in iterator:
Expand Down
Loading