Skip to content

fix: merge streamed tool results per call - #8050

Open
he-yufeng wants to merge 5 commits into
AstrBotDevs:masterfrom
he-yufeng:fix/async-generator-tool-result
Open

he-yufeng wants to merge 5 commits into
AstrBotDevs:masterfrom
he-yufeng:fix/async-generator-tool-result

Conversation

@he-yufeng

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

Copy link
Copy Markdown
Contributor

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.txt 和 pyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Consolidate streamed tool execution results into a single provider-facing tool message per call.

Bug Fixes:

  • Merge multiple streamed tool results from a single async-generator execution into one tool message associated with the original tool call.
  • Ensure empty, missing, and unsupported tool responses are represented once per tool call.

Enhancements:

  • Preserve existing image handling and tool hook behavior while consolidating streamed results.

Tests:

  • Add regression coverage for merged streamed results and single-message handling of empty and unsupported tool responses.

@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

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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.

@Soulter
Soulter force-pushed the master branch 3 times, most recently from a4c4a7d to 9bd38ca Compare May 28, 2026 16:55
@he-yufeng

Copy link
Copy Markdown
Contributor Author

Gentle ping on this streamed tool-result fix. The branch is meant to keep partial chunks grouped by tool call while still preserving streaming behavior.

The repo checks are green except for the long-running Sourcery review entry, which appears to be an external bot state rather than a test failure. Could a maintainer take a look when convenient?

@he-yufeng

Copy link
Copy Markdown
Contributor Author

顶一下~ 这个把同一次 call 的流式 tool 结果按 call 合并,否则多段结果会串在一起。有空麻烦 review 下,需要的话我可以补个用例。

@he-yufeng

Copy link
Copy Markdown
Contributor Author

Third and last nudge on this one. The runner file moved on 08-08, so the May diff probably needs a real rebase rather than a rubber stamp. The issue itself has not changed conceptually: partial chunks of one tool call can still interleave with another call's chunks in the streamed path, and this groups them per call. If a maintainer is interested I will rebase and re-verify against the new structure; otherwise I will close it next pass and keep the branch around for reference.

buyun14 pushed a commit to buyun14/AstrBot that referenced this pull request Sep 3, 2026
@he-yufeng

Copy link
Copy Markdown
Contributor Author

@camera-2018 9 月 13 号 approve 之后一直没合。现在卡着的只有 Sourcery review 这个 pending 状态,不是真实失败。还需要我这边补什么吗,还是可以直接合?

@he-yufeng

Copy link
Copy Markdown
Contributor Author

@camera-2018 批准一个月了。master(48ba71ad)上 runner 仍然按 chunk 直接 append,同一次 tool call 的流式结果分段到达时还是会串。这个补丁的合并逻辑和测试对当前 master 依旧成立。方便的时候合一下?如果项目现在对这块有别的规划,说一声我先关掉。

…or-tool-result-continue

# Conflicts:
#	astrbot/core/agent/runners/tool_loop_agent_runner.py

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery assessment

Approved.

@he-yufeng

Copy link
Copy Markdown
Contributor Author

Merged current master in 064f560b1. The conflict resolution keeps main's direct executor iteration while preserving the single-tool-call aggregation of streamed results.

Ruff check and format pass on both changed files. Local pytest did not complete because the existing environment hit iCloud read timeouts, and the isolated dependency install was unavailable; I am not claiming a local suite pass. Current-head CI remains the verification gate.

@codecov-commenter

codecov-commenter commented Nov 9, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 80.00000% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...trbot/core/agent/runners/tool_loop_agent_runner.py 80.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@he-yufeng

Copy link
Copy Markdown
Contributor Author

Supplementing the merge note: the complete tests/test_tool_loop_agent_runner.py file now passes 75/75 in a clean local Python 3.14.2 environment, including the streamed multi-result aggregation case.

@he-yufeng

Copy link
Copy Markdown
Contributor Author

@camera-2018 The current head 064f560b1 is mergeable again with the existing approval and green checks. The complete tool-runner file also passes 75/75 locally, including the multi-result aggregation case. Is anything else needed before this can merge?

@sourcery-ai

sourcery-ai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Linked issues

Checked at commit 65bceda

#8028 [Bug] 异步生成器工具多次 yield 时 tool_result_id 重复,Anthropic API 报 400 · 1 of 1 met

# Requirement Status Notes
1 Store one tool_result block for an async-generator tool call that yields multiple CallToolResult values. ✅ Met Results are accumulated during executor iteration and appended once after the loop; the regression test checks one tool message with both partial results.

@he-yufeng

Copy link
Copy Markdown
Contributor Author

Added the missing special-result regressions in 5e2e60d7e, with current master merged at 666233bee.

The new cases cover a tool returning None and an unsupported result type, asserting that each produces exactly one provider tool message. Skipping the final aggregation block makes the multi-yield, None, and unsupported-type cases all fail; restoring it gives 77 passed for the complete tool-runner file. Pre-commit passes on the changed test file.

The new head is running hosted checks now, so the earlier approval still needs to be re-established before merge.

…or-tool-result-continue

# Conflicts:
#	astrbot/core/agent/runners/tool_loop_agent_runner.py
@he-yufeng

Copy link
Copy Markdown
Contributor Author

Resolved the current master conflict in 65bceda94ba0f86dc19401b1ed05b761b98d2b05.

The resolution keeps the current prepare_tool_result() behavior, including cached-image yields, then joins the processed parts once after the async generator finishes. This preserves the upstream image and overflow handling while restoring one provider tool-result per call id.

Current-base red check before the resolution:

2 tool messages with tool_call_id="call_123"
first partial result
second partial result

Resolved head:

tests/test_tool_loop_agent_runner.py
86 passed

All 23 hosted checks now pass, and the PR is mergeable again. Ruff check passes on the production and test files; the production file also passes Ruff format. The only test-only maintenance was the import ordering required by the current Ruff rule set.

This branch has not been deployed

No deployments
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

Status: In Progress

Development

Successfully merging this pull request may close these issues.

[Bug] 异步生成器工具多次 yield 时 tool_result_id 重复,Anthropic API 报 400

3 participants