langchain-ai / langchain-google

MIT License
74 stars 78 forks source link

[genai] GoogleGenerativeAIEmbeddings.embed_documents fails for large number of documents #254

Closed adamvig96 closed 1 month ago

adamvig96 commented 1 month ago

GoogleGenerativeAIEmbeddings.embed_documents fails for a large number of documents as batch size is capped at 100 in the API and it's not handled in the google SDK.

Sample code to reproduce error:

from langchain_google_genai import GoogleGenerativeAIEmbeddings

texts = ["hi", "hello", "How are you"] * 100
embedding = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004")
embedding.embed_documents(texts)

Error:

Traceback (most recent call last)
[/usr/local/lib/python3.10/dist-packages/langchain_google_genai/embeddings.py](https://localhost:8080/#) in embed_documents(self, texts, task_type, titles, output_dimensionality)
    145         try:
--> 146             result = self.client.batch_embed_contents(
    147                 BatchEmbedContentsRequest(requests=requests, model=self.model)

9 frames
[/usr/local/lib/python3.10/dist-packages/google/ai/generativelanguage_v1beta/services/generative_service/client.py](https://localhost:8080/#) in batch_embed_contents(self, request, model, requests, retry, timeout, metadata)
   1349         # Send the request.
-> 1350         response = rpc(
   1351             request,

[/usr/local/lib/python3.10/dist-packages/google/api_core/gapic_v1/method.py](https://localhost:8080/#) in __call__(self, timeout, retry, compression, *args, **kwargs)
    130 
--> 131         return wrapped_func(*args, **kwargs)
    132 

[/usr/local/lib/python3.10/dist-packages/google/api_core/retry/retry_unary.py](https://localhost:8080/#) in retry_wrapped_func(*args, **kwargs)
    292             )
--> 293             return retry_target(
    294                 target,

[/usr/local/lib/python3.10/dist-packages/google/api_core/retry/retry_unary.py](https://localhost:8080/#) in retry_target(target, predicate, sleep_generator, timeout, on_error, exception_factory, **kwargs)
    152             # defer to shared logic for handling errors
--> 153             _retry_error_helper(
    154                 exc,

[/usr/local/lib/python3.10/dist-packages/google/api_core/retry/retry_base.py](https://localhost:8080/#) in _retry_error_helper(exc, deadline, next_sleep, error_list, predicate_fn, on_error_fn, exc_factory_fn, original_timeout)
    211         )
--> 212         raise final_exc from source_exc
    213     if on_error_fn is not None:

[/usr/local/lib/python3.10/dist-packages/google/api_core/retry/retry_unary.py](https://localhost:8080/#) in retry_target(target, predicate, sleep_generator, timeout, on_error, exception_factory, **kwargs)
    143         try:
--> 144             result = target()
    145             if inspect.isawaitable(result):

[/usr/local/lib/python3.10/dist-packages/google/api_core/timeout.py](https://localhost:8080/#) in func_with_timeout(*args, **kwargs)
    119 
--> 120             return func(*args, **kwargs)
    121 

[/usr/local/lib/python3.10/dist-packages/google/api_core/grpc_helpers.py](https://localhost:8080/#) in error_remapped_callable(*args, **kwargs)
     77         except grpc.RpcError as exc:
---> 78             raise exceptions.from_grpc_error(exc) from exc
     79 

InvalidArgument: 400 * BatchEmbedContentsRequest.requests: at most 100 requests can be in one batch

The above exception was the direct cause of the following exception:

GoogleGenerativeAIError                   Traceback (most recent call last)
[<ipython-input-78-300242ff8d26>](https://localhost:8080/#) in <cell line: 3>()
      1 texts = ["hi", "hello", "How are you"] * 100
      2 embedding = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004")
----> 3 embedding.embed_documents(texts)

[/usr/local/lib/python3.10/dist-packages/langchain_google_genai/embeddings.py](https://localhost:8080/#) in embed_documents(self, texts, task_type, titles, output_dimensionality)
    148             )
    149         except Exception as e:
--> 150             raise GoogleGenerativeAIError(f"Error embedding content: {e}") from e
    151         return [e.values for e in result.embeddings]
    152 

GoogleGenerativeAIError: Error embedding content: 400 * BatchEmbedContentsRequest.requests: at most 100 requests can be in one batch