-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix AzureOpenAI keyless auth by deferring credential validation to SDK #2464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ppon1086
wants to merge
1
commit into
confident-ai:main
Choose a base branch
from
ppon1086:fix/azure-auth-defer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−17
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -442,33 +442,52 @@ def _client_kwargs(self) -> Dict: | |
| return kwargs | ||
|
|
||
| def _build_client(self, cls): | ||
| # Only require the API key / Azure ad token if no token provider is supplied | ||
| azure_ad_token = None | ||
| api_key = None | ||
|
|
||
| # Defer authentication validation to the OpenAI SDK. | ||
| # Only fail fast if the user explicitly provided an empty credential. | ||
|
|
||
| api_key_value = None | ||
| if self.api_key is not None: | ||
| try: | ||
| api_key_value = self.api_key.get_secret_value() | ||
| except Exception: | ||
| api_key_value = str(self.api_key) | ||
|
|
||
| azure_ad_token_value = None | ||
| if self.azure_ad_token is not None: | ||
| try: | ||
| azure_ad_token_value = self.azure_ad_token.get_secret_value() | ||
| except Exception: | ||
| azure_ad_token_value = str(self.azure_ad_token) | ||
|
|
||
| if self.azure_ad_token_provider is None: | ||
| if self.azure_ad_token is not None: | ||
| azure_ad_token = require_secret_api_key( | ||
| self.azure_ad_token, | ||
| provider_label="AzureOpenAI", | ||
| env_var_name="AZURE_OPENAI_AD_TOKEN", | ||
| param_hint="`azure_ad_token` to AzureOpenAIModel(...)", | ||
| if ( | ||
| azure_ad_token_value is not None | ||
| and isinstance(azure_ad_token_value, str) | ||
| and not azure_ad_token_value.strip() | ||
| ): | ||
| raise DeepEvalError( | ||
| "azure_ad_token was provided but is empty. Omit it to defer auth to the OpenAI SDK." | ||
| ) | ||
| else: | ||
| api_key = require_secret_api_key( | ||
| self.api_key, | ||
| provider_label="AzureOpenAI", | ||
| env_var_name="AZURE_OPENAI_API_KEY", | ||
| param_hint="`api_key` to AzureOpenAIModel(...)", | ||
|
|
||
| if ( | ||
| api_key_value is not None | ||
| and isinstance(api_key_value, str) | ||
| and not api_key_value.strip() | ||
| ): | ||
| raise DeepEvalError( | ||
| "api_key was provided but is empty. Omit it to defer auth to the OpenAI SDK." | ||
| ) | ||
| # else: neither key nor token nor provider set -> defer to SDK | ||
|
|
||
|
|
||
| kw = dict( | ||
| api_key=api_key, | ||
| api_key=api_key_value, | ||
| api_version=self.api_version, | ||
| azure_endpoint=self.base_url, | ||
| azure_deployment=self.deployment_name, | ||
| azure_ad_token_provider=self.azure_ad_token_provider, | ||
| azure_ad_token=azure_ad_token, | ||
| azure_ad_token=self.azure_ad_token_value, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason we're using |
||
| **self._client_kwargs(), | ||
| ) | ||
| try: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this block we're following a precedence, we're giving priority to
azure_ad_token_providerfirst, if that doesn't exist we give priority toazure_ad_tokenoverapi_key. This allows us to handle the case where a user provides all 3 of these fields (azure_ad_token_provider > azure_ad_token > api_key), since we're already populatingapi_key_valueandazure_ad_token_valuewhich are all being passed to the client below, I'm worried about which one is used by the client in this case? Do you know what happens in this case and if it's safe to do this?