feat: raise error when timeout in api call (#793)

* small change to try catch in backend

* fix CI

* add timeout tolerance to 3
This commit is contained in:
Xu Yang
2025-04-15 23:50:20 -06:00
committed by GitHub
parent e05a645ad4
commit 829f5534ca
+22
View File
@@ -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))