How to beginner · 3 min read

How to send multiple images to Gemini API

Quick answer
To send multiple images to the Gemini API, use the OpenAI Python SDK's client.chat.completions.create method with the gemini-1.5-flash model, including each image as a separate message with role set to user and content containing the image data or URL. The images should be passed as part of the messages array in the request.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key
  • 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 gemini-1.5-flash model and send multiple images by including them as separate messages in the messages list. Each image can be represented as a base64 string or a URL in the content field.

python
import os
from openai import OpenAI

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

# Example images as URLs or base64 strings
images = [
    "https://example.com/image1.png",
    "https://example.com/image2.jpg"
]

# Prepare messages with images
messages = []
for idx, img_url in enumerate(images, start=1):
    messages.append({
        "role": "user",
        "content": f"[Image {idx}] {img_url}"
    })

response = client.chat.completions.create(
    model="gemini-1.5-flash",
    messages=messages
)

print(response.choices[0].message.content)
output
AI response text based on the multiple images sent

Common variations

  • Using base64 images: Instead of URLs, encode images as base64 strings and include them in the content field.
  • Async calls: Use async/await with the OpenAI SDK for non-blocking calls.
  • Different Gemini models: You can switch model to gemini-2.0-flash or others as needed.
python
import asyncio
import os
from openai import OpenAI

async def send_images_async():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    images = ["https://example.com/image1.png", "https://example.com/image2.jpg"]
    messages = [{"role": "user", "content": f"[Image {i+1}] {url}"} for i, url in enumerate(images)]

    response = await client.chat.completions.acreate(
        model="gemini-1.5-flash",
        messages=messages
    )
    print(response.choices[0].message.content)

asyncio.run(send_images_async())
output
AI response text based on the multiple images sent

Troubleshooting

  • If you get an error about invalid message format, ensure each image is included as a separate message with role set to user and content properly formatted.
  • If the API does not recognize the images, verify the URLs are accessible or base64 strings are correctly encoded.
  • Check your API key environment variable if authentication errors occur.

Key Takeaways

  • Use the OpenAI Python SDK with gemini-1.5-flash to send multiple images as separate messages.
  • Images can be URLs or base64-encoded strings included in the content field of each message.
  • Always set your API key in os.environ["OPENAI_API_KEY"] and never hardcode it.
  • Async calls are supported for non-blocking image sending with the Gemini API.
  • Validate image accessibility and message formatting to avoid common errors.
Verified 2026-04 · gemini-1.5-flash, gemini-2.0-flash
Verify ↗