Skip to content

Commit 5a87d43

Browse files
committed
fix: address review feedback from karpetrosyan
- Replace decoder.close() with response.close() in sync stream - Replace decoder.close() with response.aclose() in async stream - Standardize comments between sync and async implementations Addresses feedback from PR anthropics#993: - SSEDecoder doesn't have a close method, properly close httpx response instead - Ensure consistent comments between sync/async versions
1 parent cf51769 commit 5a87d43

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/anthropic/_streaming.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,9 @@ def __stream__(self) -> Iterator[_T]:
120120
response=self.response,
121121
)
122122

123-
# Explicitly closes decoder resources if available
123+
# Explicitly close the response to release the connection
124124
# Immediately releases connection instead of consuming remaining stream
125-
if hasattr(self._decoder, "close"):
126-
self._decoder.close() # Properly closes decoder resources without unnecessary iteration
125+
self.response.close()
127126

128127
def __enter__(self) -> Self:
129128
return self
@@ -206,7 +205,7 @@ async def __stream__(self) -> AsyncIterator[_T]:
206205
async for sse in iterator:
207206
if sse.event == "completion":
208207
yield process_data(data=sse.json(), cast_to=cast_to, response=response)
209-
# same O(1) lookup for consistency between sync/async versions
208+
# Single, fast membership test instead of multiple string comparisons
210209
if sse.event in MESSAGE_EVENTS:
211210
data = sse.json()
212211
if is_dict(data) and "type" not in data:
@@ -232,9 +231,9 @@ async def __stream__(self) -> AsyncIterator[_T]:
232231
response=self.response,
233232
)
234233

234+
# Explicitly close the response to release the connection
235235
# Immediately releases connection instead of consuming remaining stream
236-
if hasattr(self._decoder, "close"):
237-
self._decoder.close() # Properly closes decoder resources without unnecessary iteration
236+
await self.response.aclose()
238237

239238
async def __aenter__(self) -> Self:
240239
return self

0 commit comments

Comments
 (0)