How to send image to OpenAI API in python
Direct answer
Use the OpenAI Python SDK's
client.chat.completions.create method with the image_url or image_base64 field in the message content to send images to the OpenAI API.Setup
Install
pip install openai Env vars
OPENAI_API_KEY Imports
import os
from openai import OpenAI Examples
inSend a PNG image URL to GPT-4o for caption generation.
outA descriptive caption of the image returned by the model.
inSend a base64-encoded JPEG image to GPT-4o for analysis.
outTextual analysis or description of the image content.
inSend an invalid image URL to GPT-4o.
outAPI error response indicating invalid image input.
Integration steps
- Install the OpenAI Python SDK and set your API key in the environment variable OPENAI_API_KEY.
- Import the OpenAI client and initialize it with the API key from os.environ.
- Prepare the message content including the image data as a URL or base64 string in the message.
- Call
client.chat.completions.createwith the appropriate model and messages array. - Extract the response text from
response.choices[0].message.content.
Full code
import os
from openai import OpenAI
# Initialize client with API key from environment
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Example: Sending an image URL in the message content
image_url = "https://example.com/image.png"
messages = [
{
"role": "user",
"content": f"Here is an image for analysis: {image_url}"
}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
print("Response:", response.choices[0].message.content) API trace
Request
{"model": "gpt-4o", "messages": [{"role": "user", "content": "Here is an image for analysis: https://example.com/image.png"}]} Response
{"choices": [{"message": {"content": "This image shows a beautiful sunset over the mountains..."}}], "usage": {"total_tokens": 50}} Extract
response.choices[0].message.contentVariants
Send base64-encoded image data ›
Use this when you want to send image data directly without hosting it on a public URL.
import os
from openai import OpenAI
import base64
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Load image and encode to base64
with open("image.jpg", "rb") as img_file:
b64_string = base64.b64encode(img_file.read()).decode("utf-8")
messages = [
{
"role": "user",
"content": f"data:image/jpeg;base64,{b64_string}"
}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
print("Response:", response.choices[0].message.content) Streaming response for image analysis ›
Use streaming to get partial results immediately for large or complex image analysis tasks.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
image_url = "https://example.com/image.png"
messages = [{"role": "user", "content": f"Analyze this image: {image_url}"}]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
stream=True
)
for chunk in response:
print(chunk.choices[0].message.content, end='') Performance
Latency~1-3 seconds for typical image input processing with gpt-4o
Cost~$0.03 per 1,000 tokens for gpt-4o; image inputs may increase token usage
Rate limitsTier 1: 500 requests per minute / 30,000 tokens per minute
- Compress or resize images before encoding to reduce token count.
- Use concise prompts to minimize token usage.
- Cache frequent image analyses to avoid repeated calls.
| Approach | Latency | Cost/call | Best for |
|---|---|---|---|
| Image URL in message content | ~1-3s | ~$0.03 per 1k tokens | Quick image references with public URLs |
| Base64-encoded image data | ~2-4s | ~$0.04 per 1k tokens | Direct image data without hosting |
| Streaming response | Starts ~1s, continues | ~$0.03 per 1k tokens | Long or complex image analysis with partial results |
Quick tip
Always encode images as base64 or provide a publicly accessible URL when sending images to the OpenAI API in Python.
Common mistake
Beginners often forget to encode binary images to base64 or provide accessible URLs, causing the API to reject the image input.