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
fix(openai): use optimized async http client for text completions (#17831)
Cesar Garciaf7425f297ba5
diff --git a/litellm/llms/openai/completion/handler.py b/litellm/llms/openai/completion/handler.pyindex fa31c487cd2..1641615126e 100644--- a/litellm/llms/openai/completion/handler.py+++ b/litellm/llms/openai/completion/handler.py@@ -11,7 +11,7 @@ from litellm.types.llms.openai import AllMessageValues, OpenAITextCompletionUserfrom litellm.types.utils import LlmProviders, ModelResponse, TextCompletionResponsefrom litellm.utils import ProviderConfigManager-from ..common_utils import OpenAIError+from ..common_utils import BaseOpenAILLM, OpenAIErrorfrom .transformation import OpenAITextCompletionConfig@@ -168,7 +168,7 @@ class OpenAITextCompletion(BaseLLM):openai_aclient = AsyncOpenAI(api_key=api_key,base_url=api_base,- http_client=litellm.aclient_session,+ http_client=BaseOpenAILLM._get_async_http_client(),timeout=timeout,max_retries=max_retries,organization=organization,diff --git a/tests/llm_translation/test_text_completion_unit_tests.py b/tests/llm_translation/test_text_completion_unit_tests.pyindex fa416962ca9..628cc9b2c2b 100644--- a/tests/llm_translation/test_text_completion_unit_tests.py+++ b/tests/llm_translation/test_text_completion_unit_tests.py@@ -141,3 +141,77 @@ async def test_huggingface_text_completion_logprobs():assert response.usage["completion_tokens"] > 0assert response.usage["prompt_tokens"] > 0assert response.usage["total_tokens"] > 0+++async def test_acompletion_uses_optimized_http_client():+ """+ Test that OpenAITextCompletion.acompletion uses BaseOpenAILLM._get_async_http_client()+ instead of litellm.aclient_session directly.++ Related issue: https://github.com/BerriAI/litellm/issues/17676+ """+ from litellm.llms.openai.completion.handler import OpenAITextCompletion+ from litellm.llms.openai.common_utils import BaseOpenAILLM++ mock_http_client = MagicMock()+ mock_async_openai = AsyncMock()+ mock_async_openai.completions.with_raw_response.create = AsyncMock(+ return_value=MagicMock(+ parse=MagicMock(+ return_value=MagicMock(+ model_dump=MagicMock(+ return_value={+ "id": "test-id",+ "object": "text_completion",+ "created": 1234567890,+ "model": "gpt-3.5-turbo-instruct",+ "choices": [+ {+ "text": "test response",+ "index": 0,+ "finish_reason": "stop",+ }+ ],+ "usage": {+ "prompt_tokens": 5,+ "completion_tokens": 10,+ "total_tokens": 15,+ },+ }+ )+ )+ )+ )+ )++ with patch.object(+ BaseOpenAILLM, "_get_async_http_client", return_value=mock_http_client+ ) as mock_get_client:+ with patch(+ "litellm.llms.openai.completion.handler.AsyncOpenAI",+ return_value=mock_async_openai,+ ) as mock_openai_class:+ handler = OpenAITextCompletion()+ logging_obj = MagicMock()+ logging_obj.post_call = MagicMock()++ await handler.acompletion(+ logging_obj=logging_obj,+ api_base="https://api.openai.com/v1",+ data={"prompt": "test", "model": "gpt-3.5-turbo-instruct"},+ headers={},+ model_response=MagicMock(),+ api_key="test-key",+ model="gpt-3.5-turbo-instruct",+ timeout=30.0,+ max_retries=2,+ )++ # Verify _get_async_http_client was called+ mock_get_client.assert_called_once()++ # Verify AsyncOpenAI was initialized with the http_client from _get_async_http_client+ mock_openai_class.assert_called_once()+ call_kwargs = mock_openai_class.call_args.kwargs+ assert call_kwargs["http_client"] == mock_http_client