Skip to content

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

Open
Pybsama wants to merge 1 commit into
huggingface:mainfrom
Pybsama:coalesce-anthropic-tool-results
Open

Pybsama wants to merge 1 commit 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)

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.

1 participant