perf: Set content to None when the OpenAI message content list is empty#6551
Conversation
_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
Summary of ChangesHello, 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)后,如果历史消息中的 Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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_content 或 tool_calls 时,content 可以为 null。此更改位于 ProviderOpenAIOfficial 基类中,旨在从源头解决此兼容性问题。经过审查,该修改符合 PR 描述的意图,逻辑上解决了所述问题。
content to None when the OpenAI message content list is empty
问题
从 DeepSeek(带 reasoning)切换到 Grok 后,历史消息中的 assistant 消息 content 变成空列表
[],Grok 拒绝请求:根因
_finally_convert_payload在处理 assistant 消息时,把think类型的 content part 提取到reasoning_content,剩下的放进new_content。如果所有 part 都是 think 类型(DeepSeek 纯思考的消息),new_content就是[]。Grok 和其他一些 provider 不接受空 content list,直接 400。
修复
空列表回退到
None。OpenAI 兼容 API 普遍接受nullcontent 的 assistant 消息(只要有reasoning_content或tool_calls)。这是一个核心层面的修复,不是给某个 provider 打补丁,所以切换到任何不支持空 content 的 provider 都不会再出问题。
复现
修复后 step 3 正常返回。
Fixes #6447
Summary by Sourcery
Bug Fixes: