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.py 中 OpenAITranslator.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等)有同样问题但未改,按需修复
相关推荐
小羊没烦恼!5 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
俊昭喜喜里5 天前
java中的继承和多态的区别
java
小羊没烦恼!5 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
譕痕5 天前
JSONObject与JSONArray封装数据格式区别
java·json
胡写代码6 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖6 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商
此时不提桶,更待何时6 天前
01-06-A-JVM排查实战详解
java·jvm
伞伞悦读6 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
vipxieliang6 天前
ValidX 在 DDD 领域驱动设计中的实践
java·spring boot
C语言小火车6 天前
C/C++ 为什么需要编译器?
开发语言·c++