Skip to content

Validate image dimensions before sending to Anthropic Messages API#4688

Draft
bhavyaus wants to merge 1 commit intomainfrom
dev/bhavyau/validate-image-dimensions
Draft

Validate image dimensions before sending to Anthropic Messages API#4688
bhavyaus wants to merge 1 commit intomainfrom
dev/bhavyau/validate-image-dimensions

Conversation

@bhavyaus
Copy link
Copy Markdown
Contributor

Problem

When a tool result (e.g. get_changed_files) includes a base64 image that exceeds Anthropic's 8000px dimension limit, the API returns an opaque 400 error:

400 {"message":"messages.7.content.2.image.source.base64.data: At least one of the image dimensions exceed max allowed size: 8000 pixels"} 

This crashes the entire request with no actionable guidance for the user.

Solution

Add pre-flight dimension validation in rawContentToAnthropicContent() before pushing base64 images to the Anthropic content array:

  • validateImageDimensions() — uses getImageDimensions() from imageUtils.ts to parse the image header and check width/height against the 8000px limit
  • Throws a clear, user-friendly error: "Image dimensions 9000x6000 exceed the maximum allowed size of 8000x8000 pixels. Please resize the image and try again."
  • Gracefully skips validation if image headers are unreadable (lets the API handle those cases)

Testing

  • 4 new unit tests covering oversized width, oversized height, boundary (8000x8000), and small images
  • All 34 tests in messagesApi.spec.ts pass
  • 0 compilation errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant