Claude Image Base64 Decode Failed
Anthropic can't decode your image data. Common causes: forgot to strip data:image/png;base64, prefix, wrong media_type declared, invalid padding, or sent raw bytes instead of base64 string. All fixable in seconds.
The data field must be a pure base64 string — no data:image/png;base64, prefix, no whitespace, no line breaks. Verify with: base64.b64decode(data, validate=True). Set media_type to match the actual image format (JPEG → image/jpeg, not image/jpg).
The Error Messages You're Seeing
HTTP/1.1 400 Bad Request
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "Could not decode image from base64 data"
}
}
Variants:
"messages.0.content.0.source.data: Invalid base64 encoding"
"Invalid image data — magic bytes do not match declared media_type"
"messages.0.content.0.source.data: Value must be a string"
"Image decoded but format is not supported"
What Actually Causes This Error
data:image/png;base64,iVBORw0KGgo... — the prefix must be stripped.image/jpeg but sent PNG bytes. Magic byte mismatch = decode failure.bytes object serializes to JSON as an array or fails. Must decode to str first.= padding at end causes silent corruption.Fixes That Work (Tested Nov 2026)
1Correct Base64 Preparation (Python)
2Strip Data URL Prefix (Browser/Node)
1. FileReader.readAsDataURL() ALWAYS returns with prefix — must strip
2. Browser canvas.toDataURL() also includes prefix
3. Some CDNs return images as data URLs when queried via fetch — check!
4. Media type image/jpg is technically invalid — use image/jpeg
3Validate Before Sending
Preventing This Error Going Forward
- Always run base64 through
validate=True. Catches issues early. - Detect media_type from magic bytes. Don't trust file extensions or user input.
- Never trust FileReader output directly. Always strip the data URL prefix.
- Use
image/jpeg, notimage/jpg. The RFC-standard media type. - Validate on the client too. Fail fast before making expensive API calls.
Researcher · AI Error Hub
Frequently Asked Questions
File extensions and MIME types are different. The IANA-registered MIME type is image/jpeg. Most APIs accept both, but standards-compliant servers (like Anthropic) may reject image/jpg. Always use image/jpeg.
Not in the Messages API — JSON requires string data. If you want to avoid base64 overhead, use the Files API (beta) which accepts multipart uploads. Otherwise, base64 is required and adds ~33% size.
EXIF is stripped or ignored during Claude's processing. Rotation from EXIF metadata is NOT applied — some images may appear sideways to Claude. Apply EXIF rotation before uploading if it matters (Pillow: ImageOps.exif_transpose(img)).
No, not directly. Rasterize SVG to PNG or JPEG client-side first. Libraries: cairosvg (Python), @resvg/resvg-js (Node). Text in SVG becomes pixels — Claude reads it via OCR, not as text.
Not supported. Convert to JPEG. Python: pillow-heif plugin. Node: heic-convert. Server-side conversion recommended — don't ship HEIC to browsers either (Safari-only support).
Related Errors
Master Claude Vision Pipelines
Weekly deep dives on image processing, OCR, and multimodal patterns. 12,000+ developers subscribe.
Subscribe Free →