Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community: add request_timeout and max_retries to ChatAnthropic #19402

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions libs/partners/anthropic/langchain_anthropic/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,11 @@ class Config:
"""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"

Expand Down Expand Up @@ -286,16 +290,23 @@ def validate_environment(cls, values: Dict) -> Dict:
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(
Expand Down
Loading