From 5e0daf635134ca03f4e69d2048b01e8719aac42f Mon Sep 17 00:00:00 2001 From: Junhyuk Lee Date: Thu, 30 Apr 2026 07:43:04 +0000 Subject: [PATCH 1/2] Advance OSS contribution for The callable api_key feature seems to be broken for Azure OpenAI clients Nightly Codex produced a focused contribution for https://github.com/openai/openai-python/issues/2626. Constraint: Automated nightly run; keep changes small and reviewable. Confidence: medium Scope-risk: narrow Tested: See uploaded nightly artifacts and workflow logs. Not-tested: Maintainer CI beyond this workflow. --- NIGHTLY_CODEX_FINAL_ATTEMPT_1.md | 10 ++++ src/openai/lib/azure.py | 4 ++ tests/lib/test_azure.py | 78 ++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 NIGHTLY_CODEX_FINAL_ATTEMPT_1.md diff --git a/NIGHTLY_CODEX_FINAL_ATTEMPT_1.md b/NIGHTLY_CODEX_FINAL_ATTEMPT_1.md new file mode 100644 index 0000000000..7581e028f4 --- /dev/null +++ b/NIGHTLY_CODEX_FINAL_ATTEMPT_1.md @@ -0,0 +1,10 @@ +Implemented a small fix for issue #2626: Azure sync/async clients now refresh callable `api_key` providers before preparing Azure auth headers in [src/openai/lib/azure.py](/home/runner/work/oss-nightly-control/oss-nightly-control/target/src/openai/lib/azure.py:327). + +Added sync and async regression coverage in [tests/lib/test_azure.py](/home/runner/work/oss-nightly-control/oss-nightly-control/target/tests/lib/test_azure.py:118), verifying callable API keys are used on the initial request and refreshed on retry. Wrote the required [NIGHTLY_REPORT.md](/home/runner/work/oss-nightly-control/oss-nightly-control/target/NIGHTLY_REPORT.md:1). + +Verification passed: +- `.venv/bin/python -m pytest tests/lib/test_azure.py` +- `.venv/bin/python -m ruff check src/openai/lib/azure.py tests/lib/test_azure.py` +- `.venv/bin/python -m ruff format --check src/openai/lib/azure.py tests/lib/test_azure.py` + +No commit, push, or PR was created. Untracked `.codex-nightly-prompt.md` and `uv.lock` were already present and left untouched. \ No newline at end of file diff --git a/src/openai/lib/azure.py b/src/openai/lib/azure.py index 09fdd9507e..13302dbeb9 100644 --- a/src/openai/lib/azure.py +++ b/src/openai/lib/azure.py @@ -325,6 +325,8 @@ def _get_azure_ad_token(self) -> str | None: @override def _prepare_options(self, options: FinalRequestOptions) -> FinalRequestOptions: + self._refresh_api_key() + headers: dict[str, str | Omit] = {**options.headers} if is_given(options.headers) else {} options = model_copy(options) @@ -612,6 +614,8 @@ async def _get_azure_ad_token(self) -> str | None: @override async def _prepare_options(self, options: FinalRequestOptions) -> FinalRequestOptions: + await self._refresh_api_key() + headers: dict[str, str | Omit] = {**options.headers} if is_given(options.headers) else {} options = model_copy(options) diff --git a/tests/lib/test_azure.py b/tests/lib/test_azure.py index 52c24eba27..4b7ddf036e 100644 --- a/tests/lib/test_azure.py +++ b/tests/lib/test_azure.py @@ -114,6 +114,44 @@ def token_provider() -> str: assert calls[1].request.headers.get("Authorization") == "Bearer second" +@pytest.mark.respx() +def test_client_api_key_provider_refresh_sync(respx_mock: MockRouter) -> None: + respx_mock.post( + "https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-01" + ).mock( + side_effect=[ + httpx.Response(500, json={"error": "server error"}), + httpx.Response(200, json={"foo": "bar"}), + ] + ) + + counter = 0 + + def api_key_provider() -> str: + nonlocal counter + + counter += 1 + + if counter == 1: + return "first" + + return "second" + + client = AzureOpenAI( + api_version="2024-02-01", + api_key=api_key_provider, + azure_endpoint="https://example-resource.azure.openai.com", + ) + client.chat.completions.create(messages=[], model="gpt-4") + + calls = cast("list[MockRequestCall]", respx_mock.calls) + + assert len(calls) == 2 + + assert calls[0].request.headers.get("api-key") == "first" + assert calls[1].request.headers.get("api-key") == "second" + + @pytest.mark.asyncio @pytest.mark.respx() async def test_client_token_provider_refresh_async(respx_mock: MockRouter) -> None: @@ -154,6 +192,46 @@ def token_provider() -> str: assert calls[1].request.headers.get("Authorization") == "Bearer second" +@pytest.mark.asyncio +@pytest.mark.respx() +async def test_client_api_key_provider_refresh_async(respx_mock: MockRouter) -> None: + respx_mock.post( + "https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-01" + ).mock( + side_effect=[ + httpx.Response(500, json={"error": "server error"}), + httpx.Response(200, json={"foo": "bar"}), + ] + ) + + counter = 0 + + async def api_key_provider() -> str: + nonlocal counter + + counter += 1 + + if counter == 1: + return "first" + + return "second" + + client = AsyncAzureOpenAI( + api_version="2024-02-01", + api_key=api_key_provider, + azure_endpoint="https://example-resource.azure.openai.com", + ) + + await client.chat.completions.create(messages=[], model="gpt-4") + + calls = cast("list[MockRequestCall]", respx_mock.calls) + + assert len(calls) == 2 + + assert calls[0].request.headers.get("api-key") == "first" + assert calls[1].request.headers.get("api-key") == "second" + + class TestAzureLogging: @pytest.fixture(autouse=True) def logger_with_filter(self) -> logging.Logger: From 5070ffec45ec955913d8ec1b5fe7b701d324af2d Mon Sep 17 00:00:00 2001 From: Junhyuk Lee <58055473+xodn348@users.noreply.github.com> Date: Thu, 30 Apr 2026 11:45:52 -0500 Subject: [PATCH 2/2] Remove nightly runner artifact from PR branch --- NIGHTLY_CODEX_FINAL_ATTEMPT_1.md | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 NIGHTLY_CODEX_FINAL_ATTEMPT_1.md diff --git a/NIGHTLY_CODEX_FINAL_ATTEMPT_1.md b/NIGHTLY_CODEX_FINAL_ATTEMPT_1.md deleted file mode 100644 index 7581e028f4..0000000000 --- a/NIGHTLY_CODEX_FINAL_ATTEMPT_1.md +++ /dev/null @@ -1,10 +0,0 @@ -Implemented a small fix for issue #2626: Azure sync/async clients now refresh callable `api_key` providers before preparing Azure auth headers in [src/openai/lib/azure.py](/home/runner/work/oss-nightly-control/oss-nightly-control/target/src/openai/lib/azure.py:327). - -Added sync and async regression coverage in [tests/lib/test_azure.py](/home/runner/work/oss-nightly-control/oss-nightly-control/target/tests/lib/test_azure.py:118), verifying callable API keys are used on the initial request and refreshed on retry. Wrote the required [NIGHTLY_REPORT.md](/home/runner/work/oss-nightly-control/oss-nightly-control/target/NIGHTLY_REPORT.md:1). - -Verification passed: -- `.venv/bin/python -m pytest tests/lib/test_azure.py` -- `.venv/bin/python -m ruff check src/openai/lib/azure.py tests/lib/test_azure.py` -- `.venv/bin/python -m ruff format --check src/openai/lib/azure.py tests/lib/test_azure.py` - -No commit, push, or PR was created. Untracked `.codex-nightly-prompt.md` and `uv.lock` were already present and left untouched. \ No newline at end of file