Skip to content

Commit b6dbd4f

Browse files
fix: apply provided timeout value to ClientTimeout.total (BerriAI#16395)
1 parent 5f12e4b commit b6dbd4f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

litellm/llms/custom_httpx/aiohttp_transport.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
AIOHTTP_EXC_MAP: Dict = {
1919
# Order matters here, most specific exception first
2020
# Timeout related exceptions
21+
asyncio.TimeoutError: httpx.TimeoutException,
2122
aiohttp.ServerTimeoutError: httpx.TimeoutException,
2223
aiohttp.ConnectionTimeoutError: httpx.ConnectTimeout,
2324
aiohttp.SocketTimeoutError: httpx.ReadTimeout,
@@ -253,6 +254,7 @@ async def _make_aiohttp_request(
253254
allow_redirects=False,
254255
auto_decompress=False,
255256
timeout=ClientTimeout(
257+
total=timeout.get("read"),
256258
sock_connect=timeout.get("connect"),
257259
sock_read=timeout.get("read"),
258260
connect=timeout.get("pool"),

tests/test_litellm/llms/custom_httpx/test_aiohttp_transport.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,48 @@ async def iter_chunked(self, size):
255255

256256
return MockResp()
257257

258+
@pytest.mark.asyncio
259+
async def test_handle_async_request_total_timeout_triggers():
260+
"""
261+
Ensure that LiteLLMAiohttpTransport raises httpx.TimeoutException
262+
when the total timeout duration elapses.
263+
"""
264+
import asyncio
265+
from aiohttp import web
266+
267+
async def slow_handler(request):
268+
await asyncio.sleep(0.3)
269+
return web.Response(text="ok")
270+
271+
app = web.Application()
272+
app.router.add_get("/", slow_handler)
273+
runner = web.AppRunner(app)
274+
await runner.setup()
275+
site = web.TCPSite(runner, "127.0.0.1", 0)
276+
await site.start()
277+
278+
port = site._server.sockets[0].getsockname()[1]
279+
280+
def factory():
281+
return aiohttp.ClientSession()
282+
283+
transport = LiteLLMAiohttpTransport(client=factory) # type: ignore
284+
285+
request = httpx.Request("GET", f"http://127.0.0.1:{port}/")
286+
287+
request.extensions["timeout"] = {
288+
"connect": 0.1,
289+
"read": 0.1,
290+
"pool": 0.1,
291+
"total": 0.1,
292+
}
293+
294+
try:
295+
with pytest.raises(httpx.TimeoutException):
296+
await transport.handle_async_request(request)
297+
finally:
298+
await transport.aclose()
299+
await runner.cleanup()
258300

259301
def _make_mock_session(closed=False):
260302
"""Helper to create a mock aiohttp session"""

0 commit comments

Comments
 (0)