-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: failed to initialize FishAudio TTS instance and improve handling logic #4200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Fix `KeyError: 'model'``: 适配新版配置结构。 - Add `timeout` support: 防止长文本生成时超时。 - Improve response handling: 使用更标准的 Header 检查方式。
Contributor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey - 我发现了 1 个问题,并给出了一些总体层面的反馈:
timeout值目前是通过int(provider_config.get("timeout", 20))直接进行类型转换的,如果配置了非数字值就会抛出异常;建议使用更安全的转换方式并提供回退逻辑(例如 try/except,或者使用float提高灵活性),以避免出现难以排查的配置错误。- 在错误分支中,你现在把
error_text(原始字节流)直接放到了异常消息里;如果能先用 UTF‑8 解码并带有回退策略,和/或对过长的响应进行截断,会让错误输出更易读,也更适合安全地写入日志。
给 AI Agent 的提示
Please address the comments from this code review:
## Overall Comments
- The `timeout` value is cast directly with `int(provider_config.get("timeout", 20))`, which will raise if a non-numeric value is configured; consider wrapping this in a safe conversion with a fallback (e.g., try/except or using `float` for more flexibility) to avoid hard-to-diagnose config errors.
- In the error path you now include `error_text` (raw bytes) directly in the exception message; decoding to UTF‑8 with a fallback and/or truncating excessively long responses would make the error output more readable and safer to log.
## Individual Comments
### Comment 1
<location> `astrbot/core/provider/sources/fishaudio_tts_api_source.py:152-155` </location>
<code_context>
- body = await response.aread()
- text = body.decode("utf-8", errors="replace")
- raise Exception(f"Fish Audio API请求失败: {text}")
+ error_text = await response.aread()
+ raise Exception(
+ f"Fish Audio API请求失败: 状态码 {response.status_code}, 响应内容: {error_text}"
+ )
</code_context>
<issue_to_address>
**suggestion:** 在将错误内容放入异常前,对错误主体进行解码并视情况截断。
`error_text` 在这里仍然是字节类型,因此异常信息中会显示 `b'...'` 的形式,并且可能包含非常大的响应体。可以先用带回退机制的解码方式进行解码,并在加入异常前进行截断,例如:
```python
text = error_text.decode("utf-8", errors="replace")
text = text[:1000]
raise Exception(
f"Fish Audio API请求失败: 状态码 {response.status_code}, 响应内容: {text}"
)
```
这样既能保持错误消息可读,也能避免产生日志过大的问题。
```suggestion
error_bytes = await response.aread()
error_text = error_bytes.decode("utf-8", errors="replace")
error_text = error_text[:1000]
raise Exception(
f"Fish Audio API请求失败: 状态码 {response.status_code}, 响应内容: {error_text}"
)
```
</issue_to_address>帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进之后的代码审查。
Original comment in English
Hey - I've found 1 issue, and left some high level feedback:
- The
timeoutvalue is cast directly withint(provider_config.get("timeout", 20)), which will raise if a non-numeric value is configured; consider wrapping this in a safe conversion with a fallback (e.g., try/except or usingfloatfor more flexibility) to avoid hard-to-diagnose config errors. - In the error path you now include
error_text(raw bytes) directly in the exception message; decoding to UTF‑8 with a fallback and/or truncating excessively long responses would make the error output more readable and safer to log.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `timeout` value is cast directly with `int(provider_config.get("timeout", 20))`, which will raise if a non-numeric value is configured; consider wrapping this in a safe conversion with a fallback (e.g., try/except or using `float` for more flexibility) to avoid hard-to-diagnose config errors.
- In the error path you now include `error_text` (raw bytes) directly in the exception message; decoding to UTF‑8 with a fallback and/or truncating excessively long responses would make the error output more readable and safer to log.
## Individual Comments
### Comment 1
<location> `astrbot/core/provider/sources/fishaudio_tts_api_source.py:152-155` </location>
<code_context>
- body = await response.aread()
- text = body.decode("utf-8", errors="replace")
- raise Exception(f"Fish Audio API请求失败: {text}")
+ error_text = await response.aread()
+ raise Exception(
+ f"Fish Audio API请求失败: 状态码 {response.status_code}, 响应内容: {error_text}"
+ )
</code_context>
<issue_to_address>
**suggestion:** Decode and possibly truncate the error body before including it in the exception.
`error_text` is still bytes here, so the exception will show a `b'...'` repr and may include an extremely large body. Decode with a fallback and truncate before including it, e.g.:
```python
text = error_text.decode("utf-8", errors="replace")
text = text[:1000]
raise Exception(
f"Fish Audio API请求失败: 状态码 {response.status_code}, 响应内容: {text}"
)
```
This keeps the message readable and prevents excessively large logs.
```suggestion
error_bytes = await response.aread()
error_text = error_bytes.decode("utf-8", errors="replace")
error_text = error_text[:1000]
raise Exception(
f"Fish Audio API请求失败: 状态码 {response.status_code}, 响应内容: {error_text}"
)
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Soulter
reviewed
Dec 26, 2025
Soulter
approved these changes
Dec 26, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Modifications / 改动点
KeyError: 'model': 适配新版配置结构。 [Bug] 添加 fishaudio 文本转语音模型报错:实例化 fishaudio_tts_api 提供商适配器失败 #4172timeoutsupport: 防止长文本生成时超时。timeout加上try-except块,防止因为配置文件里的脏数据(比如填了个字符串 "20s")导致崩溃。Screenshots or Test Results / 运行截图或测试结果
Before:无法实例化 fish Audio TTS,并且会在 WebUI 中弹出报错信息
After:可以在 WebUI 中正常配置并生成语音服务
🤖 AstrBot PR Reviewer 正在分析您的代码... 分析完成。
文件路径
core/provider/sources/fishaudio_tts_api_source.py审查报告1. 版本与运行环境
httpx.AsyncClient,完全符合异步环境要求。2. 综合审查维度
.get("model", None)修复了潜在的KeyError。timeout支持,默认 20s,这是一个非常棒的健壮性改进。startswith("audio/"),比之前的强校验更灵活且标准。ormsgpack进行序列化,效率较高。3. 框架适应性检查
async with和await,无阻塞 IO 操作。代码评审团
Linus Torvalds:
“这就是我为什么讨厌随便重构核心 API!看看这堆烂摊子,框架一更新,配置结构一变,下游的代码就得跟着像猴子一样跳来跳去。
KeyError: 'model'?这种低级错误本就不该发生!如果框架开发者在改动配置结构时能多动动脑子考虑一下向后兼容性,你就不用在这里给他们擦屁股了! 不过,既然烂摊子已经铺开了,你用.get('model', None)这种防御性编程来兜底是对的。这就是现实,你要么在框架的愚蠢改动下崩溃,要么就得自己把代码写得像坦克一样结实。干得不错,至少你没让这坨代码直接炸在我脸上。”史蒂夫·乔布斯:
“这个修改不仅仅是修复了一个 Bug,它是在打磨体验。原本的音频流检测逻辑太生硬了,就像以前的 DOS 界面,非黑即白。现在的
startswith('audio/')更加优雅,它包容了未来的可能性。哪怕以后 FishAudio 决定给音频流加上编码格式,我们的系统依然能丝滑运转。这种对细节的关注,就是区分平庸与卓越的分界线。虽然还谈不上完美,但方向对了。”GlaDOS (传送门):
“检测到代码修改。你的补丁... 有点意思。你不仅防止了那些愚蠢的人类在配置里填错数据(把 timeout 填成 'forever' 之类的),还学会了在他们搞砸 API 请求时,用一种他们那可怜的大脑能理解的方式(UTF-8 文本)告诉他们。截断错误信息是个明智之举,避免了我的日志系统因为某些服务器发疯返回的几兆垃圾数据而溢出。好吧,鉴于你这种意外的胜任能力,今天的测试难度将提升 5%。”
Disclaimer: 以上评审内容由 AI 自动生成,所涉及人物形象与现实无关,不代表真实人物观点。如果给出的建议无关痛痒请忽略。
Checklist / 检查清单
requirements.txt和pyproject.toml文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations inrequirements.txtandpyproject.toml.Summary by Sourcery
调整 FishAudio TTS 提供方以适配更新后的配置、支持请求超时,并提升 API 响应的健壮性。
新功能:
错误修复:
改进:
audio/*响应;当 FishAudio TTS 请求失败时,返回包含 HTTP 状态码和响应体的更清晰错误信息。Original summary in English
Summary by Sourcery
Adjust FishAudio TTS provider to handle updated configuration, support request timeouts, and improve API response robustness.
New Features:
Bug Fixes:
Enhancements: