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]

fix(anthropic.py): respect timeouts

Krrish Dholakiab6b86b2df465
diff --git a/litellm/llms/anthropic.py b/litellm/llms/anthropic.py
index d3a3c38a483..2221a3d73f0 100644
--- a/litellm/llms/anthropic.py
+++ b/litellm/llms/anthropic.py
@@ -507,17 +507,23 @@ async def make_call(
model: str,
messages: list,
logging_obj,
+ timeout: Optional[Union[float, httpx.Timeout]],
):
if client is None:
client = _get_async_httpx_client() # Create a new client if none provided
try:
- response = await client.post(api_base, headers=headers, data=data, stream=True)
+ response = await client.post(
+ api_base, headers=headers, data=data, stream=True, timeout=timeout
+ )
except httpx.HTTPStatusError as e:
raise AnthropicError(
status_code=e.response.status_code, message=await e.response.aread()
)
except Exception as e:
+ for exception in litellm.LITELLM_EXCEPTION_TYPES:
+ if isinstance(e, exception):
+ raise e
raise AnthropicError(status_code=500, message=str(e))
if response.status_code != 200:
@@ -540,6 +546,51 @@ async def make_call(
return completion_stream
+def make_sync_call(
+ client: Optional[HTTPHandler],
+ api_base: str,
+ headers: dict,
+ data: str,
+ model: str,
+ messages: list,
+ logging_obj,
+ timeout: Optional[Union[float, httpx.Timeout]],
+):
+ if client is None:
+ client = HTTPHandler() # Create a new client if none provided
+
+ try:
+ response = client.post(
+ api_base, headers=headers, data=data, stream=True, timeout=timeout
+ )
+ except httpx.HTTPStatusError as e:
+ raise AnthropicError(
+ status_code=e.response.status_code, message=e.response.read()
+ )
+ except Exception as e:
+ for exception in litellm.LITELLM_EXCEPTION_TYPES:
+ if isinstance(e, exception):
+ raise e
+ raise AnthropicError(status_code=500, message=str(e))
+
+ if response.status_code != 200:
+ raise AnthropicError(status_code=response.status_code, message=response.read())
+
+ completion_stream = ModelResponseIterator(
+ streaming_response=response.iter_lines(), sync_stream=True
+ )
+
+ # LOGGING
+ logging_obj.post_call(
+ input=messages,
+ api_key="",
+ original_response="first stream response received",
+ additional_args={"complete_input_dict": data},
+ )
+
+ return completion_stream
+
+
class AnthropicChatCompletion(BaseLLM):
def __init__(self) -> None:
super().__init__()
@@ -647,6 +698,7 @@ class AnthropicChatCompletion(BaseLLM):
custom_prompt_dict: dict,
model_response: ModelResponse,
print_verbose: Callable,
+ timeout: Union[float, httpx.Timeout],
encoding,
api_key,
logging_obj,
@@ -659,20 +711,6 @@ class AnthropicChatCompletion(BaseLLM):
headers={},
):
data["stream"] = True
- # async_handler = AsyncHTTPHandler(
- # timeout=httpx.Timeout(timeout=600.0, connect=20.0)
- # )
-
- # response = await async_handler.post(
- # api_base, headers=headers, json=data, stream=True
- # )
-
- # if response.status_code != 200:
- # raise AnthropicError(
- # status_code=response.status_code, message=response.text
- # )
-
- # completion_stream = response.aiter_lines()
streamwrapper = CustomStreamWrapper(
completion_stream=None,
@@ -685,6 +723,7 @@ class AnthropicChatCompletion(BaseLLM):
model=model,
messages=messages,
logging_obj=logging_obj,
+ timeout=timeout,
),
model=model,
custom_llm_provider="anthropic",
@@ -700,6 +739,7 @@ class AnthropicChatCompletion(BaseLLM):
custom_prompt_dict: dict,
model_response: ModelResponse,
print_verbose: Callable,
+ timeout: Union[float, httpx.Timeout],
encoding,
api_key,
logging_obj,
@@ -716,7 +756,9 @@ class AnthropicChatCompletion(BaseLLM):
async_handler = _get_async_httpx_client()
try:
- response = await async_handler.post(api_base, headers=headers, json=data)
+ response = await async_handler.post(
+ api_base, headers=headers, json=data, timeout=timeout
+ )
except Exception as e:
## LOGGING
logging_obj.post_call(
@@ -876,6 +918,7 @@ class AnthropicChatCompletion(BaseLLM):
litellm_params=litellm_params,
logger_fn=logger_fn,
headers=headers,
+ timeout=timeout,
)
else:
return self.acompletion_function(
@@ -897,43 +940,40 @@ class AnthropicChatCompletion(BaseLLM):
headers=headers,
client=client,
json_mode=json_mode,
+ timeout=timeout,
)
else:
## COMPLETION CALL
- if client is None or isinstance(client, AsyncHTTPHandler):
+ if client is None or not isinstance(client, HTTPHandler):
client = HTTPHandler(timeout=timeout) # type: ignore
else:
client = client
if (
stream is True
): # if function call - fake the streaming (need complete blocks for output parsing in openai format)
- print_verbose("makes anthropic streaming POST request")
data["stream"] = stream
- response = requests.post(
- api_base,
- headers=headers,
- data=json.dumps(data),
- stream=stream,
- )
-
- if response.status_code != 200:
- raise AnthropicError(
- status_code=response.status_code, message=response.text
- )
-
- completion_stream = ModelResponseIterator(
- streaming_response=response.iter_lines(), sync_stream=True
- )
- streaming_response = CustomStreamWrapper(
- completion_stream=completion_stream,
+ return CustomStreamWrapper(
+ completion_stream=None,
+ make_call=partial(
+ make_sync_call,
+ client=None,
+ api_base=api_base,
+ headers=headers, # type: ignore
+ data=json.dumps(data),
+ model=model,
+ messages=messages,
+ logging_obj=logging_obj,
+ timeout=timeout,
+ ),
model=model,
custom_llm_provider="anthropic",
logging_obj=logging_obj,
)
- return streaming_response
else:
- response = client.post(api_base, headers=headers, data=json.dumps(data))
+ response = client.post(
+ api_base, headers=headers, data=json.dumps(data), timeout=timeout
+ )
if response.status_code != 200:
raise AnthropicError(
status_code=response.status_code, message=response.text
diff --git a/litellm/tests/test_timeout.py b/litellm/tests/test_timeout.py
index f24b26a0cf2..b74cf89eaa4 100644
--- a/litellm/tests/test_timeout.py
+++ b/litellm/tests/test_timeout.py
@@ -1,16 +1,21 @@
#### What this tests ####
# This tests the timeout decorator
-import sys, os
+import os
+import sys
import traceback
sys.path.insert(
0, os.path.abspath("../..")
) # Adds the parent directory to the system path
import time
-import litellm
+import uuid
+
+import httpx
import openai
-import pytest, uuid, httpx
+import pytest
+
+import litellm
@pytest.mark.parametrize(
@@ -245,3 +250,39 @@ def test_timeout_ollama():
# test_timeout_ollama()
+
+
[email protected]("streaming", [True, False])
[email protected]("sync_mode", [True, False])
+async def test_anthropic_timeout(streaming, sync_mode):
+ litellm.set_verbose = False
+
+ try:
+ if sync_mode:
+ response = litellm.completion(
+ model="claude-3-5-sonnet-20240620",
+ timeout=0.01,
+ messages=[{"role": "user", "content": "hello, write a 20 pg essay"}],
+ stream=streaming,
+ )
+ if isinstance(response, litellm.CustomStreamWrapper):
+ for chunk in response:
+ pass
+ else:
+ response = await litellm.acompletion(
+ model="claude-3-5-sonnet-20240620",
+ timeout=0.01,
+ messages=[{"role": "user", "content": "hello, write a 20 pg essay"}],
+ stream=streaming,
+ )
+ if isinstance(response, litellm.CustomStreamWrapper):
+ async for chunk in response:
+ pass
+ pytest.fail("Did not raise error `openai.APITimeoutError`")
+ except openai.APITimeoutError as e:
+ print(
+ "Passed: Raised correct exception. Got openai.APITimeoutError\nGood Job", e
+ )
+ print(type(e))
+ pass