Refresh callable Azure API keys before requests#2992
Refresh callable Azure API keys before requests#2992LittleChenLiya wants to merge 3 commits intoopenai:mainfrom
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1a67dbb199
ℹ️ 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".
src/openai/lib/azure.py
Outdated
|
|
||
| @override | ||
| def _prepare_options(self, options: FinalRequestOptions) -> FinalRequestOptions: | ||
| self._refresh_api_key() |
There was a problem hiding this comment.
Gate API key refresh behind API-key auth path
Calling self._refresh_api_key() unconditionally in Azure _prepare_options() now executes user-provided api_key callables even when the request is authenticated via azure_ad_token/azure_ad_token_provider (the token branch still takes precedence immediately after). This creates a regression for clients that provide both credentials: a failing or slow API-key provider can now break or delay requests that previously succeeded with Azure AD auth; the same pattern is present in the async override as well.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Thanks, this makes sense. I'll update the implementation so callable API keys are only refreshed when the request actually falls back to API-key auth, and I'll add coverage for the Azure AD token path as well.
There was a problem hiding this comment.
Thanks, updated this so callable API keys are only refreshed on the API-key auth path. I also added sync and async regression tests to cover the Azure AD token path and ensure the API key provider is not called when AD auth is used.
Summary
Fix Azure clients so callable
api_keyproviders are refreshed before each request.AzureOpenAIandAsyncAzureOpenAIoverride_prepare_options()to add Azure-specific auth headers, but they were not refreshing callable API keys before setting theapi-keyheader. As a result, dynamic API key providers were not applied to requests or retries.Changes
AzureOpenAI._prepare_options()AsyncAzureOpenAI._prepare_options()api_keyproviders across retriesTesting
python -m pytest tests/lib/test_azure.py -n 0python -m pytest tests/test_client.py -k "api_key_before_after_refresh or api_key_refresh_on_retry or bearer_token_refresh_async" -n 0Closes #2626