88
99from azure .core .credentials import AzureKeyCredential
1010from azure .core .exceptions import ClientAuthenticationError , HttpResponseError , ResourceNotFoundError
11+ from azure .core .pipeline .policies import UserAgentPolicy
1112from azure .identity import DefaultAzureCredential
1213from azure .search .documents import SearchClient
1314from azure .search .documents .indexes import SearchIndexClient
4142from .errors import AzureAISearchDocumentStoreConfigError
4243from .filters import _normalize_filters
4344
45+ USER_AGENT = "haystack-integrations/azure-ai-search"
46+
4447type_mapping = {
4548 str : "Edm.String" ,
4649 bool : "Edm.Boolean" ,
@@ -150,11 +153,15 @@ def client(self) -> SearchClient:
150153 resolved_key = self ._api_key .resolve_value () if isinstance (self ._api_key , Secret ) else self ._api_key
151154
152155 credential = AzureKeyCredential (resolved_key ) if resolved_key else DefaultAzureCredential ()
156+
157+ # build a UserAgentPolicy to be used for the request
158+ ua_policy = UserAgentPolicy (user_agent = USER_AGENT )
153159 try :
154160 if not self ._index_client :
155161 self ._index_client = SearchIndexClient (
156162 resolved_endpoint ,
157163 credential ,
164+ user_agent = ua_policy ,
158165 )
159166 if not self ._index_exists (self ._index_name ):
160167 # Create a new index if it does not exist
@@ -171,7 +178,7 @@ def client(self) -> SearchClient:
171178 # Get the search client, if index client is initialized
172179 index_fields = self ._index_client .get_index (self ._index_name ).fields
173180 self ._index_fields = [field .name for field in index_fields ]
174- self ._client = self ._index_client .get_search_client (self ._index_name )
181+ self ._client = self ._index_client .get_search_client (self ._index_name , user_agent = ua_policy )
175182 else :
176183 msg = "Search Index Client is not initialized."
177184 raise AzureAISearchDocumentStoreConfigError (msg )
0 commit comments