Skip to content

fix(anthropic): serialize ToolResultContent::Image with source wrapper#1772

Merged
gold-silver-copper merged 1 commit into
0xPlaygrounds:mainfrom
evolvevo:fix/anthropic-tool-result-image-serde
May 16, 2026
Merged

fix(anthropic): serialize ToolResultContent::Image with source wrapper#1772
gold-silver-copper merged 1 commit into
0xPlaygrounds:mainfrom
evolvevo:fix/anthropic-tool-result-image-serde

Conversation

@Cyanistic
Copy link
Copy Markdown
Contributor

Description

Fixes incorrect serialization of ToolResultContent::Image when sending tool results containing images to the Anthropic API. Any tool that returns image content (screenshots, image generation, etc.) was broken.

Fixes #1771

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Testing

  • Confirmed bug with live Anthropic API using upstream rig-core 0.37.0400 Bad Request with error: Input tag 'base64' found using 'type' does not match any of the expected tags
  • Applied fix and re-tested with a tool-calling agent → 200 OK, Claude correctly described the image
  • Added roundtrip test: deserialize a tool_result message with image content, re-serialize, assert structure matches Anthropic's expected format
cargo test -p rig-core -- providers::anthropic  # all tests pass
cargo check --workspace                         # zero errors

Checklist:

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Notes

The fix changes ToolResultContent::Image from a tuple variant to a struct variant with a source field:

// Before (broken — flattens ImageSource fields, "type" collision)
Image(ImageSource),

// After (correct — matches Content::Image pattern)
Image { source: ImageSource },

This is technically a breaking change to the Anthropic-specific type API (anyone pattern-matching on ToolResultContent::Image(source) needs to change to ToolResultContent::Image { source }), but since the previous serialization was completely non-functional with the Anthropic API, no one could have been using it successfully.

The Anthropic API expects tool result image content in the format:
{"type": "image", "source": {"type": "base64", ...}}

Previously, ToolResultContent::Image was a tuple variant wrapping
ImageSource directly. With #[serde(tag = "type")] internal tagging,
this flattened ImageSource's fields (including its own "type" field)
into the parent object, producing {"type": "base64", ...} which
Anthropic rejects with:

  Input tag 'base64' found using 'type' does not match any of the
  expected tags: 'document', 'image', 'search_result', 'text',
  'tool_reference'

Changed to a struct variant with a "source" field, matching the
pattern already used by Content::Image.
@gold-silver-copper
Copy link
Copy Markdown
Contributor

awesome work ty!

@gold-silver-copper gold-silver-copper added this pull request to the merge queue May 16, 2026
Merged via the queue into 0xPlaygrounds:main with commit a882dbe May 16, 2026
5 checks passed
@github-actions github-actions Bot mentioned this pull request May 15, 2026
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.

bug: ToolResultContent::Image serialized incorrectly for Anthropic API — produces "type": "base64" instead of "type": "image"

2 participants