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(timeout): time out against the local fake endpoint instead of api.openai.com
Yuneng Jiangc70e4857fae0
diff --git a/tests/_fake_openai_endpoint_server.py b/tests/_fake_openai_endpoint_server.pyindex ac83e74b66a..d3af31aeef5 100644--- a/tests/_fake_openai_endpoint_server.py+++ b/tests/_fake_openai_endpoint_server.py@@ -207,13 +207,16 @@ async def completions(request: Request) -> Response:async def embeddings(request: Request) -> Response:body = await _parse_body(request)+ model = _requested_model(body)+ if model == _SLOW_MODEL:+ await asyncio.sleep(_SLOW_RESPONSE_SECONDS)raw_input = body.get("input", "")count = len(raw_input) if isinstance(raw_input, list) else 1return JSONResponse({"object": "list","data": [{"object": "embedding", "index": i, "embedding": [0.0] * 1536} for i in range(max(count, 1))],- "model": _requested_model(body),+ "model": model,"usage": {"prompt_tokens": 5, "total_tokens": 5},})diff --git a/tests/local_testing/test_embedding.py b/tests/local_testing/test_embedding.pyindex ee2ac14f498..c119334da6f 100644--- a/tests/local_testing/test_embedding.py+++ b/tests/local_testing/test_embedding.py@@ -15,6 +15,7 @@ from unittest.mock import AsyncMock, MagicMock, patchimport litellmfrom litellm import completion, completion_cost, embedding+from tests.fake_openai_endpoint import FAKE_OPENAI_API_BASElitellm.set_verbose = False@@ -269,11 +270,14 @@ def test_openai_azure_embedding_timeouts():def test_openai_embedding_timeouts():try:response = embedding(- model="text-embedding-ada-002",+ model="openai/slow-endpoint",input=["good morning from litellm"],- timeout=0.00001,+ api_base=FAKE_OPENAI_API_BASE,+ api_key="fake-key",+ timeout=0.5,)print(response)+ pytest.fail("Expected timeout error, the request returned instead")except openai.APITimeoutError:print("Good job got OpenAI timeout error!")passdiff --git a/tests/local_testing/test_router.py b/tests/local_testing/test_router.pyindex c714bb4f9a7..bd0a9bf8df4 100644--- a/tests/local_testing/test_router.py+++ b/tests/local_testing/test_router.py@@ -1552,8 +1552,9 @@ def test_router_timeout():{"model_name": "gpt-3.5-turbo","litellm_params": {- "model": "gpt-3.5-turbo",- "api_key": "os.environ/OPENAI_API_KEY",+ "model": "openai/slow-endpoint",+ "api_base": FAKE_OPENAI_API_BASE,+ "api_key": "fake-key",},}]@@ -1562,7 +1563,7 @@ def test_router_timeout():start_time = time.time()try:res = router.completion(- model="gpt-3.5-turbo", messages=messages, timeout=0.0001+ model="gpt-3.5-turbo", messages=messages, timeout=0.5)print(res)pytest.fail("this should have timed out")diff --git a/tests/local_testing/test_timeout.py b/tests/local_testing/test_timeout.pyindex 66054a0930a..784e2c73cd7 100644--- a/tests/local_testing/test_timeout.py+++ b/tests/local_testing/test_timeout.py@@ -12,6 +12,7 @@ import openaiimport pytestimport litellm+from tests.fake_openai_endpoint import FAKE_OPENAI_API_BASE@pytest.mark.parametrize(@@ -216,13 +217,16 @@ def test_timeout_streaming():litellm.set_verbose = Falsetry:response = litellm.completion(- model="gpt-3.5-turbo",+ model="openai/slow-endpoint",messages=[{"role": "user", "content": "hello, write a 20 pg essay"}],- timeout=0.0001,+ api_base=FAKE_OPENAI_API_BASE,+ api_key="fake-key",+ timeout=0.5,stream=True,)for chunk in response:print(chunk)+ pytest.fail("Did not raise error `openai.APITimeoutError`. The stream completed instead")except openai.APITimeoutError as e:print("Passed: Raised correct exception. Got openai.APITimeoutError\nGood Job", e