Skip to content

Commit

Permalink
feat(jsonl): add .close() method (#862)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored Feb 7, 2025
1 parent 021e82b commit 75fd4c0
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/anthropic/_decoders/jsonl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,29 @@ class JSONLDecoder(Generic[_T]):
into a given type.
"""

http_response: httpx.Response | None
http_response: httpx.Response
"""The HTTP response this decoder was constructed from"""

def __init__(
self, *, raw_iterator: Iterator[bytes], line_type: type[_T], http_response: httpx.Response | None
self,
*,
raw_iterator: Iterator[bytes],
line_type: type[_T],
http_response: httpx.Response,
) -> None:
super().__init__()
self.http_response = http_response
self._raw_iterator = raw_iterator
self._line_type = line_type
self._iterator = self.__decode__()

def close(self) -> None:
"""Close the response body stream.
This is called automatically if you consume the entire stream.
"""
self.http_response.close()

def __decode__(self) -> Iterator[_T]:
buf = b""
for chunk in self._raw_iterator:
Expand Down Expand Up @@ -63,17 +74,28 @@ class AsyncJSONLDecoder(Generic[_T]):
into a given type.
"""

http_response: httpx.Response | None
http_response: httpx.Response

def __init__(
self, *, raw_iterator: AsyncIterator[bytes], line_type: type[_T], http_response: httpx.Response | None
self,
*,
raw_iterator: AsyncIterator[bytes],
line_type: type[_T],
http_response: httpx.Response,
) -> None:
super().__init__()
self.http_response = http_response
self._raw_iterator = raw_iterator
self._line_type = line_type
self._iterator = self.__decode__()

async def close(self) -> None:
"""Close the response body stream.
This is called automatically if you consume the entire stream.
"""
await self.http_response.aclose()

async def __decode__(self) -> AsyncIterator[_T]:
buf = b""
async for chunk in self._raw_iterator:
Expand Down

0 comments on commit 75fd4c0

Please sign in to comment.