Skip to content

Commit

Permalink
allow langsmith and langchain env vars (#518)
Browse files Browse the repository at this point in the history
Co-authored-by: William Fu-Hinthorn <[email protected]>
  • Loading branch information
agola11 and hinthornw authored Mar 13, 2024
1 parent 19cd100 commit d52ce1e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
15 changes: 7 additions & 8 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,20 +288,19 @@ def _get_tracing_sampling_rate() -> float | None:


def _get_api_key(api_key: Optional[str]) -> Optional[str]:
api_key = api_key if api_key is not None else os.getenv("LANGCHAIN_API_KEY")
api_key = api_key or os.getenv("LANGSMITH_API_KEY", os.getenv("LANGCHAIN_API_KEY"))
if api_key is None or not api_key.strip():
return None
return api_key.strip().strip('"').strip("'")


def _get_api_url(api_url: Optional[str], api_key: Optional[str]) -> str:
_api_url = (
api_url
if api_url is not None
else os.getenv(
def _get_api_url(api_url: Optional[str]) -> str:
_api_url = api_url or os.getenv(
"LANGSMITH_ENDPOINT",
os.getenv(
"LANGCHAIN_ENDPOINT",
"https://api.smith.langchain.com",
)
),
)
if not _api_url.strip():
raise ls_utils.LangSmithUserError("LangSmith API URL cannot be empty")
Expand Down Expand Up @@ -413,7 +412,7 @@ def __init__(
self.tracing_sample_rate = _get_tracing_sampling_rate()
self._sampled_post_uuids: set[uuid.UUID] = set()
self.api_key = _get_api_key(api_key)
self.api_url = _get_api_url(api_url, self.api_key)
self.api_url = _get_api_url(api_url)
_validate_api_key_if_hosted(self.api_url, self.api_key)
self.retry_config = retry_config or _default_retry_config()
self.timeout_ms = timeout_ms or 10000
Expand Down
65 changes: 65 additions & 0 deletions python/tests/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,78 @@ def test__is_langchain_hosted() -> None:

def test_validate_api_key_if_hosted(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("LANGCHAIN_API_KEY", raising=False)
monkeypatch.delenv("LANGSMITH_API_KEY", raising=False)
with pytest.raises(ls_utils.LangSmithUserError, match="API key must be provided"):
Client(api_url="https://api.smith.langchain.com")
client = Client(api_url="http://localhost:1984")
assert client.api_url == "http://localhost:1984"
assert client.api_key is None


def test_validate_api_url(monkeypatch: pytest.MonkeyPatch) -> None:
# Scenario 1: Both LANGCHAIN_ENDPOINT and LANGSMITH_ENDPOINT
# are set, but api_url is not
monkeypatch.setenv("LANGCHAIN_ENDPOINT", "https://api.smith.langchain-endpoint.com")
monkeypatch.setenv("LANGSMITH_ENDPOINT", "https://api.smith.langsmith-endpoint.com")

client = Client()
assert client.api_url == "https://api.smith.langsmith-endpoint.com"

# Scenario 2: Both LANGCHAIN_ENDPOINT and LANGSMITH_ENDPOINT
# are set, and api_url is set
monkeypatch.setenv("LANGCHAIN_ENDPOINT", "https://api.smith.langchain-endpoint.com")
monkeypatch.setenv("LANGSMITH_ENDPOINT", "https://api.smith.langsmith-endpoint.com")

client = Client(api_url="https://api.smith.langchain.com", api_key="123")
assert client.api_url == "https://api.smith.langchain.com"

# Scenario 3: LANGCHAIN_ENDPOINT is set, but LANGSMITH_ENDPOINT is not
monkeypatch.setenv("LANGCHAIN_ENDPOINT", "https://api.smith.langchain-endpoint.com")
monkeypatch.delenv("LANGSMITH_ENDPOINT", raising=False)

client = Client()
assert client.api_url == "https://api.smith.langchain-endpoint.com"

# Scenario 4: LANGCHAIN_ENDPOINT is not set, but LANGSMITH_ENDPOINT is set
monkeypatch.delenv("LANGCHAIN_ENDPOINT", raising=False)
monkeypatch.setenv("LANGSMITH_ENDPOINT", "https://api.smith.langsmith-endpoint.com")

client = Client()
assert client.api_url == "https://api.smith.langsmith-endpoint.com"


def test_validate_api_key(monkeypatch: pytest.MonkeyPatch) -> None:
# Scenario 1: Both LANGCHAIN_API_KEY and LANGSMITH_API_KEY are set,
# but api_key is not
monkeypatch.setenv("LANGCHAIN_API_KEY", "env_langchain_api_key")
monkeypatch.setenv("LANGSMITH_API_KEY", "env_langsmith_api_key")

client = Client()
assert client.api_key == "env_langsmith_api_key"

# Scenario 2: Both LANGCHAIN_API_KEY and LANGSMITH_API_KEY are set,
# and api_key is set
monkeypatch.setenv("LANGCHAIN_API_KEY", "env_langchain_api_key")
monkeypatch.setenv("LANGSMITH_API_KEY", "env_langsmith_api_key")

client = Client(api_url="https://api.smith.langchain.com", api_key="123")
assert client.api_key == "123"

# Scenario 3: LANGCHAIN_API_KEY is set, but LANGSMITH_API_KEY is not
monkeypatch.setenv("LANGCHAIN_API_KEY", "env_langchain_api_key")
monkeypatch.delenv("LANGSMITH_API_KEY", raising=False)

client = Client()
assert client.api_key == "env_langchain_api_key"

# Scenario 4: LANGCHAIN_API_KEY is not set, but LANGSMITH_API_KEY is set
monkeypatch.delenv("LANGCHAIN_API_KEY", raising=False)
monkeypatch.setenv("LANGSMITH_API_KEY", "env_langsmith_api_key")

client = Client()
assert client.api_key == "env_langsmith_api_key"


def test_headers(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("LANGCHAIN_API_KEY", raising=False)
client = Client(api_url="http://localhost:1984", api_key="123")
Expand Down

0 comments on commit d52ce1e

Please sign in to comment.