:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(langsmith): say what to do when the credential check fails (#100802)
Tom Owers63179b2b8a38
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/langsmith.py b/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/langsmith.pyindex 266cc9758f8f..1776b8ea605c 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/langsmith.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/langsmith.py@@ -239,6 +239,9 @@ def _check_host(base_url: str, team_id: int) -> None:raise LangSmithHostNotAllowedError(scheme_err or INSECURE_SCHEME_ERROR)+_LANGSMITH_UNREACHABLE_ERROR = "Couldn't reach LangSmith to validate your API key. Try again in a few minutes."++def _get_headers(api_key: str) -> dict[str, str]:return {"X-API-Key": api_key,@@ -323,18 +326,39 @@ def validate_credentials(api_key: str, host: str | None, team_id: int | None = Nstatus_code = response.status_codefinally:response.close()- except requests.exceptions.RequestException as e:- return False, str(e)+ except requests.exceptions.RequestException:+ # A network failure or timeout is transient and unrelated to the key; the raw exception+ # embeds the URL and gives the user nothing actionable.+ return False, _LANGSMITH_UNREACHABLE_ERRORif status_code == 200:return True, Noneif status_code == 401:- return False, "Invalid or revoked LangSmith API key"+ return (+ False,+ "Your LangSmith API key is invalid or has been revoked. Create a new key in your "+ "LangSmith settings under API keys, then reconnect.",+ )if status_code == 403:- return False, "This LangSmith API key does not have access to the workspace"+ return (+ False,+ "Your LangSmith API key can't read this workspace. Create a key in the workspace you "+ "want to sync, then reconnect.",+ )if status_code == 404:- return False, "LangSmith API not found at this host. Check the host field."- return False, f"LangSmith API returned status {status_code}"+ return (+ False,+ "PostHog reached this host but found no LangSmith API there. Check the host field, then try again.",+ )+ # 429 (rate limit) and 5xx are transient LangSmith-side problems, not a bad key, so surface a+ # retry hint rather than telling the user to fix credentials they can't fix.+ if status_code == 429 or status_code >= 500:+ return False, _LANGSMITH_UNREACHABLE_ERROR+ return (+ False,+ "Couldn't validate your LangSmith API key. Check that it's a valid key from your LangSmith "+ "settings, then try again.",+ )@retry(diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/tests/test_langsmith.py b/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/tests/test_langsmith.pyindex 4ff1bd1d0513..6dafc7a4c0b7 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/tests/test_langsmith.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/langsmith/tests/test_langsmith.py@@ -4,6 +4,7 @@ from typing import Anyimport pytestfrom unittest import mock+import requestsimport structlogfrom products.warehouse_sources.backend.temporal.data_imports.sources.langsmith.langsmith import (@@ -639,10 +640,20 @@ class TestHostSafety:class TestValidateCredentials:@pytest.mark.parametrize(- "status_code,expected_valid",- [(200, True), (401, False), (403, False), (404, False), (500, False)],+ "status_code,expected_fragment",+ [+ (200, None),+ (401, "invalid or has been revoked"),+ (403, "read this workspace"),+ (404, "no LangSmith API there"),+ # A rate limit and a LangSmith outage are not a bad key, so both ask for a retry+ # instead of sending the user to replace a key that works.+ (429, "Try again in a few minutes"),+ (500, "Try again in a few minutes"),+ (418, "valid key from your LangSmith"),+ ],)- def test_status_mapping(self, status_code, expected_valid):+ def test_status_mapping(self, status_code, expected_fragment):response = mock.MagicMock()response.status_code = status_code@@ -650,9 +661,26 @@ class TestValidateCredentials:session.return_value.get.return_value = responsevalid, message = validate_credentials("key", None)- assert valid is expected_valid- if not expected_valid:- assert message+ assert valid is (expected_fragment is None)+ if expected_fragment is None:+ assert message is None+ else:+ assert message is not None+ assert expected_fragment in message+ # The wizard shows this message verbatim, so no status code may reach it.+ assert str(status_code) not in message++ def test_unreachable_host_does_not_surface_the_raw_exception(self):+ with mock.patch(_MAKE_SESSION) as session:+ session.return_value.get.side_effect = requests.ConnectionError(+ "HTTPSConnectionPool(host='langsmith.example', port=443): Max retries exceeded"+ )+ valid, message = validate_credentials("key", None)++ assert valid is False+ assert message is not None+ assert "HTTPSConnectionPool" not in message+ assert "Try again in a few minutes" in messagedef test_unsafe_host_fails_before_network_call(self):with (