fix: force Bocha search API requests to use gzip/deflate encoding to avoid aiohttp 3.12+ brotli decoding errors.#7655
Merged
Soulter merged 2 commits intoAstrBotDevs:masterfrom Apr 19, 2026
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider adding a brief inline comment (with an aiohttp issue/PR reference if available) explaining why
Accept-Encodingis restricted here, so future maintainers understand this is a workaround for a specific brotli handling bug rather than an arbitrary header choice.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider adding a brief inline comment (with an aiohttp issue/PR reference if available) explaining why `Accept-Encoding` is restricted here, so future maintainers understand this is a workaround for a specific brotli handling bug rather than an arbitrary header choice.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Contributor
There was a problem hiding this comment.
Code Review
This pull request adds an Accept-Encoding header to the _bocha_search function to explicitly support gzip and deflate compression. The reviewer suggested adding a code comment to document that this change avoids a known brotli decompression issue in aiohttp 3.12+ and recommended applying similar changes to other search utility functions like _tavily_search and _brave_search.
| header = { | ||
| "Authorization": f"Bearer {bocha_key}", | ||
| "Content-Type": "application/json", | ||
| "Accept-Encoding": "gzip, deflate", |
Contributor
There was a problem hiding this comment.
由于 aiohttp 3.12+ 的 brotli 解压问题是库层面的通用问题,建议检查并考虑在本项目中其他使用 aiohttp 进行网络请求的函数(如 _tavily_search, _brave_search 等)中也显式指定 Accept-Encoding,以防止这些服务在未来启用 brotli 压缩时导致类似的崩溃。此外,建议在代码中添加注释说明此 Header 的作用,以便后续维护。
Suggested change
| "Accept-Encoding": "gzip, deflate", | |
| "Accept-Encoding": "gzip, deflate", # 规避 aiohttp 3.12+ brotli 解压 bug |
Soulter
approved these changes
Apr 19, 2026
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
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.
Fixes #7654
当 aiohttp >= 3.12 时,Bocha 搜索 API 返回 brotli 压缩格式的响应,而 aiohttp 3.12+ 在处理 brotli 解压时存在 bug,导致所有 Bocha 搜索请求失败,报错
Can not decode content-encoding: br。Modifications / 改动点
修改
astrbot/core/tools/web_search_tools.py中的_bocha_search()函数,在请求头中添加Accept-Encoding: gzip, deflate,禁止服务器返回 brotli 格式响应,从根本上规避 aiohttp 的兼容性问题。This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
修复前报错:
aiohttp.http_exceptions.ContentEncodingError: 400, message:
Can not decode content-encoding: br
aiohttp.client_exceptions.ClientPayloadError: 400, message:
Can not decode content-encoding: br
修复后(将 aiohttp 降至 3.11.18 验证逻辑正确,加上此 header 后 aiohttp 3.13.x 下同样正常):
Tool
web_search_bocha正常返回搜索结果。Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Bug Fixes: