Skip to content

Commit faa73a7

Browse files
committed
fix: retry Google embedding calls on 429 RESOURCE_EXHAUSTED (The-OpenROAD-Project#287)
Add tenacity retry with exponential backoff to FAISSVectorDatabase._add_to_db and process_json to handle transient rate-limit errors from the Google Generative AI / Vertex AI embedding API during backend startup. Promotes tenacity to an explicit direct dependency. Signed-off-by: Jack Luar <jluar@precisioninno.com>
1 parent 48f49d5 commit faa73a7

3 files changed

Lines changed: 2073 additions & 2053 deletions

File tree

backend/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ dependencies = [
4949
"sphinxcontrib-mermaid==1.0.0",
5050
"sqlalchemy>=2.0.43",
5151
"starlette>=1.0.1",
52+
"tenacity>=9.0.0",
5253
"unstructured==0.18.18",
5354
]
5455

backend/src/vectorstores/faiss.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from langchain_google_genai import GoogleGenerativeAIEmbeddings
1010
from langchain_google_vertexai import VertexAIEmbeddings
1111
from langchain_core.documents import Document
12+
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception
1213

1314
from ..tools.process_md import process_md
1415
from ..tools.process_pdf import process_pdf_docs
@@ -71,6 +72,14 @@ def __init__(
7172
def faiss_db(self) -> Optional[FAISS]:
7273
return self._faiss_db
7374

75+
@retry(
76+
stop=stop_after_attempt(5),
77+
wait=wait_exponential(multiplier=2, min=10, max=120),
78+
retry=retry_if_exception(
79+
lambda e: "RESOURCE_EXHAUSTED" in str(e) or "429" in str(e)
80+
),
81+
reraise=True,
82+
)
7483
def _add_to_db(self, documents: list[Document]) -> None:
7584
if self._faiss_db is None:
7685
self._faiss_db = FAISS.from_documents(
@@ -218,6 +227,14 @@ def load_db(self, name: str) -> None:
218227
def get_documents(self) -> list[Document]:
219228
return self._faiss_db.docstore._dict.values() # type: ignore
220229

230+
@retry(
231+
stop=stop_after_attempt(5),
232+
wait=wait_exponential(multiplier=2, min=10, max=120),
233+
retry=retry_if_exception(
234+
lambda e: "RESOURCE_EXHAUSTED" in str(e) or "429" in str(e)
235+
),
236+
reraise=True,
237+
)
221238
def process_json(self, folder_paths: list[str]) -> FAISS:
222239
logging.info("Processing json files...")
223240
if not isinstance(folder_paths, list):

0 commit comments

Comments
 (0)