Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cf9a651

Browse files
authoredJan 10, 2024
chore(client): improve debug logging for failed requests (#1060)
1 parent fa931da commit cf9a651

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
 

‎src/openai/_base_client.py

+35
Original file line numberDiff line numberDiff line change
@@ -646,26 +646,33 @@ def _should_retry(self, response: httpx.Response) -> bool:
646646

647647
# If the server explicitly says whether or not to retry, obey.
648648
if should_retry_header == "true":
649+
log.debug("Retrying as header `x-should-retry` is set to `true`")
649650
return True
650651
if should_retry_header == "false":
652+
log.debug("Not retrying as header `x-should-retry` is set to `false`")
651653
return False
652654

653655
# Retry on request timeouts.
654656
if response.status_code == 408:
657+
log.debug("Retrying due to status code %i", response.status_code)
655658
return True
656659

657660
# Retry on lock timeouts.
658661
if response.status_code == 409:
662+
log.debug("Retrying due to status code %i", response.status_code)
659663
return True
660664

661665
# Retry on rate limits.
662666
if response.status_code == 429:
667+
log.debug("Retrying due to status code %i", response.status_code)
663668
return True
664669

665670
# Retry internal errors.
666671
if response.status_code >= 500:
672+
log.debug("Retrying due to status code %i", response.status_code)
667673
return True
668674

675+
log.debug("Not retrying")
669676
return False
670677

671678
def _idempotency_key(self) -> str:
@@ -883,6 +890,8 @@ def _request(
883890
**kwargs,
884891
)
885892
except httpx.TimeoutException as err:
893+
log.debug("Encountered httpx.TimeoutException", exc_info=True)
894+
886895
if retries > 0:
887896
return self._retry_request(
888897
options,
@@ -893,8 +902,11 @@ def _request(
893902
response_headers=None,
894903
)
895904

905+
log.debug("Raising timeout error")
896906
raise APITimeoutError(request=request) from err
897907
except Exception as err:
908+
log.debug("Encountered Exception", exc_info=True)
909+
898910
if retries > 0:
899911
return self._retry_request(
900912
options,
@@ -905,6 +917,7 @@ def _request(
905917
response_headers=None,
906918
)
907919

920+
log.debug("Raising connection error")
908921
raise APIConnectionError(request=request) from err
909922

910923
log.debug(
@@ -914,6 +927,8 @@ def _request(
914927
try:
915928
response.raise_for_status()
916929
except httpx.HTTPStatusError as err: # thrown on 4xx and 5xx status code
930+
log.debug("Encountered httpx.HTTPStatusError", exc_info=True)
931+
917932
if retries > 0 and self._should_retry(err.response):
918933
err.response.close()
919934
return self._retry_request(
@@ -930,6 +945,7 @@ def _request(
930945
if not err.response.is_closed:
931946
err.response.read()
932947

948+
log.debug("Re-raising status error")
933949
raise self._make_status_error_from_response(err.response) from None
934950

935951
return self._process_response(
@@ -951,6 +967,11 @@ def _retry_request(
951967
stream_cls: type[_StreamT] | None,
952968
) -> ResponseT | _StreamT:
953969
remaining = remaining_retries - 1
970+
if remaining == 1:
971+
log.debug("1 retry left")
972+
else:
973+
log.debug("%i retries left", remaining)
974+
954975
timeout = self._calculate_retry_timeout(remaining, options, response_headers)
955976
log.info("Retrying request to %s in %f seconds", options.url, timeout)
956977

@@ -1349,6 +1370,8 @@ async def _request(
13491370
**kwargs,
13501371
)
13511372
except httpx.TimeoutException as err:
1373+
log.debug("Encountered httpx.TimeoutException", exc_info=True)
1374+
13521375
if retries > 0:
13531376
return await self._retry_request(
13541377
options,
@@ -1359,8 +1382,11 @@ async def _request(
13591382
response_headers=None,
13601383
)
13611384

1385+
log.debug("Raising timeout error")
13621386
raise APITimeoutError(request=request) from err
13631387
except Exception as err:
1388+
log.debug("Encountered Exception", exc_info=True)
1389+
13641390
if retries > 0:
13651391
return await self._retry_request(
13661392
options,
@@ -1371,6 +1397,7 @@ async def _request(
13711397
response_headers=None,
13721398
)
13731399

1400+
log.debug("Raising connection error")
13741401
raise APIConnectionError(request=request) from err
13751402

13761403
log.debug(
@@ -1380,6 +1407,8 @@ async def _request(
13801407
try:
13811408
response.raise_for_status()
13821409
except httpx.HTTPStatusError as err: # thrown on 4xx and 5xx status code
1410+
log.debug("Encountered httpx.HTTPStatusError", exc_info=True)
1411+
13831412
if retries > 0 and self._should_retry(err.response):
13841413
await err.response.aclose()
13851414
return await self._retry_request(
@@ -1396,6 +1425,7 @@ async def _request(
13961425
if not err.response.is_closed:
13971426
await err.response.aread()
13981427

1428+
log.debug("Re-raising status error")
13991429
raise self._make_status_error_from_response(err.response) from None
14001430

14011431
return self._process_response(
@@ -1417,6 +1447,11 @@ async def _retry_request(
14171447
stream_cls: type[_AsyncStreamT] | None,
14181448
) -> ResponseT | _AsyncStreamT:
14191449
remaining = remaining_retries - 1
1450+
if remaining == 1:
1451+
log.debug("1 retry left")
1452+
else:
1453+
log.debug("%i retries left", remaining)
1454+
14201455
timeout = self._calculate_retry_timeout(remaining, options, response_headers)
14211456
log.info("Retrying request to %s in %f seconds", options.url, timeout)
14221457

0 commit comments

Comments
 (0)
Please sign in to comment.