Skip to content

fix: restore Azure AD bearer token support in api_key auth#3374

Open
Oxygen56 wants to merge 1 commit into
openai:mainfrom
Oxygen56:fix/azure-ad-bearer-regression-3282
Open

fix: restore Azure AD bearer token support in api_key auth#3374
Oxygen56 wants to merge 1 commit into
openai:mainfrom
Oxygen56:fix/azure-ad-bearer-regression-3282

Conversation

@Oxygen56
Copy link
Copy Markdown

@Oxygen56 Oxygen56 commented Jun 5, 2026

Fixes regression in Azure AD bearer token authentication where AAD tokens
passed via api_key stopped working after v2.34.0.

Problem

In v2.34.0, AzureOpenAI._auth_headers was added as an override that
unconditionally sends api_key as an api-key header. This broke
setups where an Azure AD bearer token was passed through the api_key
parameter, which was the standard approach before azure_ad_token was
introduced.

When using an Azure API Management proxy (or any endpoint expecting
Authorization: Bearer), the token being sent as api-key instead
of Authorization: Bearer results in a 401 error.

Fix

The _auth_headers method in both AzureOpenAI and AsyncAzureOpenAI
now respects the bearer_auth flag from SecurityOptions (which
defaults to True for all requests). When bearer_auth is True and
azure_ad_token is not set, api_key is sent as an
Authorization: Bearer header, restoring the v2.33.0 behavior.

  • azure_ad_token is still sent as Authorization: Bearer (unchanged)
  • api_key with bearer_auth=True is sent as Authorization: Bearer (restored)
  • api_key without bearer_auth is sent as api-key (unchanged)

Fixes #3282

🤖 Generated with Claude Code

When bearer_auth is set in security options (the default for all requests),
the Azure client's _auth_headers now returns Authorization: Bearer using
the api_key value, restoring the v2.33.0 behavior where AAD bearer tokens
passed via api_key worked correctly.

In v2.34.0, an _auth_headers override was added that unconditionally sent
api_key as an api-key header, breaking setups where an Azure AD token was
passed through the api_key parameter (e.g., with Azure API Management proxies).

Fixes openai#3282

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@Oxygen56 Oxygen56 requested a review from a team as a code owner June 5, 2026 18:25
Copy link
Copy Markdown

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

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: 705dc36d7d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/openai/lib/azure.py
Comment on lines +355 to +356
if security.get("bearer_auth", False):
return {"Authorization": f"Bearer {self.api_key}"}
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 Avoid adding bearer auth without suppressing api-key

When an AzureOpenAI client is configured with api_key and calls any operation generated with security={"bearer_auth": True}, _prepare_options() still injects the api-key header into options.headers before _build_headers() merges these auth headers. Returning Authorization here therefore sends both Authorization: Bearer ... and api-key: ..., so the endpoint-level switch to bearer auth is not actually exclusive and can be rejected by Azure services that require a single auth scheme; the async override has the same issue.

Useful? React with 👍 / 👎.

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.

AzureOpenAI with AAD bearer token passed via api_key works in 2.33.0 but returns 401 in 2.34.0 and after

1 participant