Skip to content

fix(anthropic): consecutive assistant message merge drops content on mixed types#1703

Merged
teknium1 merged 1 commit intomainfrom
fix/anthropic-adapter-merge-content-loss
Mar 17, 2026
Merged

fix(anthropic): consecutive assistant message merge drops content on mixed types#1703
teknium1 merged 1 commit intomainfrom
fix/anthropic-adapter-merge-content-loss

Conversation

@teknium1
Copy link
Copy Markdown
Contributor

Summary

In agent/anthropic_adapter.py, when merging consecutive assistant messages (required by Anthropic's API which rejects consecutive same-role messages), the mixed-types case (one message has string content, the other has list content) simply replaced the earlier message with the later one:

# Before (line 967):
fixed[-1] = m  # Earlier message content silently dropped

This caused data loss when the conversation had consecutive assistant messages with different content formats.

What changed

Applied the same normalize-and-merge pattern already used in the tool_use merge path (lines 952-956): convert both to [{"type": "text", "text": ...}] list format before concatenating.

# After:
if isinstance(prev_blocks, str):
    prev_blocks = [{"type": "text", "text": prev_blocks}]
if isinstance(curr_blocks, str):
    curr_blocks = [{"type": "text", "text": curr_blocks}]
fixed[-1]["content"] = prev_blocks + curr_blocks

Test plan

  • Trigger consecutive assistant messages with mixed content types (e.g. tool call producing list content followed by text response)
  • Verify both messages' content is preserved in the merged result
…nt types

When two consecutive assistant messages had mixed content types (one
string, one list), the merge logic just replaced the earlier message
entirely with the later one (fixed[-1] = m), silently dropping the
earlier message's content.

Apply the same normalization pattern used in the tool_use merge path
(lines 952-956): convert both to list format before concatenating.
This preserves all content from both messages.
@teknium1 teknium1 merged commit 75c5136 into main Mar 17, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant