Sub topic 3
Supported Formats and Size Limits
This chapter assumes you can call an LLM chat API from Python, read files as bytes, and read JSON request bodies. You should also know roughly what a token and a context window are. Here you learn what a provider accepts before you build a request, and how to check a file locally so you catch problems before the API does.
Why limits matter
A multimodal API checks every image or document before the model sees it. If the file is the wrong type or too large, the request fails with an error, usually an HTTP 400. Checking locally is faster and cheaper than waiting for that error. It also lets you resize or convert a file instead of dropping it.
Typical image formats
Most providers accept the same small set of image formats:
- JPEG (
image/jpeg) - PNG (
image/png) - GIF (
image/gif), often only the first frame is used - WebP (
image/webp)
Formats such as TIFF, BMP, HEIC and SVG are commonly not supported. Convert them to PNG or JPEG first. The next chapters show how to resize and normalize images.
Use PNG for screenshots, charts and text, because it is lossless. Use JPEG for photos, because the files are smaller.
Typical limits
Limits differ by provider and change over time. The table shows the kinds of limits you will meet. The values are examples of the pattern only. Read your provider's documentation for the real numbers.
| Limit | What it controls | Typical effect when exceeded |
|---|---|---|
| File size per image | Bytes of the image or its base64 text | Request rejected |
| Total request size | All images, documents and text together | Request rejected |
| Images per request | Count of image blocks | Request rejected |
| Image dimensions | Width and height in pixels | Rejected, or silently downscaled |
| PDF pages | Pages in one document | Rejected, or extra pages ignored |
| PDF file size | Bytes of the document | Request rejected |
Two points are easy to miss. First, base64 text is about one third larger than the original bytes, so a file just under a size limit can go over it once encoded. Second, some providers downscale large images without telling you. The model then sees fewer pixels than you sent, and small text may become unreadable.
Checking a file before you send it
The function below checks the media type and file size. It uses only the standard library. The limit values are placeholders that you must replace.
import mimetypes
import os
ALLOWED_TYPES = {"image/jpeg", "image/png", "image/gif", "image/webp"}
MAX_IMAGE_BYTES = 5 * 1024 * 1024 # placeholder: use your provider's limit
def check_image(path):
"""Raise ValueError if the file is unlikely to be accepted."""
media_type, _ = mimetypes.guess_type(path)
if media_type not in ALLOWED_TYPES:
raise ValueError(f"Unsupported type {media_type} for {path}")
size = os.path.getsize(path)
# base64 text is about 4/3 the size of the raw bytes
if size * 4 / 3 > MAX_IMAGE_BYTES:
raise ValueError(f"{path} is too large after base64 encoding")
return media_type
print(check_image("chart.png"))The check uses the file extension, which can be wrong. A file named photo.png may really be a JPEG. To be safe, open it with Pillow and read img.format. Pillow is a Python image library.
Where the checks fit
Common mistakes
- Trusting the file extension instead of the file contents.
- Measuring the raw file size and forgetting the base64 overhead.
- Assuming an accepted image was also read at full resolution.
- Copying limits from a blog post that is out of date.
Exercise
A provider limits each image to 5 MB of base64 text. Your PNG is 4 MB on disk. Will it pass, and why?
Show answer
No. Base64 is about one third larger than the raw bytes, so 4 MB becomes about 5.3 MB, which is over the 5 MB limit. Resize or compress the image first.