From 829f5534ca9ffdc660bb5de6b7953fec139db1a8 Mon Sep 17 00:00:00 2001 From: Xu Yang Date: Tue, 15 Apr 2025 23:50:20 -0600 Subject: [PATCH] feat: raise error when timeout in api call (#793) * small change to try catch in backend * fix CI * add timeout tolerance to 3 --- rdagent/oai/backend/base.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/rdagent/oai/backend/base.py b/rdagent/oai/backend/base.py index 72d89a78..659592c3 100644 --- a/rdagent/oai/backend/base.py +++ b/rdagent/oai/backend/base.py @@ -18,6 +18,13 @@ from rdagent.log import rdagent_logger as logger from rdagent.oai.llm_conf import LLM_SETTINGS from rdagent.utils import md5_hash +try: + import openai + + openai_imported = True +except ImportError: + openai_imported = False + class SQliteLazyCache(SingletonBaseClass): def __init__(self, cache_location: str) -> None: @@ -321,6 +328,7 @@ class APIBackend(ABC): ) -> str | list[list[float]]: assert not (chat_completion and embedding), "chat_completion and embedding cannot be True at the same time" max_retry = LLM_SETTINGS.max_retry if LLM_SETTINGS.max_retry is not None else max_retry + timeout_count = 0 for i in range(max_retry): try: if embedding: @@ -337,6 +345,20 @@ class APIBackend(ABC): kwargs["input_content_list"] = [ content[: len(content) // 2] for content in kwargs.get("input_content_list", []) ] + elif ( + openai_imported + and isinstance(e, openai.APITimeoutError) + or ( + isinstance(e, openai.APIError) + and hasattr(e, "message") + and "Your resource has been temporarily blocked because we detected behavior that may violate our content policy." + in e.message + ) + ): + timeout_count += 1 + if timeout_count >= 3: + logger.warning("Timeout error, please check your network connection.") + raise e else: time.sleep(self.retry_wait_seconds) logger.warning(str(e))