Skip to content

fix: preserve assistant messages with reasoning_content in sanitize pass#8483

Open
renchonghan wants to merge 2 commits into
AstrBotDevs:masterfrom
renchonghan:fix/sanitize-assistant-reasoning-messages
Open

fix: preserve assistant messages with reasoning_content in sanitize pass#8483
renchonghan wants to merge 2 commits into
AstrBotDevs:masterfrom
renchonghan:fix/sanitize-assistant-reasoning-messages

Conversation

@renchonghan
Copy link
Copy Markdown

@renchonghan renchonghan commented Jun 1, 2026

Link Bug Report #8482

修复 _sanitize_assistant_messages 上传含 reasoning_content 的 assistant 历史消息时,contenttool_calls 都为空导致的 API 返回400错误

背景
现有逻辑在对 assistant 消息做空内容清理时,只要 reasoning_content content tool_calls 有一个不为空就会放行。
但 DeepSeek V4 系列、MiMo 系列等推理模型要求历史 assistant 消息中contenttool_calls 至少有一个不为空,否则 API 返回 400 错误。

改动:将有 reasoning_content 但无 content/tool_calls 的消息进行占位保留,设置 content="" 保证contenttool_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 和多轮推理均恢复正常。

image

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:

  • Prevent API 400 errors by ensuring assistant messages with reasoning_content but no content or tool_calls are preserved with a placeholder content value.

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.
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels Jun 1, 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:

  • 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.
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.

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

Comment thread astrbot/core/provider/sources/openai_source.py Outdated
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
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. size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant