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

allow langsmith and langchain env vars #518

Merged
merged 2 commits into from Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions python/langsmith/client.py
Expand Up @@ -288,19 +288,21 @@ 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:
def _get_api_url(api_url: Optional[str]) -> str:
_api_url = (
api_url
if api_url is not None
else os.getenv(
"LANGCHAIN_ENDPOINT",
"https://api.smith.langchain.com",
or os.getenv(
"LANGSMITH_ENDPOINT",
os.getenv(
"LANGCHAIN_ENDPOINT",
"https://api.smith.langchain.com",
)
)
)
if not _api_url.strip():
Expand Down Expand Up @@ -413,7 +415,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
61 changes: 61 additions & 0 deletions python/tests/unit_tests/test_client.py
Expand Up @@ -57,13 +57,74 @@ 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