litellm/tests/unit/llms/bedrock/slow_upstream.py
joshua-berri 729a29df39
fix(bedrock): resolve profile pricing before applying custom rates (#47419)
* fix(bedrock): price Converse calls using the invoked model profile

* fix(pricing): retain registered rates across metadata updates

* fix(pricing): honor temporary registered prices for Bedrock profiles

* fix(bedrock): record invocation model ID for each completion attempt

* test(bedrock): recalculate fallback cost after metadata updates

* fix(pricing): preserve cache-only registered price overrides

* fix(bedrock): preserve priced overrides without widening shared pricing

* fix(bedrock): retain explicit pricing through token cost lookup

* fix(bedrock): layer cache overrides over invoked profile rates

* fix(bedrock): keep effective prices consistent across billing

* fix(bedrock): share profile pricing and custom rate resolution

* fix(router): remove unused pricing suppression

* fix(cost): preserve rates across mixed token thresholds

* fix(router): share deployment pricing during startup

* fix(bedrock): preserve Router pricing inheritance for profiles

* fix(bedrock): retain configured pricing for unmapped profiles

---------

Co-authored-by: Joshua Valluru <326636767+joshua-berri@users.noreply.github.com>
(cherry picked from commit 3124797399)
2026-10-27 19:57:37 -07:00

44 lines
1.7 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
import json
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
CONVERSE_ANSWER: Final = json.dumps(
{
"output": {"message": {"role": "assistant", "content": [{"text": "hi"}]}},
"stopReason": "end_turn",
"usage": {"inputTokens": 1, "outputTokens": 1, "totalTokens": 2},
"metrics": {"latencyMs": 1},
}
).encode()
_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)))