66from datetime import datetime
77from typing import Any
88
9- from azure .core .credentials import AzureKeyCredential
9+ from azure .core .credentials import AzureKeyCredential , TokenCredential
1010from azure .core .exceptions import (
1111 ClientAuthenticationError ,
1212 HttpResponseError ,
@@ -120,6 +120,7 @@ def __init__(
120120 metadata_fields : dict [str , SearchField | type ] | None = None ,
121121 vector_search_configuration : VectorSearch | None = None ,
122122 include_search_metadata : bool = False ,
123+ azure_token_credential : TokenCredential | None = None ,
123124 ** index_creation_kwargs : Any ,
124125 ) -> None :
125126 """
@@ -154,6 +155,8 @@ def __init__(
154155 in the returned documents. When set to True, the `meta` field of the returned
155156 documents will contain the @search.score, @search.reranker_score, @search.highlights,
156157 @search.captions, and other fields returned by Azure AI Search.
158+ :param azure_token_credential: An Azure `TokenCredential` instance used to authenticate requests.
159+ When provided, this takes priority over `api_key`.
157160 :param index_creation_kwargs: Optional keyword parameters to be passed to `SearchIndex` class
158161 during index creation. Some of the supported parameters:
159162 - `semantic_search`: Defines semantic configuration of the search index. This parameter is needed
@@ -175,6 +178,7 @@ def __init__(
175178 self ._metadata_fields = AzureAISearchDocumentStore ._normalize_metadata_index_fields (metadata_fields )
176179 self ._vector_search_configuration = vector_search_configuration or DEFAULT_VECTOR_SEARCH
177180 self ._include_search_metadata = include_search_metadata
181+ self ._azure_token_credential = azure_token_credential
178182 self ._index_creation_kwargs = index_creation_kwargs
179183
180184 @property
@@ -183,7 +187,12 @@ def client(self) -> SearchClient:
183187 resolved_endpoint = self ._azure_endpoint .resolve_value ()
184188 resolved_key = self ._api_key .resolve_value ()
185189
186- credential = AzureKeyCredential (resolved_key ) if resolved_key else DefaultAzureCredential ()
190+ if self ._azure_token_credential is not None :
191+ credential : TokenCredential | AzureKeyCredential = self ._azure_token_credential
192+ elif resolved_key :
193+ credential = AzureKeyCredential (resolved_key )
194+ else :
195+ credential = DefaultAzureCredential ()
187196
188197 # build a UserAgentPolicy to be used for the request
189198 ua_policy = UserAgentPolicy (user_agent = USER_AGENT )
@@ -316,6 +325,13 @@ def to_dict(self) -> dict[str, Any]:
316325 :returns:
317326 Dictionary with serialized data.
318327 """
328+ if self ._azure_token_credential :
329+ logger .warning (
330+ "AzureAISearchDocumentStore was initialized with `azure_token_credential`, "
331+ "which cannot be serialized. It will be excluded from the serialized output "
332+ "and must be provided again when deserializing."
333+ )
334+
319335 return default_to_dict (
320336 self ,
321337 azure_endpoint = (self ._azure_endpoint .to_dict () if self ._azure_endpoint else None ),
0 commit comments