Skip to content

The agent engineering platform.

fix(core): fall back to `LANGSMITH_API_KEY` for gateway (#39115)

ccurmecdca311de295
diff --git a/libs/core/langchain_core/utils/_gateway.py b/libs/core/langchain_core/utils/_gateway.py
index 76a3112f4a..6bcab95890 100644
--- a/libs/core/langchain_core/utils/_gateway.py
+++ b/libs/core/langchain_core/utils/_gateway.py
@@ -22,6 +22,7 @@ if TYPE_CHECKING:
_LANGSMITH_GATEWAY_ENV = "LANGSMITH_GATEWAY"
_LANGSMITH_GATEWAY_API_KEY_ENV = "LANGSMITH_GATEWAY_API_KEY"
+_LANGSMITH_API_KEY_ENV = "LANGSMITH_API_KEY"
_LANGSMITH_GATEWAY_DEFAULT_BASE = "https://gateway.smith.langchain.com"
_TRUE_VALUES = ("true", "1", "yes")
@@ -107,7 +108,9 @@ def _resolve_gateway_config(
> ``default_base_url``.
- **api_key:** explicit ``api_key`` (returned unchanged) > the gateway key if
the base URL came from the gateway, otherwise the provider key > the other.
- The gateway key is only a candidate when the gateway is enabled.
+ The gateway key uses ``LANGSMITH_GATEWAY_API_KEY`` when the gateway is
+ enabled, with ``LANGSMITH_API_KEY`` as a fallback only when the base URL came
+ from the gateway.
The provenance flip on the key means an ``OPENAI_API_KEY``-style provider key
is preferred whenever the caller pointed the base URL at a non-gateway
@@ -151,6 +154,8 @@ def _resolve_gateway_config(
if gateway_base_url is not None
else None
)
+ if not gateway_api_key and base_url_from_gateway:
+ gateway_api_key = os.getenv(_LANGSMITH_API_KEY_ENV)
provider_api_key = _first_env(api_key_env)
chosen = (
(gateway_api_key or provider_api_key)
diff --git a/libs/core/tests/unit_tests/utils/test_gateway.py b/libs/core/tests/unit_tests/utils/test_gateway.py
index 58deae0d1d..362422cfa7 100644
--- a/libs/core/tests/unit_tests/utils/test_gateway.py
+++ b/libs/core/tests/unit_tests/utils/test_gateway.py
@@ -25,6 +25,7 @@ def _clear_env(monkeypatch: pytest.MonkeyPatch) -> None:
for var in (
"LANGSMITH_GATEWAY",
"LANGSMITH_GATEWAY_API_KEY",
+ "LANGSMITH_API_KEY",
_BASE_ENV,
_KEY_ENV,
):
@@ -91,6 +92,26 @@ def test_gateway_on_with_gateway_key(monkeypatch: pytest.MonkeyPatch) -> None:
assert config.api_key.get_secret_value() == "gateway-key"
+def test_gateway_on_falls_back_to_langsmith_key(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setenv("LANGSMITH_GATEWAY", "true")
+ monkeypatch.setenv("LANGSMITH_API_KEY", "langsmith-key")
+ config = _config()
+ assert config.base_url == _DEFAULT_GATEWAY
+ assert isinstance(config.api_key, SecretStr)
+ assert config.api_key.get_secret_value() == "langsmith-key"
+
+
+def test_gateway_key_beats_langsmith_key(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setenv("LANGSMITH_GATEWAY", "true")
+ monkeypatch.setenv("LANGSMITH_GATEWAY_API_KEY", "gateway-key")
+ monkeypatch.setenv("LANGSMITH_API_KEY", "langsmith-key")
+ config = _config()
+ assert isinstance(config.api_key, SecretStr)
+ assert config.api_key.get_secret_value() == "gateway-key"
+
+
def test_gateway_key_ignored_when_gateway_off(
monkeypatch: pytest.MonkeyPatch,
) -> None:
@@ -147,6 +168,17 @@ def test_provider_base_url_uses_provider_key(
assert config.api_key.get_secret_value() == "provider-key"
+def test_provider_base_url_does_not_use_langsmith_key(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setenv("LANGSMITH_GATEWAY", "true")
+ monkeypatch.setenv("LANGSMITH_API_KEY", "langsmith-key")
+ monkeypatch.setenv(_BASE_ENV, "https://api.openai.com/v1")
+ config = _config()
+ assert config.base_url == "https://api.openai.com/v1"
+ assert config.api_key is None
+
+
def test_provider_base_url_falls_back_to_gateway_key(
monkeypatch: pytest.MonkeyPatch,
) -> None: