ERROR:pdf2zh.converter:‘str‘ object has no attribute ‘choices‘ converter.py:357

pdf2zh 翻译报错修复记录 (2026-04-09)

错误

复制代码
ERROR:pdf2zh.converter:'str' object has no attribute 'choices'  converter.py:357

环境

  • pdf2zh 1.9.11, openai 2.30.0, Python 3.11
  • 安装路径: C:\Users\23670\AppData\Local\Programs\Python\Python311\Lib\site-packages\pdf2zh\
  • 启动方式: pdf2zh -i

原因

translator.pyOpenAITranslator.do_translate() 调用 client.chat.completions.create() 后直接访问 response.choices[0].message.content,但 API(或代理)返回了 str 而非 ChatCompletion 对象。

修复

文件: translator.py 第446行附近,添加 isinstance(response, str) 兜底:

python 复制代码
# 修改前
response = self.client.chat.completions.create(...)
if not response.choices:
    if hasattr(response, "error"):
        raise ValueError("Error response from Service", response.error)
content = response.choices[0].message.content.strip()

# 修改后
response = self.client.chat.completions.create(...)
if isinstance(response, str):
    content = response.strip()
else:
    if not response.choices:
        if hasattr(response, "error"):
            raise ValueError("Error response from Service", response.error)
    content = response.choices[0].message.content.strip()

注意

  • 升级 pdf2zh 会覆盖此修改,需重新打补丁
  • 其他翻译器类(AzureOpenAI、Silicon等)有同样问题但未改,按需修复
相关推荐
曹牧16 分钟前
文档格式:OFD
java
豆角焖肉41 分钟前
Maven进阶与搭建私服
java·maven
大不点wow1 小时前
Java序列化与反序列化:让对象走出JVM
java·开发语言·jvm
阿里嘎多学长1 小时前
2026-07-22 GitHub 热点项目精选
开发语言·程序员·github·代码托管
噢,我明白了1 小时前
Java中日期和字符串的处理
java·开发语言·日期
爱刷碗的苏泓舒1 小时前
C 语言 if-else 与 switch-case 分支语句对比
c语言·开发语言
dkbnull1 小时前
Spring Boot请求处理组件对比详解
java·spring boot
jun_bai1 小时前
Orthanc服务器使用java上传dicom影像文件
java·运维·服务器
-银雾鸢尾-1 小时前
C#中的泛型约束
开发语言·c#
顺风尿一寸1 小时前
记一次 Spring AOP 与定时任务引发的死锁排查
java