mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-01 02:43:37 +00:00
anthropic[patch]: add tests for secret_str for api key (#20986)
**Description:** Add tests to check API keys are masked **Issue:** Resolves https://github.com/langchain-ai/langchain/issues/12165 for Anthropic models **Dependencies:** None
This commit is contained in:
parent
1fad39be1c
commit
8b59bddc03
@ -9,6 +9,7 @@ from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, Tool
|
|||||||
from langchain_core.outputs import ChatGeneration, ChatResult
|
from langchain_core.outputs import ChatGeneration, ChatResult
|
||||||
from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr
|
from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr
|
||||||
from langchain_core.tools import BaseTool
|
from langchain_core.tools import BaseTool
|
||||||
|
from pytest import CaptureFixture, MonkeyPatch
|
||||||
|
|
||||||
from langchain_anthropic import ChatAnthropic
|
from langchain_anthropic import ChatAnthropic
|
||||||
from langchain_anthropic.chat_models import (
|
from langchain_anthropic.chat_models import (
|
||||||
@ -419,3 +420,53 @@ def test__format_messages_with_list_content_and_tool_calls() -> None:
|
|||||||
)
|
)
|
||||||
actual = _format_messages(messages)
|
actual = _format_messages(messages)
|
||||||
assert expected == actual
|
assert expected == actual
|
||||||
|
|
||||||
|
|
||||||
|
def test_anthropic_api_key_is_secret_string() -> None:
|
||||||
|
"""Test that the API key is stored as a SecretStr."""
|
||||||
|
chat_model = ChatAnthropic(
|
||||||
|
model="claude-3-opus-20240229",
|
||||||
|
anthropic_api_key="secret-api-key",
|
||||||
|
)
|
||||||
|
assert isinstance(chat_model.anthropic_api_key, SecretStr)
|
||||||
|
|
||||||
|
|
||||||
|
def test_anthropic_api_key_masked_when_passed_from_env(
|
||||||
|
monkeypatch: MonkeyPatch, capsys: CaptureFixture
|
||||||
|
) -> None:
|
||||||
|
"""Test that the API key is masked when passed from an environment variable."""
|
||||||
|
monkeypatch.setenv("ANTHROPIC_API_KEY ", "secret-api-key")
|
||||||
|
chat_model = ChatAnthropic(
|
||||||
|
model="claude-3-opus-20240229",
|
||||||
|
anthropic_api_key="secret-api-key",
|
||||||
|
)
|
||||||
|
print(chat_model.anthropic_api_key, end="") # noqa: T201
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
|
||||||
|
assert captured.out == "**********"
|
||||||
|
|
||||||
|
|
||||||
|
def test_anthropic_api_key_masked_when_passed_via_constructor(
|
||||||
|
capsys: CaptureFixture,
|
||||||
|
) -> None:
|
||||||
|
"""Test that the API key is masked when passed via the constructor."""
|
||||||
|
chat_model = ChatAnthropic(
|
||||||
|
model="claude-3-opus-20240229",
|
||||||
|
anthropic_api_key="secret-api-key",
|
||||||
|
)
|
||||||
|
print(chat_model.anthropic_api_key, end="") # noqa: T201
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
|
||||||
|
assert captured.out == "**********"
|
||||||
|
|
||||||
|
|
||||||
|
def test_anthropic_uses_actual_secret_value_from_secretstr() -> None:
|
||||||
|
"""Test that the actual secret value is correctly retrieved."""
|
||||||
|
chat_model = ChatAnthropic(
|
||||||
|
model="claude-3-opus-20240229",
|
||||||
|
anthropic_api_key="secret-api-key",
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
cast(SecretStr, chat_model.anthropic_api_key).get_secret_value()
|
||||||
|
== "secret-api-key"
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user