Skip to content

Coalesce consecutive Anthropic tool results into one user message - #446

Open
Pybsama wants to merge 2 commits into
huggingface:mainfrom
Pybsama:coalesce-anthropic-tool-results
Open

Pybsama wants to merge 2 commits into
huggingface:mainfrom
Pybsama:coalesce-anthropic-tool-results

Conversation

@Pybsama

@Pybsama Pybsama commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Updated Transcript.toAnthropicMessages() in Sources/AnyLanguageModel/Models/AnthropicLanguageModel.swift so consecutive .toolOutput entries are coalesced into a single role: "user" message containing all tool_result blocks, matching how in-flight tool continuation already sends them.
  • Anthropic's Messages API requires every tool_use block in a preceding assistant message to have its matching tool_result block in the immediately following single user message. Previously, replaying a transcript with parallel tool calls (on a subsequent turn, after a stopped parallel tool round, or during DynamicProfile continuation with a history transform) emitted separate consecutive user messages with one tool_result each.
  • Non-tool_result user messages (such as follow-up or pending prompts) remain separate user messages, preserving existing behavior.

Testing

  • Added cross-platform regression tests in AnthropicToolResultReplayTests (Tests/AnyLanguageModelTests/AnthropicToolContinuationTests.swift):
    • parallelToolOutputsReplayInOneUserMessage(streaming:) (reproduced failure pre-fix for both false and true)
    • parallelToolOutputsWithDynamicProfileContinueInOneUserMessage(streaming:)
    • stoppedParallelToolCallsReplayOutputsInOneUserMessage() (reproduced failure pre-fix)
  • Ran swift format lint --strict --recursive . (PASS)
  • Ran swift test --disable-index-store --filter "AnthropicToolResultReplayTests|AnthropicReasoningTests|AnthropicProfileTests|StoppedToolCallReplayTests" (PASS)
  • Ran swift test --skip-build --disable-index-store --skip OllamaLanguageModelTests (1,113 tests in 115 suites PASS)

@mattt

mattt commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Hi @Pybsama. Thanks for this! One thing before we merge: the DynamicProfile test passes on main too, because the identity transform keeps the current round, so the request reuses the in-flight messages that are already grouped. Could you have the transform edit one tool output's text (keeping both IDs), and assert that the next request contains the edited text and both results, in order, in a single user message?

@Pybsama

Pybsama commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @mattt for spotting that! Updated parallelToolOutputsWithDynamicProfileContinueInOneUserMessage in ca4c61a so the historyTransform edits call_2's text to "REDACTED" while keeping both IDs, and asserts that the continuation request sends both tool_result blocks in order with the edited text in a single user message. Verified the updated test fails on main (both streaming: false and streaming: true) and passes with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants