Sub topic 1
How multimodal models read images and documents
This chapter builds the working model for the rest of the tutorial. It explains what a multimodal model does with an image or a PDF, why that matters for cost and accuracy, and which mistakes beginners make most often. Later chapters cover the request formats and code in detail.
Prerequisites
This chapter assumes you have:
- Basic Python, including reading files and working with bytes.
- Experience calling an LLM chat API over HTTP or through an SDK.
- Familiarity with JSON request bodies.
- A rough idea of what tokens and context windows are.
You do not need any computer vision background.
What "multimodal" means in practice
A modality is a kind of input, such as text, images, or audio. A multimodal model accepts more than one kind in the same prompt. In this tutorial, "multimodal model" means a language model that accepts images and documents as input and replies with text.
The model does not hold pictures in memory as pictures. An image encoder converts the pixels into a sequence of numeric vectors. These vectors play the same role as token embeddings for text. The language model then reads text tokens and image vectors together in one sequence and generates a text answer.
Three consequences follow from this design:
- An image takes up room in the context window, like text does. You pay for it and wait for it.
- The model reasons over the whole sequence. It can compare a sentence in your prompt with a region of an image.
- The output is usually text. Most providers' chat models read images but do not return images.
How an image becomes tokens
Providers differ in the exact method, but the usual steps are:
- The service may resize the image so it fits inside a maximum size.
- The image is split into a grid of small square patches.
- An encoder turns each patch into a vector.
- The vectors are placed in the prompt sequence at the position where the image block appears.
The number of patches grows with the image area. A larger image therefore costs more tokens, up to a cap set by the provider. Above the cap, extra pixels are discarded by downscaling, so you pay in upload time and gain nothing in detail.
This is why resizing before upload (covered in a later chapter) is useful. It cuts payload size and latency, and it often leaves the cost unchanged.
What the model sees, and what it does not
The model sees a resized, compressed version of what you send. It does not see file metadata or the original resolution. It also does not run a conventional OCR (optical character recognition) program first. It reads text in an image the same way it reads other visual patterns. This usually works well for clear print. It produces errors when the text is small, blurred, rotated, or crowded.
Typical strengths:
- Describing scenes and layouts.
- Reading charts, tables, and screenshots.
- Reading printed and often handwritten text.
- Answering questions that combine the picture with your instructions.
Typical weaknesses:
- Small text. If a character covers only a few pixels after resizing, the model guesses.
- Precise counting of many similar objects.
- Exact measurements, coordinates, and spatial relations.
- Fine numeric detail in dense charts or tables.
- Very low-quality scans.
The model's guesses are fluent. A misread number looks as plausible as a correct one, so the output gives no hint that something went wrong. Later chapters on failure modes and evaluation show how to catch this.
How documents differ from images
A PDF is not one thing. It can contain real text objects, embedded images, vector graphics, or only a scanned photograph of each page. Providers that accept PDFs handle this in one of two broad ways. Check your provider's documentation to see which applies.
- Text plus page images. The service extracts any embedded text from each page and also renders each page as an image. The model receives both. This helps with charts, diagrams, and layout, and costs more tokens than text alone.
- Page images only. Every page is treated as a picture. This is simpler, and it is the only option for scanned files with no text layer.
Either way, a PDF costs roughly the sum of its pages. A 40-page report can use far more of the context window than a single photo. Page limits and size limits also apply, and they vary by provider.
You also have other options before sending anything. You can extract the text yourself with a library and send only text. You can render pages to images and send chosen pages. A later chapter compares all three routes (native PDF, page images, text extraction) and gives decision rules.
Content blocks: the message format
Most provider APIs represent a message as a list of typed content blocks instead of one string. A text block carries words. An image block carries a picture. A document block carries a file such as a PDF. The model receives the blocks in order.
The block names differ by provider, but the pattern is the same. Here is a simplified, provider-neutral request body.
{
"role": "user",
"content": [
{"type": "image", "source": "<image data or reference>"},
{"type": "text", "text": "What is the total on this receipt?"}
]
}Three points about this structure:
- Order matters. Many providers report better results when images come before the question, though you should check the guidance for your model.
- One message can hold several images and several text blocks. You can label each image with a short text block before it.
- The image data itself can be supplied in different ways: inline base64 text, a public URL, or a reference to a file you uploaded earlier. The chapter on supplying data compares these.
Cost and latency, in outline
Every visual input adds tokens. Tokens affect three things:
| Effect | What happens |
|---|---|
| Price | Input tokens are billed, and images count as input tokens. |
| Latency | More input tokens mean a longer wait before the first output token. |
| Context room | Images and pages consume space that your text and conversation history also need. |
The practical rule is to send the smallest input that still lets a person read what you care about. If you cannot read a figure on the image at the size you send, the model probably cannot either.
Common mistakes at this stage
- Treating the model as OCR. It reads text well in many cases, but it is not a deterministic transcription tool.
- Sending huge originals. A 12-megapixel photo is downscaled anyway. You pay in upload time and gain no detail.
- Cropping too little. If the answer sits in a small region, send a crop of that region. The crop gets more of the token budget than the full image would.
- Assuming every model accepts every input. Image support, PDF support, formats, and limits vary by model and provider. Look them up for the model you use.
- Forgetting that a PDF may have no text layer. A scanned PDF looks the same as a digital one when you open it, but text extraction on it returns nothing.
- Putting sensitive material into prompts without thinking. Images can contain faces, addresses, and account numbers that you did not mean to share. Later chapters cover handling these.
A quick check you can run
This short script helps you spot whether a PDF has a text layer. It uses PyMuPDF, which you can install with pip install pymupdf. Pages with no extractable text are probably scans, so a text-only route would fail on them.
import fitz # PyMuPDF
def pages_without_text(pdf_path):
"""Return the 1-based page numbers that have no extractable text."""
empty = []
with fitz.open(pdf_path) as doc:
for number, page in enumerate(doc, start=1):
if not page.get_text().strip():
empty.append(number)
return empty
empty_pages = pages_without_text("report.pdf")
if empty_pages:
print("Pages with no text layer:", empty_pages)
else:
print("Every page has extractable text.")Run it on a few of your own PDFs. A result listing many pages tells you that page images or native PDF input is the safer route for that file.
Summary of the working model
- A multimodal model converts images into vectors and reads them in the same sequence as your text.
- Image and page inputs cost tokens, so size and page count drive price and latency.
- Reading is statistical. Small or blurry details are guessed, and wrong guesses look confident.
- PDFs may contain text, images, or both. What the provider extracts and renders decides what the model sees.
- Messages are lists of typed blocks, so you can combine text, images, and documents in one request.
Exercise
A colleague sends a 6000x4000 pixel photo of a receipt to a vision model. The model reads the total as 18.60, but the receipt says 16.60. Name two likely causes related to how the model reads images, and one change that could help.
Show answer
Likely causes: the service downscaled the photo, so the digits became only a few pixels wide and the model guessed; and the model reads text statistically, so a misread digit still looks plausible. Helpful change: crop the image to the area with the total (or raise legibility by sending a clearer, appropriately sized image) so the digits get more of the token budget. Also verify the number against a second source, such as the sum of the line items.
Exercise
You have a PDF that is a scan of a signed contract. Why would extracting its text with a library and sending only that text probably fail, and which input route fits better?
Show answer
A scan contains only page pictures and no text layer, so a text extraction library returns empty or near-empty text. Sending the PDF natively (if the provider supports it) or rendering the pages to images and sending those fits better, because the model can read the printed words from the visuals.