Skip to content
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
16 changes: 16 additions & 0 deletions src/openai/_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def __stream__(self) -> Iterator[_T]:
try:
for sse in iterator:
if sse.data.startswith("[DONE]"):
# Drain remaining bytes so h11 fully parses the chunked terminator
# before close(), allowing the connection to be returned to the pool.
# Errors are suppressed — the stream is done at [DONE] regardless.
try:
for _ in iterator:
pass
except Exception:
pass
break

# we have to special case the Assistants `thread.` events since we won't have an "event" key in the data
Expand Down Expand Up @@ -171,6 +179,14 @@ async def __stream__(self) -> AsyncIterator[_T]:
try:
async for sse in iterator:
if sse.data.startswith("[DONE]"):
# Drain remaining bytes so h11 fully parses the chunked terminator
# before aclose(), allowing the connection to be returned to the pool.
# Errors are suppressed — the stream is done at [DONE] regardless.
try:
async for _ in iterator:
pass
except Exception:
pass
break

# we have to special case the Assistants `thread.` events since we won't have an "event" key in the data
Expand Down