Skip to content

Anthropic: Fix message content flattening for grouped consecutive messages #8065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

sbruel
Copy link
Contributor

@sbruel sbruel commented Jun 26, 2025

Pull Request Template

Summary

When consecutive messages from the same role were grouped together, multi-element content arrays (e.g., vision messages with image + text) would become nested arrays that failed Anthropic's API validation. The original flattening logic only handled single-element arrays, leaving multi-element arrays improperly nested.

The messages property would be something like

{
  "content": [
    [
      {
        "source": {
          "data": "...",
          "media_type": "image/png",
          "type": "base64"
        },
        "type": "image"
      },
      {
        "text": "describe this image",
        "type": "text"
      }
    ],
    "prompt text"
  ],
  "role": "user"
}

instead of

{
  "content": [
      {
        "source": {
          "data": "...",
          "media_type": "image/png",
          "type": "base64"
        },
        "type": "image"
      },
      {
        "text": "describe this image",
        "type": "text"
      },
      {
        "text": "prompt text",
        "type": "text"
      }
  ],
  "role": "user"
}
Screen.Recording.2025-06-26.at.14.07.24.mov

Error logs
image

  • Fix "Input should be a valid dictionary or object" error when sending grouped messages with multi-element content arrays to Anthropic API.
  • Improve message content flattening logic to handle both single and multi-element arrays correctly.
  • Ensure raw strings are converted to proper {type: "text", text: "..."} objects.
  • Maintain trailing whitespace trimming for final assistant messages in all content formats.

Change Type

  • Bug fix (non-breaking change which fixes an issue)

Testing

The easiest way I could find to reproduce the bug is shown in the video above.

  • Create a message using and Anthropic model.
  • After the response is returned, fork the original prompt into a new conversation.
  • Write another message so there are 2 consecutive user messages.

Test Configuration:

Checklist

Please delete any irrelevant options.

  • My code adheres to this project's style guidelines
  • I have performed a self-review of my own code
  • I have commented in any complex areas of my code
  • I have made pertinent documentation changes
  • My changes do not introduce new warnings
  • I have written tests demonstrating that my changes are effective or that my feature works
  • Local unit tests pass with my changes
  • Any changes dependent on mine have been merged and published in downstream modules.
  • A pull request for updating the documentation has been submitted.

@danny-avila
Copy link
Owner

api/app/clients/AnthropicClient.js is no longer used after #8013

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