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等)有同样问题但未改,按需修复
相关推荐
جيون داد ناالام ميづ23 分钟前
七、MyBatis 源码剖析:核心流程拆解(初始化 + SQL 执行)
java·sql·mybatis
ClkLog-开源埋点用户分析33 分钟前
【产品说明】【埋点分析】ClkLog 商业版 Java 运行环境安全更新说明
java·开发语言
秋饼38 分钟前
Spring Framework 7 内置弹性实战:@Retryable 语义翻转、虚拟线程流量放大与 LLM 网关治理
java·ai·技术分享·后端开发
RuoyiOffice1 小时前
Flowable只是起点:一套OA背后的单据、资源与协作设计
java·vue.js·spring boot·flowable·企业管理·oa·ruoyi office
yuzhiboyouye1 小时前
《阿里巴巴 Java 开发手册》的 MySQL 核心规范
java·开发语言·mysql
郝学胜-神的一滴2 小时前
Effective STL 条款12:对容器线程安全性的现实期望
c语言·开发语言·c++·qt·网络协议·rpc·numpy
geovindu2 小时前
python: KNeighborsClassifier K
开发语言·后端·python·算法
秋饼3 小时前
Spring AI 2.0 渐进式工具披露实战:ToolSearchToolCallingAdvisor 源码机制、四种 ToolIndex 选型与多租户踩坑
java·ai·技术分享·后端开发
时间的情敌4 小时前
Three.js 射线拾取进阶:hover 高亮、点击弹窗、多物体过滤、穿透拾取
开发语言·javascript·three.js
Amumu121384 小时前
TypeScript学习: 基本类型(一)
开发语言·前端·typescript