diff --git a/src/anthropic/_decoders/jsonl.py b/src/anthropic/_decoders/jsonl.py index e9d29a1c..ac5ac74f 100644 --- a/src/anthropic/_decoders/jsonl.py +++ b/src/anthropic/_decoders/jsonl.py @@ -17,11 +17,15 @@ 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 @@ -29,6 +33,13 @@ def __init__( 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: @@ -63,10 +74,14 @@ 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 @@ -74,6 +89,13 @@ def __init__( 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: