Skip to content

fix(azure): resolve one authentication mode - #3689

Merged
apcha-oai merged 4 commits into
mainfrom
apcha/azure-auth-mode
Aug 19, 2026
Merged

fix(azure): resolve one authentication mode#3689
apcha-oai merged 4 commits into
mainfrom
apcha/azure-auth-mode

Conversation

@apcha-oai

@apcha-oai apcha-oai commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Resolve one Azure authentication mode consistently for synchronous and asynchronous clients. Explicit credentials take precedence over credential environment variables; conflicting explicit modes raise the existing MutuallyExclusiveAuthError.
  • Preserve environment-only AD-token precedence, client-copy behavior, and callable API-key refresh.
  • Keep module-level explicit Azure configuration separate from environment fallback, including import-time configuration and automatic Azure selection.
  • Add focused regressions for explicit and ambient credential selection, client copies, HTTP and Realtime configuration, and callable keys.

No dependency or exported client API changes. This is independent of the Azure redirect-transport changes in #3684.

@apcha-oai
apcha-oai requested a review from a team as a code owner August 19, 2026 21:21
@openai-sdks

openai-sdks Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

OkTest Summary

237/237 SDK tests passed in 9.565s for Python SDK PR #3689.

Test results — 42 files
Test Result Time
tests/chat-completions-complex-body.test.ts ✅ Passed 218ms
tests/chat-completions-create.test.ts ✅ Passed 200ms
tests/chat-completions-stream.test.ts ✅ Passed 95ms
tests/files-content-binary.test.ts ✅ Passed 300ms
tests/files-create-multipart.test.ts ✅ Passed 316ms
tests/files-list-pagination.test.ts ✅ Passed 126ms
tests/initialize-config.test.ts ✅ Passed 120ms
tests/instance-isolation.test.ts ✅ Passed 151ms
tests/models-list.test.ts ✅ Passed 143ms
tests/responses-background-lifecycle.test.ts ✅ Passed 171ms
tests/responses-body-method-errors.test.ts ✅ Passed 267ms
tests/responses-cancel-timeout.test.ts ✅ Passed 196ms
tests/responses-cancel.test.ts ✅ Passed 202ms
tests/responses-compact-retries.test.ts ✅ Passed 217ms
tests/responses-compact.test.ts ✅ Passed 260ms
tests/responses-create-advanced-stream.test.ts ✅ Passed 235ms
tests/responses-create-advanced.test.ts ✅ Passed 175ms
tests/responses-create-disconnect.test.ts ✅ Passed 1.181s
tests/responses-create-errors.test.ts ✅ Passed 149ms
tests/responses-create-malformed-api-responses.test.ts ✅ Passed 188ms
tests/responses-create-retries.test.ts ✅ Passed 300ms
tests/responses-create-stream-failures.test.ts ✅ Passed 98ms
tests/responses-create-stream-timeout.test.ts ✅ Passed 219ms
tests/responses-create-stream-wire.test.ts ✅ Passed 2.896s
tests/responses-create-stream.test.ts ✅ Passed 63ms
tests/responses-create-terminal-states.test.ts ✅ Passed 157ms
tests/responses-create-timeout.test.ts ✅ Passed 189ms
tests/responses-create.test.ts ✅ Passed 388ms
tests/responses-delete.test.ts ✅ Passed 166ms
tests/responses-input-items-errors.test.ts ✅ Passed 342ms
tests/responses-input-items-list.test.ts ✅ Passed 239ms
tests/responses-input-items-options.test.ts ✅ Passed 229ms
tests/responses-input-tokens-count-timeout.test.ts ✅ Passed 223ms
tests/responses-input-tokens-count.test.ts ✅ Passed 172ms
tests/responses-malformed-inputs.test.ts ✅ Passed 2.302s
tests/responses-not-found-errors.test.ts ✅ Passed 216ms
tests/responses-parse.test.ts ✅ Passed 291ms
tests/responses-retrieve-retries.test.ts ✅ Passed 200ms
tests/responses-retrieve.test.ts ✅ Passed 155ms
tests/responses-stored-method-errors.test.ts ✅ Passed 601ms
tests/retry-behavior.test.ts ✅ Passed 2.988s
tests/sdk-error-shape.test.ts ✅ Passed 247ms

