litellm/tests/unit/llms/bedrock/slow_upstream.py
devin-ai-integration[bot] 1b98748528
fix(bedrock): honor the per-request timeout on Converse and Invoke streaming (internal copy of #38210) (#44134)
* fix(bedrock): propagate timeout to streaming requests

* test(bedrock): prove streaming fails at the request timeout against a slow upstream

* test(bedrock): simulate the slow upstream in process instead of over a local socket

* test(bedrock): audit the Converse and Invoke stream timeout on the proxy

Two integration files drive the per-request timeout on Bedrock streams
through the real proxy against an owned wire peer: the wire file covers
every surface (chat, messages, responses, invoke, pass-through), the
sad, edge and precedence rows, and the chaos file covers bursts, a
dropping upstream, a killed worker and a proxy stopped mid-burst.

The wire peer gains Reply.drop_connection so a cell can close the
socket before any response, and the harness's graceful stop grace is
now INTEGRATION_PROXY_STOP_SECONDS (default unchanged at 30), since a
two-worker supervisor's interpreter finalization takes longer than that
on a loaded box.

* test(bedrock): pin the fallback audit cell to one proxy worker

The fallback cell created both deployments through /model/new on one
worker and sent the chat request to the other, whose registry
read-through loads only the requested model, so the fallback target
was unknown there until the periodic DB poll. The cell now warms the
fallback model and sends the request over one keep-alive client, so
one TCP connection stays with one uvicorn worker, and it expects the
fallback upstream to see both requests.

---------

Co-authored-by: Sainyam Kapoor <hello@sainyam.me>
Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-10-03 19:29:15 +00:00

35 lines
1.4 KiB
Python

"""An upstream whose first byte takes longer than the request allows, the way a slow model does.
httpx hands every transport the request's timeout in ``request.extensions["timeout"]``, so this one honours it
in process the way a socket would: a read timeout shorter than the first byte's latency times out, a longer one
gets the answer.
"""
from __future__ import annotations
from typing import Final
import httpx
from pydantic import TypeAdapter
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
STREAM_TIMEOUT_SECONDS: Final = 0.5
UPSTREAM_FIRST_BYTE_SECONDS: Final = 2.0
_TIMEOUT_EXTENSION: Final = TypeAdapter(dict[str, float | None])
def _answer_once_the_first_byte_is_due(request: httpx.Request) -> httpx.Response:
read_timeout: Final = _TIMEOUT_EXTENSION.validate_python(request.extensions["timeout"])["read"]
if read_timeout is not None and read_timeout < UPSTREAM_FIRST_BYTE_SECONDS:
raise httpx.ReadTimeout(f"no byte arrived within {read_timeout}s", request=request)
return httpx.Response(200, content=b"", request=request)
def slow_upstream_async_client() -> AsyncHTTPHandler:
return AsyncHTTPHandler(transport=httpx.MockTransport(_answer_once_the_first_byte_is_due))
def slow_upstream_sync_client() -> HTTPHandler:
return HTTPHandler(client=httpx.Client(transport=httpx.MockTransport(_answer_once_the_first_byte_is_due)))