Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(aiohttp): Gracefully handle connection closed errors during streaming (#16294)

Alexsander Hamir263629b7816e
diff --git a/litellm/llms/custom_httpx/aiohttp_transport.py b/litellm/llms/custom_httpx/aiohttp_transport.py
index 50bbccd6a4b..769bc0fed1e 100644
--- a/litellm/llms/custom_httpx/aiohttp_transport.py
+++ b/litellm/llms/custom_httpx/aiohttp_transport.py
@@ -95,6 +95,15 @@ class AiohttpResponseStream(httpx.AsyncByteStream):
# If the error is due to incomplete transfer encoding, we can still
# return what we've received so far, similar to how httpx handles it
return
+ except RuntimeError as e:
+ # Some providers (e.g., SSE streams) may close the connection
+ # causing aiohttp StreamReader to raise a generic RuntimeError
+ # with message "Connection closed.". Treat this as a graceful
+ # end-of-stream so downstream consumers don't error.
+ if "Connection closed" in str(e):
+ verbose_logger.debug("Upstream closed streaming connection; ending iterator gracefully")
+ return
+ raise
except aiohttp.http_exceptions.TransferEncodingError as e:
# Handle transfer encoding errors gracefully
verbose_logger.debug(f"Transfer encoding error, but continuing: {e}")