Skip to content

perf: Set content to None when the OpenAI message content list is empty#6551

Merged
Soulter merged 1 commit intoAstrBotDevs:masterfrom
he-yufeng:fix/empty-content-after-reasoning
Apr 3, 2026
Merged

perf: Set content to None when the OpenAI message content list is empty#6551
Soulter merged 1 commit intoAstrBotDevs:masterfrom
he-yufeng:fix/empty-content-after-reasoning

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

@he-yufeng he-yufeng commented Mar 18, 2026

问题

从 DeepSeek(带 reasoning)切换到 Grok 后,历史消息中的 assistant 消息 content 变成空列表 [],Grok 拒绝请求:

Invalid request content: Each message must have at least one content element.

根因

_finally_convert_payload 在处理 assistant 消息时,把 think 类型的 content part 提取到 reasoning_content,剩下的放进 new_content。如果所有 part 都是 think 类型(DeepSeek 纯思考的消息),new_content 就是 []

Grok 和其他一些 provider 不接受空 content list,直接 400。

修复

# 修复前
message["content"] = new_content

# 修复后
message["content"] = new_content or None

空列表回退到 None。OpenAI 兼容 API 普遍接受 null content 的 assistant 消息(只要有 reasoning_contenttool_calls)。

这是一个核心层面的修复,不是给某个 provider 打补丁,所以切换到任何不支持空 content 的 provider 都不会再出问题。

复现

  1. 用 DeepSeek(开启 reasoning)对话,触发工具调用(产生 reasoning_content + 空 content)
  2. 切换到 Grok 系列模型
  3. 继续对话 → 400 错误

修复后 step 3 正常返回。

Fixes #6447

Summary by Sourcery

Bug Fixes:

  • Avoid 400 errors from providers like Grok by ensuring assistant messages never send an empty content list when they contain only reasoning content.

_finally_convert_payload 提取 think 部分后,如果 assistant 消息的
所有 content 都是 think 类型,new_content 会变成空列表 []。
Grok 等 provider 不接受空 content list,直接报 400。

改为 new_content or None,空列表时回退到 None(OpenAI 兼容 API
普遍接受 null content 的 assistant 消息)。

Fixes AstrBotDevs#6447
@auto-assign auto-assign Bot requested review from Fridemn and Raven95676 March 18, 2026 05:54
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Mar 18, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在解决一个关键的兼容性问题,即在切换 AI 模型(例如从 DeepSeek 切换到 Grok)后,如果历史消息中的 assistant 消息 content 字段为空列表,会导致某些提供商(如 Grok)返回 400 错误。通过将空列表 content 优雅地回退到 None,此修复提高了系统的健壮性和跨不同模型提供商的兼容性,确保了即使在 assistant 消息仅包含推理内容时也能正常通信。

Highlights

  • 修复 Grok 400 错误: 解决了从 DeepSeek 切换到 Grok 等模型时,因 assistant 消息的 content 为空列表而导致的 400 错误。
  • 内容处理优化: 当 assistant 消息的所有内容部分都是 think 类型时,将 content 从空列表 [] 转换为 None
  • 兼容性增强: 确保了与不接受空 content 列表的 OpenAI 兼容 API 提供商的兼容性,这是一个核心层面的修复。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

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 reviewed your changes and they look great!


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

这个 Pull Request 旨在修复一个问题:当从生成 think 内容的模型切换到不支持空 content 列表的 Provider (如 Grok) 时,会因为历史消息中只包含 think 内容而导致 content 字段变为空列表 [],进而引发 400 错误。修复方案是将 message["content"] = new_content 修改为 message["content"] = new_content or None。这使得在 new_content 为空列表的情况下,content 字段会变为 None。根据 PR 描述,这符合 OpenAI 兼容 API 的预期行为,即当存在 reasoning_contenttool_calls 时,content 可以为 null。此更改位于 ProviderOpenAIOfficial 基类中,旨在从源头解决此兼容性问题。经过审查,该修改符合 PR 描述的意图,逻辑上解决了所述问题。

@dosubot dosubot Bot added the area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. label Mar 18, 2026
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Apr 3, 2026
@Soulter Soulter changed the title 修复切换模型后 empty content 导致 Grok 等 provider 返回 400 perf: Set content to None when the OpenAI message content list is empty Apr 3, 2026
@Soulter Soulter merged commit 1408a84 into AstrBotDevs:master Apr 3, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. 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] 400 from Grok after switching model

2 participants