From 8ef35259f181995ef0d6b2f5cf8ee41c3c74a4ef Mon Sep 17 00:00:00 2001 From: Dev Sharma <12devsharma10c@gmail.com> Date: Wed, 6 Aug 2025 00:44:32 +0530 Subject: [PATCH] Improve Handling for Read Timeouts with Clear Errors & Retry Support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR improves the SDK’s behavior when a read timeout occurs during API calls. --- src/openai/_base_client.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/openai/_base_client.py b/src/openai/_base_client.py index 3fe669259f..89cb7af28a 100644 --- a/src/openai/_base_client.py +++ b/src/openai/_base_client.py @@ -1,4 +1,6 @@ from __future__ import annotations +# openai/_exceptions.py + import sys import json @@ -67,6 +69,29 @@ AsyncAPIResponse, extract_response_type, ) +# openai/_exceptions.py +class ReadTimeoutError(OpenAIError): + """Raised when the server takes too long to send a response.""" + pass +import httpx +from ._exceptions import ReadTimeoutError + +class BaseClient: + def _request(self, method, path, **kwargs): + try: + return self._http_client.request(method, path, **kwargs) + except httpx.ReadTimeout as e: + raise ReadTimeoutError( + f"Read timeout occurred while waiting for response from {path}. " + f"Consider increasing the timeout or using retries." + ) from e +class BaseClient: + def __init__(self, *, timeout: float = 30.0, max_retries: int = 0, **kwargs): + self._timeout = timeout + self._max_retries = max_retries + super().__init__(**kwargs) +self._http_client = httpx.Client(timeout=self._timeout) + from ._constants import ( DEFAULT_TIMEOUT, MAX_RETRY_DELAY,