22#
33# SPDX-License-Identifier: Apache-2.0
44
5- from typing import Any , Literal , Optional , Union
5+ from typing import Any , Literal
66
77from google .genai import types
88from haystack import Document , component , default_from_dict , default_to_dict , logging
@@ -75,16 +75,16 @@ def __init__(
7575 * ,
7676 api_key : Secret = Secret .from_env_var (["GOOGLE_API_KEY" , "GEMINI_API_KEY" ], strict = False ),
7777 api : Literal ["gemini" , "vertex" ] = "gemini" ,
78- vertex_ai_project : Optional [ str ] = None ,
79- vertex_ai_location : Optional [ str ] = None ,
78+ vertex_ai_project : str | None = None ,
79+ vertex_ai_location : str | None = None ,
8080 model : str = "text-embedding-004" ,
8181 prefix : str = "" ,
8282 suffix : str = "" ,
8383 batch_size : int = 32 ,
8484 progress_bar : bool = True ,
85- meta_fields_to_embed : Optional [ list [str ]] = None ,
85+ meta_fields_to_embed : list [str ] | None = None ,
8686 embedding_separator : str = "\n " ,
87- config : Optional [ dict [str , Any ]] = None ,
87+ config : dict [str , Any ] | None = None ,
8888 ) -> None :
8989 """
9090 Creates an GoogleGenAIDocumentEmbedder component.
@@ -195,7 +195,7 @@ def _prepare_texts_to_embed(self, documents: list[Document]) -> list[str]:
195195
196196 def _embed_batch (
197197 self , texts_to_embed : list [str ], batch_size : int
198- ) -> tuple [list [Optional [ list [float ]] ], dict [str , Any ]]:
198+ ) -> tuple [list [list [float ] | None ], dict [str , Any ]]:
199199 """
200200 Embed a list of texts in batches.
201201 """
@@ -227,7 +227,7 @@ def _embed_batch(
227227
228228 async def _embed_batch_async (
229229 self , texts_to_embed : list [str ], batch_size : int
230- ) -> tuple [list [Optional [ list [float ]] ], dict [str , Any ]]:
230+ ) -> tuple [list [list [float ] | None ], dict [str , Any ]]:
231231 """
232232 Embed a list of texts in batches asynchronously.
233233 """
@@ -257,7 +257,7 @@ async def _embed_batch_async(
257257 return all_embeddings , meta
258258
259259 @component .output_types (documents = list [Document ], meta = dict [str , Any ])
260- def run (self , documents : list [Document ]) -> Union [ dict [str , list [Document ]], dict [str , Any ] ]:
260+ def run (self , documents : list [Document ]) -> dict [str , list [Document ]] | dict [str , Any ]:
261261 """
262262 Embeds a list of documents.
263263
@@ -281,13 +281,13 @@ def run(self, documents: list[Document]) -> Union[dict[str, list[Document]], dic
281281 meta : dict [str , Any ]
282282 embeddings , meta = self ._embed_batch (texts_to_embed = texts_to_embed , batch_size = self ._batch_size )
283283
284- for doc , emb in zip (documents , embeddings ):
284+ for doc , emb in zip (documents , embeddings , strict = True ):
285285 doc .embedding = emb
286286
287287 return {"documents" : documents , "meta" : meta }
288288
289289 @component .output_types (documents = list [Document ], meta = dict [str , Any ])
290- async def run_async (self , documents : list [Document ]) -> Union [ dict [str , list [Document ]], dict [str , Any ] ]:
290+ async def run_async (self , documents : list [Document ]) -> dict [str , list [Document ]] | dict [str , Any ]:
291291 """
292292 Embeds a list of documents asynchronously.
293293
@@ -310,7 +310,7 @@ async def run_async(self, documents: list[Document]) -> Union[dict[str, list[Doc
310310
311311 embeddings , meta = await self ._embed_batch_async (texts_to_embed = texts_to_embed , batch_size = self ._batch_size )
312312
313- for doc , emb in zip (documents , embeddings ):
313+ for doc , emb in zip (documents , embeddings , strict = True ):
314314 doc .embedding = emb
315315
316316 return {"documents" : documents , "meta" : meta }
0 commit comments