Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fixes #184: JSON parsing issue when async streaming #216

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

Merged
merged 1 commit into from
Feb 7, 2023
Merged
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
17 changes: 8 additions & 9 deletions openai/api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ def parse_stream_helper(line: bytes) -> Optional[str]:
# and it will close http connection with TCP Reset
return None
if line.startswith(b"data: "):
line = line[len(b"data: ") :]
return line.decode("utf-8")
line = line[len(b"data: "):]
return line.decode("utf-8")
else:
return None
return None


Expand All @@ -109,13 +111,10 @@ def parse_stream(rbody: Iterator[bytes]) -> Iterator[str]:


async def parse_stream_async(rbody: aiohttp.StreamReader):
async for chunk, _ in rbody.iter_chunks():
# While the `ChunkTupleAsyncStreamIterator` iterator is meant to iterate over chunks (and thus lines) it seems
# to still sometimes return multiple lines at a time, so let's split the chunk by lines again.
for line in chunk.splitlines():
_line = parse_stream_helper(line)
if _line is not None:
yield _line
async for line in rbody:
_line = parse_stream_helper(line)
if _line is not None:
yield _line


class APIRequestor:
Expand Down