1616from haystack .dataclasses import Document
1717from haystack .document_stores .errors import DocumentStoreError , DuplicateDocumentError
1818from haystack .document_stores .types import DuplicatePolicy
19- from haystack .utils import Secret , deserialize_secrets_inplace
19+ from haystack .utils import Secret
2020from haystack .version import __version__ as haystack_version
2121
2222from elasticsearch import AsyncElasticsearch , Elasticsearch , helpers
@@ -82,8 +82,8 @@ def __init__(
8282 hosts : Hosts | None = None ,
8383 custom_mapping : dict [str , Any ] | None = None ,
8484 index : str = "default" ,
85- api_key : Secret = Secret .from_env_var ("ELASTIC_API_KEY" , strict = False ),
86- api_key_id : Secret = Secret .from_env_var ("ELASTIC_API_KEY_ID" , strict = False ),
85+ api_key : Secret | str | None = Secret .from_env_var ("ELASTIC_API_KEY" , strict = False ),
86+ api_key_id : Secret | str | None = Secret .from_env_var ("ELASTIC_API_KEY_ID" , strict = False ),
8787 embedding_similarity_function : Literal ["cosine" , "dot_product" , "l2_norm" , "max_inner_product" ] = "cosine" ,
8888 ** kwargs : Any ,
8989 ):
@@ -217,8 +217,10 @@ def _handle_auth(self) -> str | tuple[str, str] | None:
217217
218218 api_key : str | tuple [str , str ] | None # make the type checker happy
219219
220- api_key_resolved = self ._api_key .resolve_value ()
221- api_key_id_resolved = self ._api_key_id .resolve_value ()
220+ api_key_resolved = self ._api_key .resolve_value () if isinstance (self ._api_key , Secret ) else self ._api_key
221+ api_key_id_resolved = (
222+ self ._api_key_id .resolve_value () if isinstance (self ._api_key_id , Secret ) else self ._api_key_id
223+ )
222224
223225 # Scenario 1: both are found, use them
224226 if api_key_id_resolved and api_key_resolved :
@@ -271,8 +273,8 @@ def to_dict(self) -> dict[str, Any]:
271273 hosts = self ._hosts ,
272274 custom_mapping = self ._custom_mapping ,
273275 index = self ._index ,
274- api_key = self ._api_key .to_dict (),
275- api_key_id = self ._api_key_id .to_dict (),
276+ api_key = self ._api_key .to_dict () if isinstance ( self . _api_key , Secret ) else None ,
277+ api_key_id = self ._api_key_id .to_dict () if isinstance ( self . _api_key_id , Secret ) else None ,
276278 embedding_similarity_function = self ._embedding_similarity_function ,
277279 ** self ._kwargs ,
278280 )
@@ -287,7 +289,10 @@ def from_dict(cls, data: dict[str, Any]) -> "ElasticsearchDocumentStore":
287289 :returns:
288290 Deserialized component.
289291 """
290- deserialize_secrets_inplace (data , keys = ["api_key" , "api_key_id" ], recursive = True )
292+ if (api_key := data .get ("api_key" )) is not None and isinstance (api_key , dict ):
293+ data ["api_key" ] = Secret .from_dict (api_key )
294+ if (api_key_id := data .get ("api_key_id" )) is not None and isinstance (api_key_id , dict ):
295+ data ["api_key_id" ] = Secret .from_dict (api_key_id )
291296 return default_from_dict (cls , data )
292297
293298 def count_documents (self ) -> int :
0 commit comments