Skip to content

fix(openai translator): enhance error handling for empty API responses#604

Merged
awwaawwa merged 1 commit intoPDFMathTranslate:mainfrom
zqqian:add-openai-err-raise
Feb 12, 2025
Merged

fix(openai translator): enhance error handling for empty API responses#604
awwaawwa merged 1 commit intoPDFMathTranslate:mainfrom
zqqian:add-openai-err-raise

Conversation

@zqqian
Copy link
Contributor

@zqqian zqqian commented Feb 12, 2025

fix(api): add error handling for OpenAI API failures

Previously, API errors were not handled, This commit adds exception
handling for API errors, connection issues, and rate limits, ensuring
better stability.

Handled exceptions:

  • openai.APIError: Captures general API errors.
  • openai.APIConnectionError: Handles network issues.
  • openai.RateLimitError: Prevents crashes due to rate limiting.
  • Checks for empty responses to avoid index errors. (For tencent cloud lkeap)

This improves robustness when interacting with OpenAI's API.

@awwaawwa
Copy link
Collaborator

In current versions, there was error handling for upstream issues at this position, you can take a look.

For logger, you can use logger.exception which automatically records exception information.

https://github.com/Byaidu/PDFMathTranslate/blob/fece247d5e6d89a032399d30edcc15719681e8f5/pdf2zh/converter.py#L349C1-L350C1

@zqqian
Copy link
Contributor Author

zqqian commented Feb 12, 2025

@awwaawwa I have removed the error-handling code, keeping only a check for response.choices is None and raising an error.

When using third-party OpenAI-compatible APIs, such as Tencent Cloud's Deepseek, errors are not generated according to OpenAI's specifications. This leads to issues like:

Files before translation: ['xxx.pdf']
  0%|                                                                                                                                                                                                                  | 0/26 [00:00<?, ?it/s]ERROR:pdf2zh.converter:'NoneType' object is not subscriptable
ERROR:pdf2zh.converter:'NoneType' object is not subscriptable
ERROR:pdf2zh.converter:'NoneType' object is not subscriptable
ERROR:pdf2zh.converter:'NoneType' object is not subscriptable
ERROR:pdf2zh.converter:'NoneType' object is not subscriptable

This makes it difficult to diagnose the root cause of the issue, as it appears to be a problem with Pdf2zh itself.

With the code modification, the error message now explicitly indicates an issue with the OpenAI API call:

Files before translation: ['xxx.pdf']
  0%|                                                                                                                                                                                                                  | 0/26 [00:00<?, ?it/s]
ERROR:pdf2zh.converter:('Empty response from OpenAI API', {'message': 'concurrency exceeded', 'type': 'runtime_error', 'param': None, 'code': '20034'})
ERROR:pdf2zh.converter:('Empty response from OpenAI API', {'message': 'concurrency exceeded', 'type': 'runtime_error', 'param': None, 'code': '20034'})
ERROR:pdf2zh.converter:('Empty response from OpenAI API', {'message': 'concurrency exceeded', 'type': 'runtime_error', 'param': None, 'code': '20034'})
ERROR:pdf2zh.converter:('Empty response from OpenAI API', {'message': 'concurrency exceeded', 'type': 'runtime_error', 'param': None, 'code': '20034'})
ERROR:pdf2zh.converter:('Empty response from OpenAI API', {'message': 'concurrency exceeded', 'type': 'runtime_error', 'param': None, 'code': '20034'})

This change helps to better identify API-related issues.

@awwaawwa
Copy link
Collaborator

Thank you for your contribution!

@awwaawwa awwaawwa changed the title fix(api): add error handling for OpenAI API failures fix(openai translator): enhance error handling for empty API responses Feb 12, 2025
@awwaawwa awwaawwa merged commit 3f50d5c into PDFMathTranslate:main Feb 12, 2025
2 checks passed
@zqqian zqqian deleted the add-openai-err-raise branch February 12, 2025 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants