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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
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.pyindex 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 itreturn+ 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+ raiseexcept aiohttp.http_exceptions.TransferEncodingError as e:# Handle transfer encoding errors gracefullyverbose_logger.debug(f"Transfer encoding error, but continuing: {e}")