Skip to content
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
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