Skip to content

The agent engineering platform.

community: add request_timeout and max_retries to ChatAnthropic (#19402)

kaijiettia812839f0cd1
diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py
index 20afaa8cef..7825a16518 100644
--- a/libs/partners/anthropic/langchain_anthropic/chat_models.py
+++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py
@@ -244,7 +244,11 @@ class ChatAnthropic(BaseChatModel):
"""Total probability mass of tokens to consider at each step."""
default_request_timeout: Optional[float] = Field(None, alias="timeout")
- """Timeout for requests to Anthropic Completion API. Default is 600 seconds."""
+ """Timeout for requests to Anthropic Completion API."""
+
+ # sdk default = 2: https://github.com/anthropics/anthropic-sdk-python?tab=readme-ov-file#retries
+ max_retries: int = 2
+ """Number of retries allowed for requests sent to the Anthropic Completion API."""
anthropic_api_url: str = "https://api.anthropic.com"
@@ -286,16 +290,23 @@ class ChatAnthropic(BaseChatModel):
or "https://api.anthropic.com"
)
values["anthropic_api_url"] = api_url
- values["_client"] = anthropic.Client(
- api_key=api_key,
- base_url=api_url,
- default_headers=values.get("default_headers"),
- )
- values["_async_client"] = anthropic.AsyncClient(
- api_key=api_key,
- base_url=api_url,
- default_headers=values.get("default_headers"),
- )
+ client_params = {
+ "api_key": api_key,
+ "base_url": api_url,
+ "max_retries": values["max_retries"],
+ "default_headers": values.get("default_headers"),
+ }
+ # value <= 0 indicates the param should be ignored. None is a meaningful value
+ # for Anthropic client and treated differently than not specifying the param at
+ # all.
+ if (
+ values["default_request_timeout"] is None
+ or values["default_request_timeout"] > 0
+ ):
+ client_params["timeout"] = values["default_request_timeout"]
+
+ values["_client"] = anthropic.Client(**client_params)
+ values["_async_client"] = anthropic.AsyncClient(**client_params)
return values
def _format_params(