Skip to content

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]

test(llm_http_handler): pin the websocket and callback gates the request path branches on (#37814)

yuneng-jiangf88421bb4346
diff --git a/tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py b/tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py
index c87abbd8bc4..3c972ae9c84 100644
--- a/tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py
+++ b/tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py
@@ -24,7 +24,10 @@ from litellm.llms.base_llm.chat.transformation import BaseLLMException
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
from litellm.llms.custom_httpx.llm_http_handler import (
BaseLLMHTTPHandler,
+ _collect_ws_project_quota_callbacks,
_google_genai_streaming_hidden_params,
+ _has_pre_call_deployment_hook,
+ _rust_responses_websocket_enabled,
)
from litellm.types.llms.openai import ResponsesAPIResponse
from litellm.types.router import GenericLiteLLMParams
@@ -2445,3 +2448,82 @@ async def test_generic_http_handler_async_streaming_forwards_provider_response_h
collected = [chunk async for chunk in response]
assert "".join([chunk.choices[0].delta.content or "" for chunk in collected]) == "hi"
+
+
+ "custom_llm_provider, litellm_params, expected",
+ [
+ ("openai", GenericLiteLLMParams(rust=True), True),
+ ("openai", GenericLiteLLMParams(), False),
+ ("openai", GenericLiteLLMParams(rust=False), False),
+ ("azure", GenericLiteLLMParams(rust=True), False),
+ ("hosted_vllm", GenericLiteLLMParams(rust=True), False),
+ (None, GenericLiteLLMParams(rust=True), False),
+ ],
+)
+def test_the_rust_responses_websocket_needs_both_openai_and_the_rust_flag(
+ custom_llm_provider, litellm_params, expected
+):
+ assert _rust_responses_websocket_enabled(custom_llm_provider, litellm_params) is expected
+
+
+def test_a_plain_callback_does_not_advertise_a_pre_call_deployment_hook(monkeypatch):
+ from litellm.integrations.custom_logger import CustomLogger
+
+ class _PlainLogger(CustomLogger):
+ pass
+
+ logging_obj = Mock()
+ logging_obj.dynamic_success_callbacks = []
+
+ monkeypatch.setattr(litellm, "callbacks", [])
+ assert _has_pre_call_deployment_hook(logging_obj) is False
+
+ monkeypatch.setattr(litellm, "callbacks", [_PlainLogger()])
+ assert _has_pre_call_deployment_hook(logging_obj) is False
+
+
+def test_a_callback_that_overrides_the_deployment_hook_is_detected(monkeypatch):
+ from litellm.integrations.custom_logger import CustomLogger
+
+ class _DeploymentHookLogger(CustomLogger):
+ async def async_pre_call_deployment_hook(self, kwargs, call_type):
+ return None
+
+ class _InheritsTheHook(_DeploymentHookLogger):
+ pass
+
+ logging_obj = Mock()
+ logging_obj.dynamic_success_callbacks = []
+
+ monkeypatch.setattr(litellm, "callbacks", [_DeploymentHookLogger()])
+ assert _has_pre_call_deployment_hook(logging_obj) is True
+
+ monkeypatch.setattr(litellm, "callbacks", [_InheritsTheHook()])
+ assert _has_pre_call_deployment_hook(logging_obj) is True
+
+ monkeypatch.setattr(litellm, "callbacks", [])
+ logging_obj.dynamic_success_callbacks = [_DeploymentHookLogger()]
+ assert _has_pre_call_deployment_hook(logging_obj) is True
+
+
+def test_only_callbacks_that_can_charge_a_frame_are_collected_for_ws_quota(monkeypatch):
+ from litellm.integrations.custom_logger import CustomLogger
+
+ class _PlainLogger(CustomLogger):
+ pass
+
+ class _QuotaLogger(CustomLogger):
+ async def enforce_project_io_token_quota_for_frame(self, *args, **kwargs):
+ return None
+
+ class _NotCallableAttribute:
+ enforce_project_io_token_quota_for_frame = "not a method"
+
+ plain, quota, decoy = _PlainLogger(), _QuotaLogger(), _NotCallableAttribute()
+
+ monkeypatch.setattr(litellm, "callbacks", [plain, decoy])
+ assert _collect_ws_project_quota_callbacks() == ()
+
+ monkeypatch.setattr(litellm, "callbacks", [plain, quota, decoy])
+ assert _collect_ws_project_quota_callbacks() == (quota,)