-
Notifications
You must be signed in to change notification settings - Fork 3k
JSONL stream #39478
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
Closed
Closed
JSONL stream #39478
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
036c7d0
move wip jsonl stream impl to corehttp
kristapratico 1ae20e8
async fixes
kristapratico 03c0dc1
lint/black
kristapratico c289dbe
add more tests
kristapratico 4275fab
add docstrings
kristapratico 505c2ef
pylint/black
kristapratico 8ef1983
fix test: anext --> s.__anext__ for <py3.10
kristapratico fb3eb4d
callback should take pipeline_response
kristapratico 852b7ee
move protocols
kristapratico 744a0a5
fix docstring
kristapratico cfb365d
black
kristapratico 55cbb96
make decoders, events public; stream accepts HttpResponse instead of …
kristapratico 0e9f38d
johan feedback: use codecs.getincrementaldecoder()
kristapratico 43e2517
make decoder kwonly required
kristapratico b3fbc02
remove docstring repeat
kristapratico 889f1d8
create incrementaldecoder in iter_events; remove unused test
kristapratico 6d2e5bb
switch back to splitlines(); remove event() from decoders
kristapratico 7ba7b5c
johan's feedback
kristapratico 77d90a6
remove file
kristapratico e4daa81
some feedback
kristapratico 217d14b
fix typing
kristapratico 23746d9
make JSONLDecoder generic type default to JSON + remove terminal_even…
kristapratico 02ee683
decoders -> _decoders + add more tests
kristapratico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# -------------------------------------------------------------------------- | ||
# | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# | ||
# The MIT License (MIT) | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the ""Software""), to | ||
# deal in the Software without restriction, including without limitation the | ||
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
# sell copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
# IN THE SOFTWARE. | ||
# | ||
# -------------------------------------------------------------------------- | ||
|
||
from ._stream import Stream, AsyncStream | ||
from ._decoders import StreamDecoder, AsyncStreamDecoder, JSONLDecoder, AsyncJSONLDecoder | ||
|
||
|
||
__all__ = [ | ||
"Stream", | ||
"AsyncStream", | ||
"StreamDecoder", | ||
"AsyncStreamDecoder", | ||
"JSONLDecoder", | ||
"AsyncJSONLDecoder", | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
# -------------------------------------------------------------------------- | ||
# | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# | ||
# The MIT License (MIT) | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the ""Software""), to | ||
# deal in the Software without restriction, including without limitation the | ||
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
# sell copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
# IN THE SOFTWARE. | ||
# | ||
# -------------------------------------------------------------------------- | ||
|
||
import codecs | ||
import json | ||
from typing import Iterator, AsyncIterator, Protocol, Any, MutableMapping, Generic | ||
|
||
from typing_extensions import runtime_checkable, TypeVar | ||
|
||
T_co = TypeVar("T_co", covariant=True) | ||
T = TypeVar("T", default=MutableMapping[str, Any]) | ||
|
||
|
||
@runtime_checkable | ||
class StreamDecoder(Protocol[T_co]): | ||
"""Protocol for stream decoders.""" | ||
|
||
def iter_events(self, iter_bytes: Iterator[bytes]) -> Iterator[T_co]: | ||
"""Iterate over events from a byte iterator. | ||
|
||
:param iter_bytes: An iterator of byte chunks. | ||
:type iter_bytes: Iterator[bytes] | ||
:return: An iterator of decoded data. | ||
:rtype: Iterator[DecodedType_co] | ||
""" | ||
... | ||
|
||
|
||
@runtime_checkable | ||
class AsyncStreamDecoder(Protocol[T_co]): | ||
"""Protocol for async stream decoders.""" | ||
|
||
# Why this isn't async def: https://mypy.readthedocs.io/en/stable/more_types.html#asynchronous-iterators | ||
def aiter_events(self, iter_bytes: AsyncIterator[bytes]) -> AsyncIterator[T_co]: | ||
"""Asynchronously iterate over events from a byte iterator. | ||
|
||
:param iter_bytes: An asynchronous iterator of byte chunks. | ||
:type iter_bytes: AsyncIterator[bytes] | ||
:return: An asynchronous iterator of decoded data. | ||
:rtype: AsyncIterator[DecodedType_co] | ||
""" | ||
... | ||
|
||
|
||
def iter_lines(iter_bytes: Iterator[bytes]) -> Iterator[str]: | ||
"""Iterate over lines from a byte iterator. | ||
|
||
:param iter_bytes: An iterator of byte chunks. | ||
:type iter_bytes: Iterator[bytes] | ||
:rtype: Iterator[str] | ||
:return: An iterator of lines. | ||
""" | ||
decoder = codecs.getincrementaldecoder("utf-8")() | ||
|
||
decoded = "" | ||
for chunk in iter_bytes: | ||
decoded += decoder.decode(chunk) | ||
if decoded: | ||
decoded_lines = decoded.splitlines() | ||
if decoded.endswith(("\n", "\r\n")): | ||
yield from decoded_lines | ||
decoded = "" | ||
else: | ||
yield from decoded_lines[:-1] | ||
decoded = decoded_lines[-1] | ||
|
||
decoded += decoder.decode(b"", final=True) | ||
if decoded: | ||
yield from decoded.splitlines() | ||
|
||
|
||
async def aiter_lines(iter_bytes: AsyncIterator[bytes]) -> AsyncIterator[str]: | ||
"""Iterate over lines from a byte iterator. | ||
|
||
:param iter_bytes: An iterator of byte chunks. | ||
:type iter_bytes: Iterator[bytes] | ||
:rtype: Iterator[str] | ||
:return: An iterator of lines. | ||
""" | ||
decoder = codecs.getincrementaldecoder("utf-8")() | ||
|
||
decoded = "" | ||
async for chunk in iter_bytes: | ||
decoded += decoder.decode(chunk) | ||
if decoded: | ||
decoded_lines = decoded.splitlines() | ||
if decoded.endswith(("\n", "\r\n")): | ||
for line in decoded_lines: | ||
yield line | ||
decoded = "" | ||
else: | ||
for line in decoded_lines[:-1]: | ||
yield line | ||
decoded = decoded_lines[-1] | ||
|
||
decoded += decoder.decode(b"", final=True) | ||
if decoded: | ||
for line in decoded.splitlines(): | ||
yield line | ||
|
||
|
||
class JSONLDecoder(Generic[T]): | ||
"""Decoder for JSON Lines (JSONL) format. https://jsonlines.org/""" | ||
|
||
def iter_events(self, iter_bytes: Iterator[bytes]) -> Iterator[T]: | ||
"""Iterate over JSONL events from a byte iterator. | ||
|
||
:param iter_bytes: An iterator of byte chunks. | ||
:type iter_bytes: Iterator[bytes] | ||
:rtype: Iterator[T] | ||
:return: An iterator of objects. | ||
""" | ||
|
||
yield from (json.loads(line) for line in iter_lines(iter_bytes)) | ||
|
||
|
||
class AsyncJSONLDecoder(Generic[T]): | ||
"""Asynchronous decoder for JSON Lines (JSONL) format. https://jsonlines.org/""" | ||
|
||
# pylint: disable=invalid-overridden-method | ||
async def aiter_events(self, iter_bytes: AsyncIterator[bytes]) -> AsyncIterator[T]: | ||
"""Asynchronously iterate over JSONL events from a byte iterator. | ||
|
||
:param iter_bytes: An asynchronous iterator of byte chunks. | ||
:type iter_bytes: AsyncIterator[bytes] | ||
:rtype: AsyncIterator[T] | ||
:return: An asynchronous iterator of objects. | ||
""" | ||
|
||
async for line in aiter_lines(iter_bytes): | ||
yield json.loads(line) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# -------------------------------------------------------------------------- | ||
# | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# | ||
# The MIT License (MIT) | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the ""Software""), to | ||
# deal in the Software without restriction, including without limitation the | ||
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
# sell copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
# IN THE SOFTWARE. | ||
# | ||
# -------------------------------------------------------------------------- | ||
|
||
from types import TracebackType | ||
from typing import Iterator, AsyncIterator, TypeVar, Callable, Optional, Type | ||
|
||
from typing_extensions import Self | ||
|
||
from ..rest import HttpResponse, AsyncHttpResponse | ||
from ._decoders import StreamDecoder, AsyncStreamDecoder | ||
|
||
DecodedType = TypeVar("DecodedType") | ||
ReturnType_co = TypeVar("ReturnType_co", covariant=True) | ||
|
||
|
||
class Stream(Iterator[ReturnType_co]): | ||
"""Stream class for streaming JSONL. | ||
|
||
:keyword response: The response object. | ||
:paramtype response: ~corehttp.rest.HttpResponse | ||
:keyword decoder: A decoder to use for the stream. | ||
:paramtype decoder: ~corehttp.streaming.decoders.StreamDecoder | ||
:keyword deserialization_callback: A callback that takes JSON and returns a deserialized object. | ||
:paramtype deserialization_callback: Callable[[Any], ReturnType] | ||
""" | ||
|
||
def __init__( | ||
self, | ||
*, | ||
response: HttpResponse, | ||
decoder: StreamDecoder[DecodedType], | ||
deserialization_callback: Callable[[DecodedType], ReturnType_co], | ||
) -> None: | ||
self._response = response | ||
self._decoder = decoder | ||
self._deserialization_callback = deserialization_callback | ||
self._iterator = self._iter_results() | ||
|
||
def __next__(self) -> ReturnType_co: | ||
return self._iterator.__next__() | ||
|
||
def __iter__(self) -> Iterator[ReturnType_co]: | ||
yield from self._iterator | ||
|
||
def _iter_results(self) -> Iterator[ReturnType_co]: | ||
for event in self._decoder.iter_events(self._response.iter_bytes()): | ||
|
||
result = self._deserialization_callback(event) | ||
yield result | ||
|
||
def __exit__( | ||
self, | ||
exc_type: Optional[Type[BaseException]] = None, | ||
exc_value: Optional[BaseException] = None, | ||
traceback: Optional[TracebackType] = None, | ||
) -> None: | ||
self.close() | ||
|
||
def __enter__(self) -> Self: | ||
return self | ||
|
||
def close(self) -> None: | ||
self._response.close() | ||
|
||
|
||
class AsyncStream(AsyncIterator[ReturnType_co]): | ||
"""AsyncStream class for asynchronously streaming JSONL. | ||
|
||
:keyword response: The response object. | ||
:paramtype response: ~corehttp.rest.AsyncHttpResponse | ||
:keyword decoder: A decoder to use for the stream. | ||
:paramtype decoder: ~corehttp.streaming.decoders.AsyncStreamDecoder | ||
:keyword deserialization_callback: A callback that takes JSON and returns a deserialized object. | ||
:paramtype deserialization_callback: Callable[[Any], ReturnType] | ||
""" | ||
|
||
def __init__( | ||
self, | ||
*, | ||
response: AsyncHttpResponse, | ||
decoder: AsyncStreamDecoder[DecodedType], | ||
deserialization_callback: Callable[[DecodedType], ReturnType_co], | ||
) -> None: | ||
self._response = response | ||
self._decoder = decoder | ||
self._deserialization_callback = deserialization_callback | ||
self._iterator = self._iter_results() | ||
|
||
async def __anext__(self) -> ReturnType_co: | ||
return await self._iterator.__anext__() | ||
|
||
async def __aiter__(self) -> AsyncIterator[ReturnType_co]: # pylint: disable=invalid-overridden-method | ||
async for item in self._iterator: | ||
yield item | ||
|
||
async def _iter_results(self) -> AsyncIterator[ReturnType_co]: | ||
async for event in self._decoder.aiter_events(self._response.iter_bytes()): | ||
|
||
result = self._deserialization_callback(event) | ||
yield result | ||
|
||
async def __aexit__( | ||
self, | ||
exc_type: Optional[Type[BaseException]] = None, | ||
exc_value: Optional[BaseException] = None, | ||
traceback: Optional[TracebackType] = None, | ||
) -> None: | ||
await self.close() | ||
|
||
async def __aenter__(self) -> Self: | ||
return self | ||
|
||
async def close(self) -> None: | ||
await self._response.close() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.