How to send images to Claude API
Quick answer
To send images to the Claude API, use the Anthropic SDK with the
tools parameter specifying a type of image_upload_20231018. Include the image as a base64-encoded string in the message content or as a file upload, then call client.messages.create with the appropriate model and betas flags for multimodal support.PREREQUISITES
Python 3.8+Anthropic API keypip install anthropic>=0.20
Setup
Install the Anthropic Python SDK and set your API key as an environment variable.
pip install anthropic>=0.20 Step by step
This example shows how to send an image to Claude using the image_upload_20231018 tool type. The image is base64-encoded and included in the message content. The betas parameter enables multimodal features.
import os
import base64
import anthropic
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
# Load and base64-encode the image
with open("example.png", "rb") as f:
image_bytes = f.read()
image_b64 = base64.b64encode(image_bytes).decode("utf-8")
# Define the tool for image upload
tools = [{
"type": "image_upload_20231018",
"name": "image_upload",
"description": "Upload an image for multimodal input"
}]
# Compose the message with the base64 image
messages = [{
"role": "user",
"content": f"data:image/png;base64,{image_b64}"
}]
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
messages=messages,
tools=tools,
betas=["multimodal-2024-10-18"],
max_tokens=1024
)
print(response.content[0].text) output
Hello! I received your image. How can I assist you with it?
Common variations
- Use async calls with
client.messages.acreatefor asynchronous workflows. - Send images as URLs if supported by your use case instead of base64 encoding.
- Switch models to other Claude versions that support multimodal input by checking the latest Anthropic docs.
Troubleshooting
- If you get an error about missing
betas, ensure you includebetas=["multimodal-2024-10-18"]to enable image support. - Check your API key and environment variables if authentication fails.
- Verify the image is correctly base64-encoded and the content string starts with
data:image/.
Key Takeaways
- Use the Anthropic SDK's
toolsparameter withimage_upload_20231018to send images to Claude. - Always include the
betasflagmultimodal-2024-10-18to enable multimodal capabilities. - Encode images as base64 strings prefixed with the data URI scheme in the message content.
- Check the latest Anthropic docs for updated model names and beta feature flags.
- Use async methods for scalable or concurrent image processing workflows.