Skip to content

fix: merge streamed tool results per call#8050

Open
he-yufeng wants to merge 1 commit intoAstrBotDevs:masterfrom
he-yufeng:fix/async-generator-tool-result
Open

fix: merge streamed tool results per call#8050
he-yufeng wants to merge 1 commit intoAstrBotDevs:masterfrom
he-yufeng:fix/async-generator-tool-result

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

@he-yufeng he-yufeng commented May 7, 2026

Fixes #8028.

Modifications / 改动点

  • Collects multiple CallToolResult chunks produced by an async-generator tool execution and appends a single tool result block for the original tool_call_id.

  • Keeps cached image handling and tool hook behavior in the existing flow.

  • Adds a regression test that yields two CallToolResult values for one tool call and verifies AstrBot stores one tool message with both partial results.

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

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

Verification commands run locally on Windows:

uv run pytest tests\test_tool_loop_agent_runner.py::test_async_generator_tool_results_share_one_tool_call_id -q
# 1 passed

uv run pytest tests\test_tool_loop_agent_runner.py -q
# 30 passed

uv run ruff check astrbot\core\agent\runners\tool_loop_agent_runner.py
# All checks passed

uv run ruff format astrbot\core\agent\runners\tool_loop_agent_runner.py tests\test_tool_loop_agent_runner.py --check
# 2 files already formatted

uv run python -m py_compile astrbot\core\agent\runners\tool_loop_agent_runner.py tests\test_tool_loop_agent_runner.py
# passed

git diff --check
# passed

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

Merge multiple streamed tool execution results for a single tool call into one consolidated tool message.

Bug Fixes:

  • Ensure multiple CallToolResult chunks from async generator tools are combined into a single provider tool message per tool_call_id instead of producing separate messages.

Tests:

  • Add regression test verifying that two streamed CallToolResult yields for one tool_call_id are stored as a single tool message containing both partial results.

@auto-assign auto-assign Bot requested review from Fridemn and anka-afk May 7, 2026 05:35
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels May 7, 2026
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 tool execution logic to aggregate multiple results from a single tool call into one message. By collecting partial results in a list and joining them before final processing, the runner now ensures that streamed or multi-yield tool outputs are associated with a single tool call ID. Additionally, a new test case and a mock executor were added to verify this aggregation behavior. I have no feedback to provide.

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] 异步生成器工具多次 yield 时 tool_result_id 重复,Anthropic API 报 400

1 participant