Skip to content

fix(azure): send JWT api_key values as Bearer token#3283

Open
JiwaniZakir wants to merge 1 commit into
openai:mainfrom
JiwaniZakir:fix/azure-aad-api-key-bearer
Open

fix(azure): send JWT api_key values as Bearer token#3283
JiwaniZakir wants to merge 1 commit into
openai:mainfrom
JiwaniZakir:fix/azure-aad-api-key-bearer

Conversation

@JiwaniZakir
Copy link
Copy Markdown

When an Azure AD access token (JWT) is passed via api_key= to AzureOpenAI, the client now sends it as Authorization: Bearer <token> instead of api-key: <token>. The JWT is detected by checking whether the value starts with "eyJ" (the base64-encoded {" that begins every JWT header). This restores the behavior from v2.33.0 where such tokens worked correctly through Azure APIM/proxy setups that validate Bearer tokens. The same fix is applied to both AzureOpenAI and AsyncAzureOpenAI, in both _auth_headers and _prepare_options.

Fixes #3282

When an Azure AD access token (JWT starting with "eyJ") is passed via
the api_key parameter to AzureOpenAI, send it as Authorization: Bearer
<token> instead of api-key: <token> to restore the behavior from
v2.33.0 and fix 401 errors from Azure APIM/proxy setups.
@JiwaniZakir JiwaniZakir requested a review from a team as a code owner May 20, 2026 10:02
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: 4f8745fa0b

ℹ️ 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 self.api_key.startswith("eyJ"):
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 classifying any eyJ-prefixed key as JWT

This branch treats every api_key starting with eyJ as an Entra bearer token and moves it from api-key to Authorization, but api_key values are opaque and this prefix check is not a reliable JWT validation. In environments that pass non-JWT secrets (for example APIM subscription keys or other opaque keys) that happen to begin with eyJ, requests will now be sent with the wrong auth header and can fail with 401s. The detection should verify JWT structure/content more robustly (or be opt-in) before switching header types.

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