API guides
Images & vision
Send an image together with a question to a model that supports vision. Image inputs use content blocks; selecting a text-only model will not add image understanding.
Prepare an image
Choose a vision-capable model and an image you are permitted to share with the provider. The example reads a local PNG and encodes it as a data URL, avoiding the need to publish it on a public host.
- Match the MIME type to the actual file type. This example expects image.png to be a PNG.
- Provider limits on file size, image dimensions, and image count vary. Resize large images and consult the selected model's limits.
- Avoid credentials, personal information, or confidential material in example screenshots.
Send an image
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.straitly.ai/v1",
api_key=os.environ["STRAITLY_API_KEY"],
)
import base64
from pathlib import Path
encoded = base64.b64encode(Path("image.png").read_bytes()).decode("ascii")
response = client.chat.completions.create(
model="claude-sonnet-5",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "Describe this image."},
{"type": "image_url", "image_url": {
"url": "data:image/png;base64," + encoded,
}},
],
}],
)
print(response.choices[0].message.content)URLs, costs, and results
- For models accepting remote image URLs, the URL must be reachable by the provider without your browser session cookies. Never put secrets into the URL.
- Image token accounting is model-specific. Inspect usage and the recorded charge rather than estimating from the base64 string length.
- Vision output can be wrong. Validate extracted values and use human review for consequential decisions.
- The Anthropic Messages API uses its own image source block format. Do not paste Chat Completions image_url blocks into a Messages request.