Sub topic 10
Working with images, audio, and documents
Multimodal means a model accepts or produces more than text. This chapter assumes you know how a model turns a prompt into text, how to write prompts, and how to validate structured output. Those skills carry over directly, because most multimodal features are text tasks with a different input.
Common patterns
Most multimodal applications fall into a few patterns.
| Input | Typical task | Example |
|---|---|---|
| Image | Describe, classify, read text | Check whether a photo shows damage |
| Scanned document | Extract fields | Read vendor and total from an invoice |
| Audio | Transcribe, then process | Summarize a recorded meeting |
| Screenshot or chart | Answer questions | Explain a dashboard to a user |
Some models take images or audio directly in the prompt. Others need a separate step first, such as speech-to-text or optical character recognition (OCR), which converts pictures of text into plain text.
Two ways to build the pipeline
You can pass the raw file to a multimodal model, or convert it to text first and send only text.
- Direct: simpler, and the model sees layout, handwriting, and images. Cost and latency are higher, and you cannot inspect what the model "read".
- Convert first: you can check the intermediate text, reuse text tools such as search, and often pay less. Errors in the conversion step pass silently downstream.
Reusing validation
Treat extracted fields from a scan like any other model output. The reply is still text, so parse and check it before use.
import json
def parse_invoice(raw: str) -> dict:
data = json.loads(raw)
if not isinstance(data.get("vendor"), str):
raise ValueError("vendor must be a string")
if not isinstance(data.get("total"), (int, float)):
raise ValueError("total must be a number")
return data
print(parse_invoice('{"vendor": "Acme", "total": 129.5}'))The same function works whether the text came from an OCR step or from a model reading the image directly.
Common errors
- Low-quality input. Blurry photos, background noise, and skewed scans lower accuracy. Check input quality before you blame the prompt.
- Misread numbers. Models can swap digits or misread a decimal point. Cross-check totals, such as line items against the sum.
- Hallucinated content. A model may describe text or objects that are not there. Ask it to answer "not visible" when it cannot tell.
- Large files. Images, long audio, and many-page documents consume the context limit and raise cost. Split them and process the parts.
- Sensitive data. Faces, voices, and ID documents are personal data. Check where the file is sent and stored.
Exercise
You extract the total from scanned invoices and sometimes get a wrong number that still parses as valid JSON. Name one check, beyond type validation, that would catch it.
Show answer
Cross-check the total against the line items. If the sum of the extracted line items does not match the total, flag the invoice for human review. A second option is to compare the result against a separate OCR pass.