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
test: tighten regression tests added in #37974
mateo-berri8dda73c935f5
diff --git a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_openai_cache_write_cost.py b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_openai_cache_write_cost.pyindex 92cb417f96c..18acfeda07d 100644--- a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_openai_cache_write_cost.py+++ b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_openai_cache_write_cost.py@@ -39,7 +39,7 @@ def test_openai_cache_write_tokens_billed_at_the_cache_creation_rate(local_modelinput_rate = rates["input_cost_per_token"]cache_write_rate = rates["cache_creation_input_token_cost"]output_rate = rates["output_cost_per_token"]- assert cache_write_rate == pytest.approx(input_rate * 1.25)+ assert cache_write_rate > input_rateprompt_tokens = 12317cache_write_tokens = 12314diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.pyindex 9e58ded81bd..1e6dacbb7cb 100644--- a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.py+++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.py@@ -1187,7 +1187,7 @@ async def _flush_logging_worker(capture: "_SuccessPayloadCapture") -> None:await asyncio.sleep(0)try:await asyncio.wait_for(GLOBAL_LOGGING_WORKER.flush(), timeout=10.0)- except (asyncio.TimeoutError, RuntimeError):+ except asyncio.TimeoutError:passdeadline = asyncio.get_running_loop().time() + 10.0while not capture.payloads and asyncio.get_running_loop().time() < deadline:diff --git a/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.py b/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.pyindex 6f9142c85df..8f7146d8647 100644--- a/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.py+++ b/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.py@@ -1830,8 +1830,10 @@ class TestOpenAIPassthroughResponsesStreamingSpendLog:def setup_method(self):self.start_time = datetime.now()self.end_time = datetime.now()++ def _expected_spend(self) -> float:rates = litellm.model_cost[self.MODEL_MAP_KEY]- self.expected_spend = (+ return (self.INPUT_TOKENS * rates["input_cost_per_token"]+ self.OUTPUT_TOKENS * rates["output_cost_per_token"])@@ -1912,7 +1914,7 @@ class TestOpenAIPassthroughResponsesStreamingSpendLog:logging_obj.model_call_details["custom_llm_provider"] = "openai"return logging_obj- def test_streamed_responses_passthrough_spend_log_is_priced(self):+ def test_streamed_responses_passthrough_spend_log_is_priced(self, local_model_cost_map):"""The spend row books the same tokens, spend and `resp_` id as the buffered call."""result = OpenAIPassthroughLoggingHandler._handle_logging_openai_collected_chunks(litellm_logging_obj=self._logging_obj(),@@ -1940,7 +1942,7 @@ class TestOpenAIPassthroughResponsesStreamingSpendLog:assert spend_log_row["prompt_tokens"] == self.INPUT_TOKENSassert spend_log_row["completion_tokens"] == self.OUTPUT_TOKENSassert spend_log_row["total_tokens"] == self.INPUT_TOKENS + self.OUTPUT_TOKENS- assert spend_log_row["spend"] == self.expected_spend+ assert spend_log_row["spend"] == pytest.approx(self._expected_spend())assert spend_log_row["request_id"] == self.RESPONSE_IDassert spend_log_row["model"] == "gpt-4o-mini"@@ -1965,7 +1967,6 @@ class TestOpenAIPassthroughEmbeddingsSpendLog:def setup_method(self):self.start_time = datetime.now()self.end_time = datetime.now()- self.expected_spend = self.PROMPT_TOKENS * litellm.model_cost[self.MODEL]["input_cost_per_token"]self.response_body = {"object": "list","data": [{"object": "embedding", "index": 0, "embedding": [0.0, 1.0]}],@@ -1974,6 +1975,9 @@ class TestOpenAIPassthroughEmbeddingsSpendLog:}self.request_body = {"model": self.MODEL, "input": "hello"}+ def _expected_spend(self) -> float:+ return self.PROMPT_TOKENS * litellm.model_cost[self.MODEL]["input_cost_per_token"]+def _create_mock_httpx_response(self) -> httpx.Response:mock_response = MagicMock(spec=httpx.Response)mock_response.status_code = 200@@ -1999,7 +2003,7 @@ class TestOpenAIPassthroughEmbeddingsSpendLog:)return logging_obj- def test_embeddings_passthrough_spend_log_is_priced(self):+ def test_embeddings_passthrough_spend_log_is_priced(self, local_model_cost_map):"""The dispatched call books prompt tokens and cost onto the spend row."""dispatched = PassThroughEndpointLogging().normalize_llm_passthrough_logging_payload(httpx_response=self._create_mock_httpx_response(),@@ -2018,7 +2022,7 @@ class TestOpenAIPassthroughEmbeddingsSpendLog:)assert dispatched["standard_logging_response_object"] is not None- assert dispatched["kwargs"]["response_cost"] == self.expected_spend+ assert dispatched["kwargs"]["response_cost"] == pytest.approx(self._expected_spend())spend_log_row = get_logging_payload(kwargs=dispatched["kwargs"],@@ -2029,7 +2033,7 @@ class TestOpenAIPassthroughEmbeddingsSpendLog:assert spend_log_row["prompt_tokens"] == self.PROMPT_TOKENSassert spend_log_row["total_tokens"] == self.PROMPT_TOKENS- assert spend_log_row["spend"] == self.expected_spend+ assert spend_log_row["spend"] == pytest.approx(self._expected_spend())assert spend_log_row["model"] == self.MODELassert spend_log_row["custom_llm_provider"] == "openai"assert spend_log_row["request_id"] == self.CALL_IDdiff --git a/tests/test_litellm/test_main.py b/tests/test_litellm/test_main.pyindex 99b1cc826aa..2599b81cd2c 100644--- a/tests/test_litellm/test_main.py+++ b/tests/test_litellm/test_main.py@@ -850,15 +850,25 @@ def test_responses_api_bridge_check_gpt_5_4_tools_with_default_reasoning_routes_assert model_info.get("mode") == "responses"[email protected]("model_name", ["gpt-5.6-sol", "gpt-5.6-luna", "gpt-5.6-terra"])+ "model_name, expected_mode",+ [+ pytest.param("gpt-5.6-sol", "responses", id="above-boundary-bridges"),+ pytest.param("gpt-5.1", None, id="below-boundary-stays-chat"),+ ],+)def test_responses_api_bridge_check_gpt_5_6_tools_with_default_reasoning_routes_to_responses(- monkeypatch, model_name+ monkeypatch, model_name, expected_mode):"""- The whole gpt-5.6 family must bridge on function tools alone. The bridge used to- require an explicit reasoning_effort, so a gpt-5.6 call carrying tools and no effort- was rejected with "Function tools with reasoning_effort are not supported for- gpt-5.6-sol in /v1/chat/completions".+ gpt-5.6 must bridge on function tools alone. The bridge used to require an explicit+ reasoning_effort, so a gpt-5.6 call carrying tools and no effort was rejected with+ "Function tools with reasoning_effort are not supported for gpt-5.6-sol in+ /v1/chat/completions".++ Paired with a model below the gpt-5.4 boundary, which must still stay on chat. The+ gate parses the version and drops any suffix, so the family members bridge+ identically and only the boundary distinguishes behaviour."""import litellmfrom litellm.main import responses_api_bridge_check@@ -877,7 +887,7 @@ def test_responses_api_bridge_check_gpt_5_6_tools_with_default_reasoning_routes_)assert model == model_name- assert model_info.get("mode") == "responses"+ assert model_info.get("mode") == expected_modedef test_responses_api_bridge_check_gpt_5_4_tools_with_reasoning_none_stays_chat():@@ -2865,15 +2875,19 @@ def local_cost_map(monkeypatch):"""The prices these tests assert are the checked-in ones. Setting the environmentvariable alone does not reload the map, so pin the map itself.- ``get_model_info`` is lru_cached, so pinning ``model_cost`` is not enough on its- own: a cached entry warmed against the network-fetched map keeps its old prices- and ``completion_cost`` bills at those while the assertions read the pinned map.- Clear on the way in and out so entries never leak across tests in either direction."""+ Prices are read through two separate lru_caches, so pinning ``model_cost`` is not+ enough on its own: an entry warmed against the network-fetched map keeps its old+ prices and billing reads those while the assertions read the pinned map.+ ``_invalidate_model_cost_lowercase_map`` clears both caches, where+ ``get_model_info.cache_clear`` reaches only one. Invalidate on the way in and out+ so entries never leak across tests in either direction."""+ from litellm.utils import _invalidate_model_cost_lowercase_map+monkeypatch.setenv("LITELLM_LOCAL_MODEL_COST_MAP", "True")monkeypatch.setattr(litellm, "model_cost", litellm.get_model_cost_map(url=""))- litellm.get_model_info.cache_clear()+ _invalidate_model_cost_lowercase_map()yield- litellm.get_model_info.cache_clear()+ _invalidate_model_cost_lowercase_map()def test_a_streamed_response_bills_the_usage_the_provider_reported(local_cost_map):