Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/openai/lib/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,13 @@ def _get_azure_ad_token(self) -> str | None:
return None

@override
def _auth_headers(self, security: SecurityOptions) -> dict[str, str]: # noqa: ARG002
def _auth_headers(self, security: SecurityOptions) -> dict[str, str]:
if self._azure_ad_token is not None:
return {"Authorization": f"Bearer {self._azure_ad_token}"}

if self.api_key and self.api_key != API_KEY_SENTINEL:
if security.get("bearer_auth", False):
return {"Authorization": f"Bearer {self.api_key}"}
Comment on lines +355 to +356
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 👍 / 👎.

return {"api-key": self.api_key}

return {}
Expand Down Expand Up @@ -669,11 +671,13 @@ async def _get_azure_ad_token(self) -> str | None:
return None

@override
def _auth_headers(self, security: SecurityOptions) -> dict[str, str]: # noqa: ARG002
def _auth_headers(self, security: SecurityOptions) -> dict[str, str]:
if self._azure_ad_token is not None:
return {"Authorization": f"Bearer {self._azure_ad_token}"}

if self.api_key and self.api_key != API_KEY_SENTINEL:
if security.get("bearer_auth", False):
return {"Authorization": f"Bearer {self.api_key}"}
return {"api-key": self.api_key}

return {}
Expand Down