How to beginner · 3 min read

How to analyze image with Gemini in python

Quick answer
Use the OpenAI Python SDK with the gemini-1.5-pro model to analyze images by sending the image URL or base64 data in the messages parameter with image_url or image_base64. The response contains the model's analysis or caption of the image.

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.

bash
pip install openai>=1.0

Step by step

This example shows how to analyze an image by providing its URL to the gemini-1.5-pro model using the OpenAI Python SDK v1.

python
import os
from openai import OpenAI

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

response = client.chat.completions.create(
    model="gemini-1.5-pro",
    messages=[
        {
            "role": "user",
            "content": "Analyze this image and describe its contents.",
            "image_url": "https://example.com/path/to/image.jpg"
        }
    ]
)

print("Analysis result:", response.choices[0].message.content)
output
Analysis result: A scenic mountain landscape with a clear blue sky, pine trees, and a lake reflecting the mountains.

Common variations

  • Use image_base64 instead of image_url to send image data directly.
  • Switch to gemini-2.0-flash for faster responses with slightly different capabilities.
  • Use async calls with asyncio for concurrent image analysis.
python
import os
import asyncio
from openai import OpenAI

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

async def analyze_image():
    response = await client.chat.completions.acreate(
        model="gemini-1.5-pro",
        messages=[
            {
                "role": "user",
                "content": "Describe this image.",
                "image_url": "https://example.com/path/to/image.jpg"
            }
        ]
    )
    print("Async analysis result:", response.choices[0].message.content)

asyncio.run(analyze_image())
output
Async analysis result: A close-up photo of a red rose with dew drops on its petals.

Troubleshooting

  • If you get an authentication error, verify your OPENAI_API_KEY environment variable is set correctly.
  • If the image URL is inaccessible, ensure it is publicly reachable or use base64 encoding.
  • For unexpected model errors, check you are using a supported model name like gemini-1.5-pro.

Key Takeaways

  • Use the OpenAI Python SDK v1 with model gemini-1.5-pro for image analysis.
  • Send images via image_url or image_base64 in the messages payload.
  • Async calls enable concurrent image processing for better performance.
  • Verify environment variables and image accessibility to avoid common errors.
Verified 2026-04 · gemini-1.5-pro, gemini-2.0-flash
Verify ↗