View OkTest run #32307569437

SDK merge (f357c65f27ce) · head (655a9a4194cf) · base (f36e6f79662b) · OkTest (2b1bdfd25e98)

Comment thread tests/lib/test_azure_auth.py Fixed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f89264f3ee

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/openai/lib/azure.py
Comment on lines +79 to +81
api_key is not None and api_key != API_KEY_SENTINEL,
azure_ad_token is not None,
azure_ad_token_provider is not None,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve explicit auth precedence in the module client

When the module-level client is forced to Azure, _load_client() materializes AZURE_OPENAI_AD_TOKEN into azure_ad_token before passing it to _AzureModuleClient; therefore an explicit openai.api_key or openai.azure_ad_token_provider is counted here alongside that ambient token and raises MutuallyExclusiveAuthError. For example, openai.api_type = "azure" with an explicit API key and an existing token environment variable now fails instead of honoring the explicit credential as the direct client does. Preserve whether the token came from the environment in this path and add a focused module-client authentication regression.

AGENTS.md reference: AGENTS.md:L31-L35

Useful? React with 👍 / 👎.

Comment on lines +248 to +262
"import os\n"
"from unittest.mock import patch\n"
"with patch.dict(os.environ, {\n"
" 'AZURE_OPENAI_AD_TOKEN': 'fake-ambient-token',\n"
" 'OPENAI_API_TYPE': 'azure',\n"
" 'OPENAI_API_VERSION': '2024-02-01',\n"
" 'AZURE_OPENAI_ENDPOINT': 'https://azure.test',\n"
"}, clear=True):\n"
" import openai\n"
" assert openai.azure_ad_token is None\n"
" openai.api_key = 'fake-explicit-key'\n"
" client = openai.models._client\n"
" assert client._azure_ad_token is None\n"
" assert client.api_key == 'fake-explicit-key'\n"
" client.close()\n",

@jbeckwith-oai jbeckwith-oai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent two-pass review confirms Azure authentication-mode precedence, sync/async providers, module-client behavior, copies, and CI are sound. One small required cleanup remains: the newly introduced B018 suppressions are unnecessary and conflict with the repository instruction to prefer straightforward lint-compliant tests.

Comment thread tests/test_module_client.py Outdated
openai.api_key = "fake-explicit-key"
openai.azure_ad_token_provider = lambda: "fake-explicit-token"
with pytest.raises(MutuallyExclusiveAuthError):
openai.models._client # noqa: B018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Low] Remove the two avoidable B018 suppressions. This new line and line 274 both use # noqa: B018, although _ = openai.models._client evaluates the same lazy property, preserves the surrounding exception assertion, and passes Ruff without suppression. Please replace both bare accesses with that compliant form and remove both directives.

@apcha-oai
apcha-oai requested review from a team and jbeckwith-oai August 19, 2026 22:10

@jbeckwith-oai jbeckwith-oai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent two-pass re-review confirms both requested B018 suppressions were removed with the straightforward _ = openai.models._client form, and the follow-up changes only those two test lines. Azure synchronous/asynchronous credential precedence, environment fallback, provider refresh, module configuration, copies, and Realtime authentication remain sound. CI and CodeQL are green; prior concerns are resolved.

@apcha-oai
apcha-oai added this pull request to the merge queue Aug 19, 2026
Merged via the queue into main with commit e3d0681 Aug 19, 2026
21 checks passed
@apcha-oai
apcha-oai deleted the apcha/azure-auth-mode branch August 19, 2026 22:22
@openai-sdks openai-sdks Bot mentioned this pull request Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants