How to send images to GPT-4o API
Quick answer
To send images to the
gpt-4o API, use the chat.completions.create method with messages containing image_url or image_base64 fields inside the message content. The gpt-4o model supports multimodal inputs by accepting images alongside text in the chat messages.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the official OpenAI Python SDK v1+ and set your API key as an environment variable.
pip install openai>=1.0 Step by step
Use the OpenAI client to send a chat completion request to gpt-4o with an image included in the message content as a URL or base64 string. The model will process the image along with any text prompt.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
messages = [
{
"role": "user",
"content": {
"text": "Describe this image",
"image_url": "https://example.com/path/to/image.jpg"
}
}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
print(response.choices[0].message.content) output
A detailed description of the image content printed to the console.
Common variations
- Use
image_base64instead ofimage_urlto send images encoded in base64. - Send multiple images by including multiple messages or multiple image fields in one message.
- Use async calls with
asyncioandawaitfor non-blocking requests.
import os
import asyncio
from openai import OpenAI
async def main():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
messages = [
{
"role": "user",
"content": {
"text": "Analyze this image",
"image_base64": "<base64-encoded-image-string>"
}
}
]
response = await client.chat.completions.create(
model="gpt-4o",
messages=messages
)
print(response.choices[0].message.content)
asyncio.run(main()) output
Analysis or description of the base64 image printed asynchronously.
Troubleshooting
- If you get an error about unsupported content, verify your message content structure matches the multimodal format with
textandimage_urlorimage_base64. - Ensure your API key has access to
gpt-4oand multimodal features. - Check image URLs are publicly accessible or base64 strings are correctly encoded.
Key Takeaways
- Use the
gpt-4omodel withchat.completions.createto send images as part of chat messages. - Include images via
image_urlorimage_base64fields inside the message content dictionary. - Ensure your API key supports multimodal inputs and your image data is accessible and correctly formatted.