Skip to content

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
MagicSun7940:master
Apr 19, 2026
Merged

fix: force Bocha search API requests to use gzip/deflate encoding to avoid aiohttp 3.12+ brotli decoding errors.#7655
Soulter merged 2 commits intoAstrBotDevs:masterfrom
MagicSun7940:master

Conversation

@MagicSun7940
Copy link
Copy Markdown
Contributor

@MagicSun7940 MagicSun7940 commented Apr 18, 2026

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.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Bug Fixes:

  • Force Bocha search API requests to use gzip/deflate encoding to avoid aiohttp 3.12+ brotli decoding errors.

@auto-assign auto-assign bot requested review from Fridemn and advent259141 April 18, 2026 17:38
@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Apr 18, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-Encoding is 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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

由于 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

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Apr 19, 2026
@Soulter Soulter merged commit b2a9571 into AstrBotDevs:master Apr 19, 2026
20 checks passed
@Soulter Soulter changed the title 修复了使用 Bocha 搜索时报错 "Can not decode content-encoding: br"的bug fix: force Bocha search API requests to use gzip/deflate encoding to avoid aiohttp 3.12+ brotli decoding errors. Apr 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 使用 Bocha 搜索时报错 "Can not decode content-encoding: br"

2 participants