Skip to content

Commit c8d01be

Browse files
committed
fix sessions closing warning with AsyncInferenceClient (#3252)
1 parent e2ff3a7 commit c8d01be

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
@@ -347,9 +347,12 @@ def _format_chat_completion_stream_output(
347347

348348

349349
async def _async_yield_from(client: "ClientSession", response: "ClientResponse") -> AsyncIterable[bytes]:
350-
async for byte_payload in response.content:
351-
yield byte_payload.strip()
352-
await client.close()
350+
try:
351+
async for byte_payload in response.content:
352+
yield byte_payload.strip()
353+
finally:
354+
# Always close the underlying HTTP session to avoid resource leaks
355+
await client.close()
353356

354357

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

0 commit comments

Comments
 (0)