Image understanding
Send a local image to a vision-capable model and distinguish understanding from generation.
Prepare the image and model
Choose a model supporting image input through Chat Completions and prepare Python with the OpenAI SDK guide. Save an actual PNG as sample.png in the working directory.
Image understanding returns analysis text and is separate from image generation.
Send a local image
import base64
import os
from pathlib import Path
from openai import OpenAI
encoded = base64.b64encode(Path("sample.png").read_bytes()).decode("ascii")
client = OpenAI(api_key=os.environ["IROUTER_API_KEY"], base_url="https://gateway.irouter.io/v1")
reply = client.chat.completions.create(
model=os.environ["IROUTER_MODEL"],
messages=[{"role": "user", "content": [
{"type": "text", "text": "Describe the visible objects."},
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{encoded}"}},
]}],
)
print(reply.choices[0].message.content)The MIME type must match the file. For JPEG use image/jpeg. Base64 increases request size; compress or resize large files according to model limits.
Use a remote image
Where supported, replace image_url.url with an accessible HTTPS image address. Authenticated, expired, or source-restricted URLs may fail to download. Use images you may access and keep sensitive signed URLs out of logs.
Results and costs
Image input consumes model resources; reconcile catalog pricing with usage. Validate extracted text, amounts, and coordinates in your application. Supply a cropped detail when resolution is insufficient. Messages and Gemini use different image structures, so reorganize content when changing protocols.