Skip to content

fix: accept reasoning-only OpenAI completions#6115

Open
stablegenius49 wants to merge 1 commit intoAstrBotDevs:masterfrom
stablegenius49:fix/6108-kimi-reasoning-only
Open

fix: accept reasoning-only OpenAI completions#6115
stablegenius49 wants to merge 1 commit intoAstrBotDevs:masterfrom
stablegenius49:fix/6108-kimi-reasoning-only

Conversation

@stablegenius49
Copy link
Contributor

@stablegenius49 stablegenius49 commented Mar 12, 2026

Modifications / 改动点

  • allow _parse_openai_completion to accept reasoning-only responses when content is empty and there are no tool calls

  • add a regression test covering a completion that only carries reasoning_content

  • keep the existing parse failure for completions that have neither text, tool calls, nor reasoning output

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

python3.11 -m pytest tests/test_openai_source.py -q
# 11 passed, 3 warnings in 1.40s

ruff check astrbot/core/provider/sources/openai_source.py tests/test_openai_source.py
ruff format --check astrbot/core/provider/sources/openai_source.py tests/test_openai_source.py
# All checks passed!

Closes #6108.

Summary by Sourcery

处理仅包含推理内容的 OpenAI 聊天补全结果,而不再将其视为解析失败。

Bug 修复:

  • 允许解析那些内容为空、没有工具调用,但具有非空推理内容的 OpenAI 补全结果,同时对于既没有文本、也没有工具、也没有推理内容的补全结果仍然视为失败。

测试:

  • 添加一个异步回归测试,用于验证仅含推理内容的 OpenAI 补全能够被正确解析为 assistant 响应。
Original summary in English

Summary by Sourcery

Handle OpenAI chat completions that contain only reasoning content without treating them as parse failures.

Bug Fixes:

  • Allow parsing of OpenAI completions that have empty content, no tool calls, but non-empty reasoning content, while still failing completions that have no text, tools, or reasoning.

Tests:

  • Add an async regression test verifying reasoning-only OpenAI completions are parsed into assistant responses correctly.

@auto-assign auto-assign bot requested review from Soulter and advent259141 March 12, 2026 09:46
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Mar 12, 2026
@gemini-code-assist
Copy link
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!

This pull request enhances the system's ability to process OpenAI API responses by allowing the _parse_openai_completion function to correctly handle completions that consist solely of reasoning content. This change improves the robustness of the integration, ensuring that valid but non-standard responses are not erroneously rejected, thereby broadening the types of LLM outputs the application can effectively utilize.

Highlights

  • OpenAI Completion Parsing: Modified the _parse_openai_completion function to accept responses that contain only reasoning content, even if completion_text and tool_calls are empty.
  • Regression Test: Added a new regression test to specifically cover the scenario of a reasoning-only OpenAI completion, ensuring the new parsing logic works as expected.
  • Error Handling: Preserved the existing error handling for completions that lack any form of content (text, tool calls, or reasoning), preventing parsing failures for truly empty responses.
Changelog
  • astrbot/core/provider/sources/openai_source.py
    • Modified the conditional check within _parse_openai_completion to include llm_response.reasoning_content as a valid form of completion, preventing an exception when only reasoning is present.
  • tests/test_openai_source.py
    • Added test_parse_openai_completion_accepts_reasoning_only_response to verify that the parser correctly handles OpenAI completions where only reasoning_content is provided.
Activity
  • No human activity has been recorded on this pull request yet.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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
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 - 我在这里给出一些整体性的反馈:

  • 新的解析条件依赖于 llm_response.reasoning_content 一定存在;如果这个属性不能在所有 llm_response 实例上得到保证,建议在构造时统一进行初始化(例如设为 None),以避免属性访问错误。
  • 你可能需要增加一个负向测试用例:当 reasoning_content 是空字符串(或仅包含空白字符)时,验证这种响应仍然会被视作无法解析,从而确保只接受真正“仅推理内容”的补全结果这一意图得以保持。
给 AI 智能体的提示词
Please address the comments from this code review:

## Overall Comments
- The new parsing condition relies on `llm_response.reasoning_content` being present; if that attribute is not guaranteed on all `llm_response` instances, consider initializing it consistently (e.g., to `None`) at construction to avoid attribute errors.
- You might want to add a negative test case where `reasoning_content` is an empty string (or only whitespace) to confirm that such responses are still treated as unparseable, matching the intent to only accept genuinely reasoning-only completions.

Sourcery 对开源项目是免费的——如果你觉得这些评审对你有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的代码评审。
Original comment in English

Hey - I've left some high level feedback:

  • The new parsing condition relies on llm_response.reasoning_content being present; if that attribute is not guaranteed on all llm_response instances, consider initializing it consistently (e.g., to None) at construction to avoid attribute errors.
  • You might want to add a negative test case where reasoning_content is an empty string (or only whitespace) to confirm that such responses are still treated as unparseable, matching the intent to only accept genuinely reasoning-only completions.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new parsing condition relies on `llm_response.reasoning_content` being present; if that attribute is not guaranteed on all `llm_response` instances, consider initializing it consistently (e.g., to `None`) at construction to avoid attribute errors.
- You might want to add a negative test case where `reasoning_content` is an empty string (or only whitespace) to confirm that such responses are still treated as unparseable, matching the intent to only accept genuinely reasoning-only completions.

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.

@dosubot dosubot bot added the area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. label Mar 12, 2026
Copy link
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 modifies _parse_openai_completion to correctly handle OpenAI completions that only contain reasoning_content, preventing a parsing failure. The logic is updated to consider reasoning_content as a valid output, alongside completion_text and tools_call_args. A corresponding regression test has been added to ensure these reasoning-only responses are parsed successfully. The changes are logical and the new test effectively covers the fix. The implementation looks solid.

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:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 使用 Kimi 模型时,模型仅返回 reasoning_content 而 content 为空时,parse_openai_completion 抛出异常

1 participant