44
55import sys
66from collections .abc import Mapping
7- from typing import TYPE_CHECKING , Any , ClassVar , Generic
7+ from typing import Any , ClassVar , Generic
88
9- from openai .lib .azure import AsyncAzureADTokenProvider , AsyncAzureOpenAI
9+ from openai .lib .azure import AsyncAzureOpenAI
1010
1111from .._settings import load_settings
1212from ..exceptions import ServiceInitializationError
1313from ..openai import OpenAIAssistantsClient
1414from ..openai ._assistants_client import OpenAIAssistantsOptions
15- from ._entra_id_authentication import get_entra_auth_token
16- from ._shared import DEFAULT_AZURE_TOKEN_ENDPOINT , AzureOpenAISettings , _apply_azure_defaults
17-
18- if TYPE_CHECKING :
19- from azure .core .credentials import TokenCredential
15+ from ._entra_id_authentication import AzureCredentialTypes , AzureTokenProvider , resolve_credential_to_token_provider
16+ from ._shared import AzureOpenAISettings , _apply_azure_defaults
2017
2118if sys .version_info >= (3 , 13 ):
2219 from typing import TypeVar # type: ignore # pragma: no cover
@@ -61,10 +58,8 @@ def __init__(
6158 endpoint : str | None = None ,
6259 base_url : str | None = None ,
6360 api_version : str | None = None ,
64- ad_token : str | None = None ,
65- ad_token_provider : AsyncAzureADTokenProvider | None = None ,
6661 token_endpoint : str | None = None ,
67- credential : TokenCredential | None = None ,
62+ credential : AzureCredentialTypes | AzureTokenProvider | None = None ,
6863 default_headers : Mapping [str , str ] | None = None ,
6964 async_client : AsyncAzureOpenAI | None = None ,
7065 env_file_path : str | None = None ,
@@ -93,11 +88,12 @@ def __init__(
9388 api_version: The deployment API version. If provided will override the value
9489 in the env vars or .env file.
9590 Can also be set via environment variable AZURE_OPENAI_API_VERSION.
96- ad_token: The Azure Active Directory token.
97- ad_token_provider: The Azure Active Directory token provider.
9891 token_endpoint: The token endpoint to request an Azure token.
9992 Can also be set via environment variable AZURE_OPENAI_TOKEN_ENDPOINT.
100- credential: The Azure credential to use for authentication.
93+ credential: Azure credential or token provider for authentication. Accepts a
94+ ``TokenCredential``, ``AsyncTokenCredential``, or a callable that returns a
95+ bearer token string (sync or async), for example from
96+ ``azure.identity.get_bearer_token_provider()``.
10197 default_headers: The default headers mapping of string keys to
10298 string values for HTTP requests.
10399 async_client: An existing client to use.
@@ -156,20 +152,15 @@ class MyOptions(AzureOpenAIAssistantsOptions, total=False):
156152 "or 'AZURE_OPENAI_CHAT_DEPLOYMENT_NAME' environment variable."
157153 )
158154
159- # Handle authentication: try API key first, then AD token, then Entra ID
160- if (
161- not async_client
162- and not azure_openai_settings ["api_key" ]
163- and not ad_token
164- and not ad_token_provider
165- and azure_openai_settings ["token_endpoint" ]
166- and credential
167- ):
168- token_ep = azure_openai_settings ["token_endpoint" ] or DEFAULT_AZURE_TOKEN_ENDPOINT
169- ad_token = get_entra_auth_token (credential , token_ep )
170-
171- if not async_client and not azure_openai_settings ["api_key" ] and not ad_token and not ad_token_provider :
172- raise ServiceInitializationError ("The Azure OpenAI API key, ad_token, or ad_token_provider is required." )
155+ # Resolve credential to token provider
156+ ad_token_provider = None
157+ if not async_client and not azure_openai_settings ["api_key" ] and credential :
158+ ad_token_provider = resolve_credential_to_token_provider (
159+ credential , azure_openai_settings ["token_endpoint" ]
160+ )
161+
162+ if not async_client and not azure_openai_settings ["api_key" ] and not ad_token_provider :
163+ raise ServiceInitializationError ("Please provide either api_key, credential, or a client." )
173164
174165 # Create Azure client if not provided
175166 if not async_client :
@@ -180,8 +171,6 @@ class MyOptions(AzureOpenAIAssistantsOptions, total=False):
180171
181172 if azure_openai_settings ["api_key" ]:
182173 client_params ["api_key" ] = azure_openai_settings ["api_key" ].get_secret_value ()
183- elif ad_token :
184- client_params ["azure_ad_token" ] = ad_token
185174 elif ad_token_provider :
186175 client_params ["azure_ad_token_provider" ] = ad_token_provider
187176
0 commit comments