How to beginner · 3 min read

How to send image url to OpenAI vision API

Quick answer
Use the OpenAI Python SDK to send an image URL to the Vision API by including the URL in the message content with the image_url field or embedding the URL in the prompt. Call client.chat.completions.create with a model like gpt-4o that supports vision inputs. The API processes the image from the URL directly.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the official OpenAI Python SDK and set your API key as an environment variable.

  • Run pip install openai to install the SDK.
  • Set your API key in your environment: export OPENAI_API_KEY='your_api_key' (Linux/macOS) or setx OPENAI_API_KEY "your_api_key" (Windows).
bash
pip install openai

Step by step

Use the OpenAI SDK to send a chat completion request with an image URL embedded in the message content. The Vision API-enabled model will fetch and analyze the image from the URL.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

image_url = "https://example.com/path/to/image.jpg"

messages = [
    {
        "role": "user",
        "content": f"Analyze this image: {image_url}"
    }
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=messages
)

print(response.choices[0].message.content)
output
The image shows a beautiful sunset over the mountains with vibrant orange and purple hues.

Common variations

You can also send the image URL as a JSON object in the message content if the API supports explicit image fields, or use other vision-capable models like gpt-4o-mini. Async calls and streaming responses are supported by the SDK but require different method calls.

python
import asyncio
import os
from openai import OpenAI

async def main():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    image_url = "https://example.com/path/to/image.jpg"
    messages = [{"role": "user", "content": f"Analyze this image: {image_url}"}]

    response = await client.chat.completions.acreate(
        model="gpt-4o",
        messages=messages
    )
    print(response.choices[0].message.content)

asyncio.run(main())
output
The image depicts a modern city skyline at night with illuminated skyscrapers.

Troubleshooting

  • If you get an error about unsupported content, verify your model supports vision inputs like gpt-4o.
  • If the image URL is inaccessible, ensure it is publicly reachable and uses HTTPS.
  • Check your API key and environment variable setup if authentication errors occur.

Key Takeaways

  • Use the official OpenAI Python SDK with gpt-4o to send image URLs for vision tasks.
  • Embed the image URL directly in the chat message content to have the model fetch and analyze it.
  • Ensure your image URL is publicly accessible and uses HTTPS to avoid fetch errors.
Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