From 4fe9d3062f4b68e2e472eb92decf369c93aebb46 Mon Sep 17 00:00:00 2001 From: ccurme Date: Thu, 27 Aug 2026 15:21:39 -0400 Subject: [PATCH] chore(openai): fix tests (#39972) --- .github/workflows/_release.yml | 4 ++- .../openai/tests/unit_tests/conftest.py | 35 +++++++++++++++++++ .../tests/unit_tests/embeddings/test_base.py | 3 -- .../openai/tests/unit_tests/llms/test_base.py | 4 --- 4 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 libs/partners/openai/tests/unit_tests/conftest.py diff --git a/.github/workflows/_release.yml b/.github/workflows/_release.yml index 8a2e89553b..6220efdf5d 100644 --- a/.github/workflows/_release.yml +++ b/.github/workflows/_release.yml @@ -737,7 +737,9 @@ jobs: # Run tests uv sync --group test --group test_integration uv pip install $PRERELEASE_FLAG ../../core/dist/*.whl - make test + + env -u LANGSMITH_GATEWAY -u LANGSMITH_GATEWAY_API_KEY -u OPENAI_API_KEY make test + make integration_tests # Test external packages that depend on langchain-core/langchain against the new release diff --git a/libs/partners/openai/tests/unit_tests/conftest.py b/libs/partners/openai/tests/unit_tests/conftest.py new file mode 100644 index 0000000000..c43183772c --- /dev/null +++ b/libs/partners/openai/tests/unit_tests/conftest.py @@ -0,0 +1,35 @@ +"""Shared fixtures for `langchain-openai` unit tests.""" + +import pytest + +# Set by CI (and by developers routing local traffic through the LangSmith +# gateway). `ChatOpenAI` resolves these at construction time and swaps in the +# gateway base URL and key, which flips `_uses_gateway` and diverts mocked +# clients down the `with_raw_response` path. +_GATEWAY_VARS = ( + "LANGSMITH_GATEWAY", + "LANGSMITH_GATEWAY_API_KEY", + "LANGSMITH_API_KEY", +) + +_PROVIDER_VARS = ( + "OPENAI_BASE_URL", + "OPENAI_API_BASE", + "OPENAI_ORG_ID", + "OPENAI_ORGANIZATION", + "OPENAI_PROXY", +) + + +@pytest.fixture(autouse=True) +def hermetic_env(monkeypatch: pytest.MonkeyPatch) -> None: + """Isolate unit tests from ambient OpenAI and gateway configuration. + + Unit tests run offline against mocked clients, so any real credential or + base URL in the environment can only change behavior for the worse. Tests + that exercise environment handling override these with their own + `monkeypatch` calls, which still take precedence. + """ + for var in (*_GATEWAY_VARS, *_PROVIDER_VARS): + monkeypatch.delenv(var, raising=False) + monkeypatch.setenv("OPENAI_API_KEY", "foo") diff --git a/libs/partners/openai/tests/unit_tests/embeddings/test_base.py b/libs/partners/openai/tests/unit_tests/embeddings/test_base.py index 7169fb7bb5..a0e671dc54 100644 --- a/libs/partners/openai/tests/unit_tests/embeddings/test_base.py +++ b/libs/partners/openai/tests/unit_tests/embeddings/test_base.py @@ -1,4 +1,3 @@ -import os from typing import Any from unittest.mock import Mock, patch @@ -7,8 +6,6 @@ from pydantic import SecretStr from langchain_openai import OpenAIEmbeddings -os.environ["OPENAI_API_KEY"] = "foo" - def test_openai_invalid_model_kwargs() -> None: with pytest.raises(ValueError): diff --git a/libs/partners/openai/tests/unit_tests/llms/test_base.py b/libs/partners/openai/tests/unit_tests/llms/test_base.py index eecfbe4ba1..2663dec098 100644 --- a/libs/partners/openai/tests/unit_tests/llms/test_base.py +++ b/libs/partners/openai/tests/unit_tests/llms/test_base.py @@ -1,13 +1,9 @@ -import os - import pytest from langchain_core.outputs import GenerationChunk from langchain_openai import OpenAI from langchain_openai.llms.base import _stream_response_to_generation_chunk -os.environ["OPENAI_API_KEY"] = "foo" - OPENAI_LLM_TEST_MODEL = "gpt-3.5-turbo-instruct"