Skip to content

The agent engineering platform.

Dedup methods (#8049)

William FHdc9d6cadab88
diff --git a/langchain/chat_models/openai.py b/langchain/chat_models/openai.py
index fbc888111e..28d59182c1 100644
--- a/langchain/chat_models/openai.py
+++ b/langchain/chat_models/openai.py
@@ -289,30 +289,9 @@ class ChatOpenAI(BaseChatModel):
**self.model_kwargs,
}
- def _create_retry_decorator(self) -> Callable[[Any], Any]:
- import openai
-
- min_seconds = 1
- max_seconds = 60
- # Wait 2^x * 1 second between each retry starting with
- # 4 seconds, then up to 10 seconds, then 10 seconds afterwards
- return retry(
- reraise=True,
- stop=stop_after_attempt(self.max_retries),
- wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds),
- retry=(
- retry_if_exception_type(openai.error.Timeout)
- | retry_if_exception_type(openai.error.APIError)
- | retry_if_exception_type(openai.error.APIConnectionError)
- | retry_if_exception_type(openai.error.RateLimitError)
- | retry_if_exception_type(openai.error.ServiceUnavailableError)
- ),
- before_sleep=before_sleep_log(logger, logging.WARNING),
- )
-
def completion_with_retry(self, **kwargs: Any) -> Any:
"""Use tenacity to retry the completion call."""
- retry_decorator = self._create_retry_decorator()
+ retry_decorator = _create_retry_decorator(self)
@retry_decorator
def _completion_with_retry(**kwargs: Any) -> Any: