修复 ZhipuTranslator 错误处理程序中的 UnboundLocalError

作者: pamod-madubashana创建于 2026年9月23日更新于 2026年9月23日

Description

When translating with the Zhipu service, I encountered an UnboundLocalError and the API returned an error. In pdf2zh/translator.py:626, ZhipuTranslator.do_translate references response inside the except OpenAI.BadRequestError handler, but response is only assigned when self.client.chat.completions.create() succeeds -- that is, exactly when the handler is not running. So any API-level failure masks the real error with UnboundLocalError: cannot access local variable 'response' ..., and the intended "IRREPARABLE TRANSLATION ERROR" fallback for code 1301 can never trigger.

内容来源: PDFMathTranslate/PDFMathTranslate