Skip to content

Commit 99b2325

Browse files
fix sessions closing warning with AsyncInferenceClient (#3252)
1 parent 49c7979 commit 99b2325

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/huggingface_hub/inference/_common.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,12 @@ def _format_chat_completion_stream_output(
374374

375375

376376
async def _async_yield_from(client: "ClientSession", response: "ClientResponse") -> AsyncIterable[bytes]:
377-
async for byte_payload in response.content:
378-
yield byte_payload.strip()
379-
await client.close()
377+
try:
378+
async for byte_payload in response.content:
379+
yield byte_payload.strip()
380+
finally:
381+
# Always close the underlying HTTP session to avoid resource leaks
382+
await client.close()
380383

381384

382385
# "TGI servers" are servers running with the `text-generation-inference` backend.

0 commit comments

Comments
 (0)