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
pass llm provider when creating async httpx clients
Ishaan Jaff421b8577145f
diff --git a/litellm/caching.py b/litellm/caching.pyindex 2c0a5a2da8e..5add0cd8e92 100644--- a/litellm/caching.py+++ b/litellm/caching.py@@ -1244,6 +1244,7 @@ class QdrantSemanticCache(BaseCache):from litellm.llms.custom_httpx.http_handler import (_get_httpx_client,get_async_httpx_client,+ httpxSpecialProvider,)if collection_name is None:@@ -1290,7 +1291,9 @@ class QdrantSemanticCache(BaseCache):self.headers = headersself.sync_client = _get_httpx_client()- self.async_client = get_async_httpx_client()+ self.async_client = get_async_httpx_client(+ llm_provider=httpxSpecialProvider.Caching+ )if quantization_config is None:print_verbose(diff --git a/litellm/llms/anthropic/chat.py b/litellm/llms/anthropic/chat.pyindex a5cee1715f6..0520506bb4a 100644--- a/litellm/llms/anthropic/chat.py+++ b/litellm/llms/anthropic/chat.py@@ -918,7 +918,9 @@ class AnthropicChatCompletion(BaseLLM):headers={},client=None,) -> Union[ModelResponse, CustomStreamWrapper]:- async_handler = get_async_httpx_client()+ async_handler = get_async_httpx_client(+ llm_provider=litellm.LlmProviders.ANTHROPIC+ )try:response = await async_handler.post(diff --git a/litellm/llms/bedrock/chat.py b/litellm/llms/bedrock/chat.pyindex 80a546bd5db..96c47c20c62 100644--- a/litellm/llms/bedrock/chat.py+++ b/litellm/llms/bedrock/chat.py@@ -209,7 +209,9 @@ async def make_call():try:if client is None:- client = get_async_httpx_client() # Create a new client if none provided+ client = get_async_httpx_client(+ llm_provider=litellm.LlmProviders.BEDROCK+ ) # Create a new client if none providedresponse = await client.post(api_base,@@ -1041,7 +1043,7 @@ class BedrockLLM(BaseAWSLLM):if isinstance(timeout, float) or isinstance(timeout, int):timeout = httpx.Timeout(timeout)_params["timeout"] = timeout- client = get_async_httpx_client(_params) # type: ignore+ client = get_async_httpx_client(_params, llm_provider=litellm.LlmProviders.BEDROCK) # type: ignoreelse:client = client # type: ignore@@ -1498,7 +1500,7 @@ class BedrockConverseLLM(BaseAWSLLM):if isinstance(timeout, float) or isinstance(timeout, int):timeout = httpx.Timeout(timeout)_params["timeout"] = timeout- client = get_async_httpx_client(_params) # type: ignore+ client = get_async_httpx_client(_params, llm_provider=litellm.LlmProviders.BEDROCK) # type: ignoreelse:client = client # type: ignorediff --git a/litellm/llms/bedrock/embed/embedding.py b/litellm/llms/bedrock/embed/embedding.pyindex ddc8f216276..b0fbaa5e0ca 100644--- a/litellm/llms/bedrock/embed/embedding.py+++ b/litellm/llms/bedrock/embed/embedding.py@@ -130,7 +130,7 @@ class BedrockEmbedding(BaseAWSLLM):if isinstance(timeout, float) or isinstance(timeout, int):timeout = httpx.Timeout(timeout)_params["timeout"] = timeout- client = get_async_httpx_client(_params) # type: ignore+ client = get_async_httpx_client(_params, llm_provider=litellm.LlmProviders.BEDROCK) # type: ignoreelse:client = clientdiff --git a/litellm/llms/cohere/rerank.py b/litellm/llms/cohere/rerank.pyindex 137624ade23..64afcae4f37 100644--- a/litellm/llms/cohere/rerank.py+++ b/litellm/llms/cohere/rerank.py@@ -9,6 +9,7 @@ from typing import Any, Dict, List, Optional, Unionimport httpxfrom pydantic import BaseModel+import litellmfrom litellm.llms.base import BaseLLMfrom litellm.llms.custom_httpx.http_handler import (_get_httpx_client,@@ -65,7 +66,7 @@ class CohereRerank(BaseLLM):api_key: str,api_base: str,) -> RerankResponse:- client = get_async_httpx_client()+ client = get_async_httpx_client(llm_provider=litellm.LlmProviders.COHERE)response = await client.post(api_base,diff --git a/litellm/llms/custom_httpx/http_handler.py b/litellm/llms/custom_httpx/http_handler.pyindex babab894021..ed5773be83c 100644--- a/litellm/llms/custom_httpx/http_handler.py+++ b/litellm/llms/custom_httpx/http_handler.py@@ -7,6 +7,8 @@ import httpximport litellm+from .types import httpxSpecialProvider+try:from litellm._version import versionexcept:@@ -378,7 +380,10 @@ class HTTPHandler:pass-def get_async_httpx_client(params: Optional[dict] = None) -> AsyncHTTPHandler:+def get_async_httpx_client(+ llm_provider: Union[litellm.LlmProviders, httpxSpecialProvider],+ params: Optional[dict] = None,+) -> AsyncHTTPHandler:"""Retrieves the async HTTP client from the cacheIf not present, creates a new client@@ -393,7 +398,7 @@ def get_async_httpx_client(params: Optional[dict] = None) -> AsyncHTTPHandler:except Exception:pass- _cache_key_name = "async_httpx_client" + _params_key_name+ _cache_key_name = "async_httpx_client" + _params_key_name + llm_providerif _cache_key_name in litellm.in_memory_llm_clients_cache:return litellm.in_memory_llm_clients_cache[_cache_key_name]diff --git a/litellm/llms/custom_httpx/types.py b/litellm/llms/custom_httpx/types.pynew file mode 100644index 00000000000..dc09581185c--- /dev/null+++ b/litellm/llms/custom_httpx/types.py@@ -0,0 +1,10 @@+from enum import Enum++import litellm+++class httpxSpecialProvider(str, Enum):+ LoggingCallback = "logging_callback"+ GuardrailCallback = "guardrail_callback"+ Caching = "caching"+ Oauth2Check = "oauth2_check"diff --git a/litellm/llms/sagemaker/sagemaker.py b/litellm/llms/sagemaker/sagemaker.pyindex 733826aeeac..b124308eb49 100644--- a/litellm/llms/sagemaker/sagemaker.py+++ b/litellm/llms/sagemaker/sagemaker.py@@ -565,8 +565,8 @@ class SagemakerLLM(BaseAWSLLM):):try:if client is None:- client = (- get_async_httpx_client()+ client = get_async_httpx_client(+ llm_provider=litellm.LlmProviders.SAGEMAKER) # Create a new client if none providedresponse = await client.post(api_base,@@ -673,7 +673,9 @@ class SagemakerLLM(BaseAWSLLM):model_id: Optional[str],):timeout = 300.0- async_handler = get_async_httpx_client()+ async_handler = get_async_httpx_client(+ llm_provider=litellm.LlmProviders.SAGEMAKER+ )async_transform_prompt = asyncify(self._transform_prompt)diff --git a/litellm/llms/togetherai/rerank.py b/litellm/llms/togetherai/rerank.pyindex 17e4ce7e598..ea57c46c744 100644--- a/litellm/llms/togetherai/rerank.py+++ b/litellm/llms/togetherai/rerank.py@@ -9,6 +9,7 @@ from typing import Any, Dict, List, Optional, Unionimport httpxfrom pydantic import BaseModel+import litellmfrom litellm.llms.base import BaseLLMfrom litellm.llms.custom_httpx.http_handler import (_get_httpx_client,@@ -77,7 +78,9 @@ class TogetherAIRerank(BaseLLM):request_data_dict: Dict[str, Any],api_key: str,) -> RerankResponse:- client = get_async_httpx_client() # Use async client+ client = get_async_httpx_client(+ llm_provider=litellm.LlmProviders.TOGETHER_AI+ ) # Use async clientresponse = await client.post("https://api.together.xyz/v1/rerank",diff --git a/litellm/llms/vertex_ai_and_google_ai_studio/gemini/vertex_and_google_ai_studio_gemini.py b/litellm/llms/vertex_ai_and_google_ai_studio/gemini/vertex_and_google_ai_studio_gemini.pyindex d2456fd1f42..7d3c09271e8 100644--- a/litellm/llms/vertex_ai_and_google_ai_studio/gemini/vertex_and_google_ai_studio_gemini.py+++ b/litellm/llms/vertex_ai_and_google_ai_studio/gemini/vertex_and_google_ai_studio_gemini.py@@ -1293,7 +1293,9 @@ class VertexLLM(BaseLLM):_async_client_params = {}if timeout:_async_client_params["timeout"] = timeout- client = get_async_httpx_client(params=_async_client_params)+ client = get_async_httpx_client(+ params=_async_client_params, llm_provider=litellm.LlmProviders.VERTEX_AI+ )## LOGGINGlogging_obj.pre_call(input=messages,diff --git a/litellm/llms/vertex_ai_and_google_ai_studio/text_to_speech/text_to_speech_handler.py b/litellm/llms/vertex_ai_and_google_ai_studio/text_to_speech/text_to_speech_handler.pyindex dbb38a71588..f3837d94b56 100644--- a/litellm/llms/vertex_ai_and_google_ai_studio/text_to_speech/text_to_speech_handler.py+++ b/litellm/llms/vertex_ai_and_google_ai_studio/text_to_speech/text_to_speech_handler.py@@ -4,6 +4,7 @@ from typing import Any, Coroutine, Literal, Optional, TypedDict, Unionimport httpx+import litellmfrom litellm._logging import verbose_loggerfrom litellm.llms.base import BaseLLMfrom litellm.llms.custom_httpx.http_handler import (@@ -178,7 +179,9 @@ class VertexTextToSpeechAPI(VertexLLM):) -> HttpxBinaryResponseContent:import base64- async_handler = get_async_httpx_client()+ async_handler = get_async_httpx_client(+ llm_provider=litellm.LlmProviders.VERTEX_AI+ )response = await async_handler.post(url=url,diff --git a/litellm/proxy/auth/oauth2_check.py b/litellm/proxy/auth/oauth2_check.pyindex f2e175632d4..85a112ef1fa 100644--- a/litellm/proxy/auth/oauth2_check.py+++ b/litellm/proxy/auth/oauth2_check.py@@ -20,7 +20,10 @@ async def check_oauth2_token(token: str) -> UserAPIKeyAuth:import httpxfrom litellm._logging import verbose_proxy_logger- from litellm.llms.custom_httpx.http_handler import get_async_httpx_client+ from litellm.llms.custom_httpx.http_handler import (+ get_async_httpx_client,+ httpxSpecialProvider,+ )from litellm.proxy._types import CommonProxyErrorsfrom litellm.proxy.proxy_server import premium_user@@ -40,7 +43,7 @@ async def check_oauth2_token(token: str) -> UserAPIKeyAuth:if not token_info_endpoint:raise ValueError("OAUTH_TOKEN_INFO_ENDPOINT environment variable is not set")- client = get_async_httpx_client()+ client = get_async_httpx_client(llm_provider=httpxSpecialProvider.Oauth2Check)headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}try:diff --git a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.pyindex f4e1a345eb8..4c47aeb8620 100644--- a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py+++ b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py@@ -34,6 +34,7 @@ from litellm.llms.base_aws_llm import BaseAWSLLMfrom litellm.llms.custom_httpx.http_handler import (AsyncHTTPHandler,get_async_httpx_client,+ httpxSpecialProvider,)from litellm.proxy._types import UserAPIKeyAuthfrom litellm.proxy.guardrails.guardrail_helpers import should_proceed_based_on_metadata@@ -55,7 +56,9 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM):guardrailVersion: Optional[str] = None,**kwargs,):- self.async_handler = get_async_httpx_client()+ self.async_handler = get_async_httpx_client(+ llm_provider=httpxSpecialProvider.GuardrailCallback+ )self.guardrailIdentifier = guardrailIdentifierself.guardrailVersion = guardrailVersiondiff --git a/litellm/proxy/guardrails/guardrail_hooks/presidio.py b/litellm/proxy/guardrails/guardrail_hooks/presidio.pyindex db22c1872da..eb09e5203ae 100644--- a/litellm/proxy/guardrails/guardrail_hooks/presidio.py+++ b/litellm/proxy/guardrails/guardrail_hooks/presidio.py@@ -22,7 +22,6 @@ import litellm # noqa: E401from litellm._logging import verbose_proxy_loggerfrom litellm.caching import DualCachefrom litellm.integrations.custom_guardrail import CustomGuardrail-from litellm.llms.custom_httpx.http_handler import get_async_httpx_clientfrom litellm.proxy._types import UserAPIKeyAuthfrom litellm.utils import (EmbeddingResponse,