fix: preserve assistant messages with reasoning_content in sanitize pass#8483
Open
renchonghan wants to merge 2 commits into
Open
fix: preserve assistant messages with reasoning_content in sanitize pass#8483renchonghan wants to merge 2 commits into
renchonghan wants to merge 2 commits into
Conversation
When _sanitize_assistant_messages encounters an assistant message with empty content and no tool_calls but with reasoning_content, keep it with content set to empty string instead of dropping it. Reasoning models (DeepSeek V4, MiMo, etc.) require this history for subsequent turn validation.
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The nested
ifinsideif _is_empty(content) and not tool_calls:is a bit hard to follow; consider extracting thereasoning_contenthandling into a small helper or using early returns/continues to flatten the control flow for readability. - The log message for discarded messages now mentions
(无 context | tool_calls | reasoning_content)while the code checkscontent,tool_calls, andreasoning_content; aligning the terminology in the log with the actual field names would avoid confusion when debugging.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The nested `if` inside `if _is_empty(content) and not tool_calls:` is a bit hard to follow; consider extracting the `reasoning_content` handling into a small helper or using early returns/continues to flatten the control flow for readability.
- The log message for discarded messages now mentions `(无 context | tool_calls | reasoning_content)` while the code checks `content`, `tool_calls`, and `reasoning_content`; aligning the terminology in the log with the actual field names would avoid confusion when debugging.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 updates the empty assistant message filtering logic in openai_source.py to properly handle reasoning models. When a message contains reasoning_content but lacks content and tool_calls, it now sets msg["content"] to an empty string to satisfy API validation instead of discarding it. The reviewer noted a minor typo in the new debug log message, where "context" was used instead of "content", and provided a code suggestion to correct it.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
2 tasks
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.
Link Bug Report #8482
修复
_sanitize_assistant_messages上传含reasoning_content的 assistant 历史消息时,content和tool_calls都为空导致的 API 返回400错误背景:
现有逻辑在对 assistant 消息做空内容清理时,只要
reasoning_contentcontenttool_calls有一个不为空就会放行。但 DeepSeek V4 系列、MiMo 系列等推理模型要求历史 assistant 消息中
content和tool_calls至少有一个不为空,否则 API 返回 400 错误。改动:将有
reasoning_content但无content/tool_calls的消息进行占位保留,设置content=""保证content和tool_calls至少有一个不为空的API要求;仅对三者全空的真正垃圾消息执行过滤,同时日志级别从
warning降为debug,减少噪音。Modifications / 改动点
astrbot/core/provider/sources/openai_source.py—_sanitize_assistant_messages()方法内条件分支重构This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
已在实际环境中验证,DeepSeek V4 系列模型的 tool_calls 和多轮推理均恢复正常。
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: