Skip to content

The agent engineering platform.

anthropic[patch]: standardize init args (#20161)

Bagatura27d88f12aea
diff --git a/docs/docs/get_started/quickstart.mdx b/docs/docs/get_started/quickstart.mdx
index 3a4d7d9de7..7cb7bb7679 100644
--- a/docs/docs/get_started/quickstart.mdx
+++ b/docs/docs/get_started/quickstart.mdx
@@ -141,10 +141,10 @@ from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(model="claude-3-sonnet-20240229", temperature=0.2, max_tokens=1024)
```
-If you'd prefer not to set an environment variable you can pass the key in directly via the `anthropic_api_key` named parameter when initiating the Anthropic Chat Model class:
+If you'd prefer not to set an environment variable you can pass the key in directly via the `api_key` named parameter when initiating the Anthropic Chat Model class:
```python
-llm = ChatAnthropic(anthropic_api_key="...")
+llm = ChatAnthropic(api_key="...")
```
</TabItem>
diff --git a/docs/docs/integrations/chat/anthropic.ipynb b/docs/docs/integrations/chat/anthropic.ipynb
index d8c9ff9f8d..f08351d5ab 100644
--- a/docs/docs/integrations/chat/anthropic.ipynb
+++ b/docs/docs/integrations/chat/anthropic.ipynb
@@ -69,7 +69,7 @@
"source": [
"The code provided assumes that your ANTHROPIC_API_KEY is set in your environment variables. If you would like to manually specify your API key and also choose a different model, you can use the following code:\n",
"```python\n",
- "chat = ChatAnthropic(temperature=0, anthropic_api_key=\"YOUR_API_KEY\", model_name=\"claude-3-opus-20240229\")\n",
+ "chat = ChatAnthropic(temperature=0, api_key=\"YOUR_API_KEY\", model_name=\"claude-3-opus-20240229\")\n",
"\n",
"```\n",
"\n",
diff --git a/docs/docs/modules/model_io/chat/quick_start.ipynb b/docs/docs/modules/model_io/chat/quick_start.ipynb
index 5b827fa44f..8e69a258eb 100644
--- a/docs/docs/modules/model_io/chat/quick_start.ipynb
+++ b/docs/docs/modules/model_io/chat/quick_start.ipynb
@@ -52,8 +52,8 @@
"source": [
"```{=mdx}\n",
"<ChatModelTabs\n",
+ " anthropicParams={`model=\"claude-3-sonnet-20240229\", api_key=\"...\"`}\n",
" openaiParams={`model=\"gpt-3.5-turbo-0125\", api_key=\"...\"`}\n",
- " anthropicParams={`model=\"claude-3-sonnet-20240229\", anthropic_api_key=\"...\"`}\n",
" mistralParams={`model=\"mistral-large-latest\", api_key=\"...\"`}\n",
" fireworksParams={`model=\"accounts/fireworks/models/mixtral-8x7b-instruct\", api_key=\"...\"`}\n",
" googleParams={`model=\"gemini-pro\", google_api_key=\"...\"`}\n",
diff --git a/docs/docs/modules/model_io/index.mdx b/docs/docs/modules/model_io/index.mdx
index 55d1c0cc45..d6850cdd4f 100644
--- a/docs/docs/modules/model_io/index.mdx
+++ b/docs/docs/modules/model_io/index.mdx
@@ -158,10 +158,10 @@ from langchain_anthropic import ChatAnthropic
chat_model = ChatAnthropic(model="claude-3-sonnet-20240229", temperature=0.2, max_tokens=1024)
```
-If you'd prefer not to set an environment variable you can pass the key in directly via the `anthropic_api_key` named parameter when initiating the Anthropic Chat Model class:
+If you'd prefer not to set an environment variable you can pass the key in directly via the `api_key` named parameter when initiating the Anthropic Chat Model class:
```python
-chat_model = ChatAnthropic(anthropic_api_key="...")
+chat_model = ChatAnthropic(api_key="...")
```
</TabItem>
diff --git a/docs/docs/modules/model_io/quick_start.mdx b/docs/docs/modules/model_io/quick_start.mdx
index 3aba7516b9..26d1ed3bfe 100644
--- a/docs/docs/modules/model_io/quick_start.mdx
+++ b/docs/docs/modules/model_io/quick_start.mdx
@@ -87,10 +87,10 @@ from langchain_anthropic import ChatAnthropic
chat_model = ChatAnthropic(model="claude-3-sonnet-20240229", temperature=0.2, max_tokens=1024)
```
-If you'd prefer not to set an environment variable you can pass the key in directly via the `anthropic_api_key` named parameter when initiating the Anthropic Chat Model class:
+If you'd prefer not to set an environment variable you can pass the key in directly via the `api_key` named parameter when initiating the Anthropic Chat Model class:
```python
-chat_model = ChatAnthropic(anthropic_api_key="...")
+chat_model = ChatAnthropic(api_key="...")
```
</TabItem>
diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py
index ef09aa3799..20afaa8cef 100644
--- a/libs/partners/anthropic/langchain_anthropic/chat_models.py
+++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py
@@ -243,12 +243,13 @@ class ChatAnthropic(BaseChatModel):
top_p: Optional[float] = None
"""Total probability mass of tokens to consider at each step."""
- default_request_timeout: Optional[float] = None
+ default_request_timeout: Optional[float] = Field(None, alias="timeout")
"""Timeout for requests to Anthropic Completion API. Default is 600 seconds."""
anthropic_api_url: str = "https://api.anthropic.com"
- anthropic_api_key: Optional[SecretStr] = None
+ anthropic_api_key: Optional[SecretStr] = Field(None, alias="api_key")
+ """Automatically read from env var `ANTHROPIC_API_KEY` if not provided."""
default_headers: Optional[Mapping[str, str]] = None
"""Headers to pass to the Anthropic clients, will be used for every API call."""
diff --git a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py b/libs/partners/anthropic/tests/unit_tests/test_chat_models.py
index b38d5da3ca..85019553fe 100644
--- a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py
+++ b/libs/partners/anthropic/tests/unit_tests/test_chat_models.py
@@ -1,16 +1,16 @@
"""Test chat model integration."""
import os
-from typing import Any, Callable, Dict, Literal, Type
+from typing import Any, Callable, Dict, Literal, Type, cast
import pytest
from anthropic.types import ContentBlock, Message, Usage
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage
from langchain_core.outputs import ChatGeneration, ChatResult
-from langchain_core.pydantic_v1 import BaseModel, Field
+from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr
from langchain_core.tools import BaseTool
-from langchain_anthropic import ChatAnthropic, ChatAnthropicMessages
+from langchain_anthropic import ChatAnthropic
from langchain_anthropic.chat_models import _merge_messages, convert_to_anthropic_tool
os.environ["ANTHROPIC_API_KEY"] = "foo"
@@ -18,8 +18,17 @@ os.environ["ANTHROPIC_API_KEY"] = "foo"
def test_initialization() -> None:
"""Test chat model initialization."""
- ChatAnthropicMessages(model_name="claude-instant-1.2", anthropic_api_key="xyz")
- ChatAnthropicMessages(model="claude-instant-1.2", anthropic_api_key="xyz")
+ for model in [
+ ChatAnthropic(model_name="claude-instant-1.2", api_key="xyz", timeout=2),
+ ChatAnthropic(
+ model="claude-instant-1.2",
+ anthropic_api_key="xyz",
+ default_request_timeout=2,
+ ),
+ ]:
+ assert model.model == "claude-instant-1.2"
+ assert cast(SecretStr, model.anthropic_api_key).get_secret_value() == "xyz"
+ assert model.default_request_timeout == 2.0
@pytest.mark.requires("anthropic")