Sub topic 16
Testing Your Pipeline
This chapter assumes you can build image and PDF requests, you know the failure modes from the previous chapters, and you understand tokens and context windows. Here you will test a visual input pipeline so you can change prompts, models, or preprocessing settings without guessing whether quality dropped.
Why visual pipelines need their own tests
Model output is not deterministic, and a run that looks right on one image proves little. Two things go wrong often. A change such as a smaller MAX_SIDE can quietly make small text unreadable. A model upgrade can change how it formats answers. Both break extraction without raising an error. A small test set with a scoring script catches them.
Build a test set
Collect 20 to 50 real inputs that match your production files. Include the hard cases you found earlier: small text, rotated scans, low contrast, and multi-page PDFs. For each input, write the expected output by hand and store it next to the file.
- Keep one JSON file per input with the fields you expect.
- Mark any field that is sensitive, and use redacted or synthetic copies if the originals must not leave your systems.
- Add a new case every time you find a production failure.
Two kinds of checks
Separate checks that need no model from checks that do.
- Offline checks run on your own code and are free. Test that
load_as_base64rejects unknown file types, thatdownscalekeeps the longest side at or below the limit, and thatrender_pagesreturns one PNG per page. - Live checks call the model and compare its answer with the expected values. They cost tokens, so run them before releases and when you change a prompt, model, or preprocessing step.
import json
import mimetypes
import tempfile
from pathlib import Path
from PIL import Image
MAX_SIDE = 1568
def downscale(src_path, dst_path, max_side=MAX_SIDE):
with Image.open(src_path) as img:
img = img.convert("RGB")
img.thumbnail((max_side, max_side))
img.save(dst_path, format="JPEG", quality=90)
def test_downscale_limits_longest_side():
with tempfile.TemporaryDirectory() as tmp:
src = Path(tmp) / "big.png"
dst = Path(tmp) / "small.jpg"
Image.new("RGB", (4000, 2000), "white").save(src)
downscale(src, dst)
with Image.open(dst) as out:
assert max(out.size) <= MAX_SIDE
assert out.size[0] == 2 * out.size[1] # aspect ratio kept
def score_fields(expected, actual):
"""Return the share of expected fields the model got exactly right."""
if not expected:
return 1.0
correct = sum(1 for k, v in expected.items() if actual.get(k) == v)
return correct / len(expected)
def test_score_fields():
expected = {"total": "42.50", "currency": "EUR"}
assert score_fields(expected, {"total": "42.50", "currency": "USD"}) == 0.5
assert score_fields(expected, json.loads('{"total": "42.50", "currency": "EUR"}')) == 1.0
test_downscale_limits_longest_side()
test_score_fields()
print("all tests passed")Score what matters
Compare fields, not whole answers. Normalize values first, for example by stripping whitespace and unifying number formats, so that 42.5 and 42.50 do not count as different. Track accuracy per field. A drop in one field, such as dates, points to a specific cause.
Also record the input tokens and the latency for each case. A change that keeps accuracy but doubles token cost is a regression too.
The evaluation loop
Practical rules
- Set the temperature low for extraction runs, and repeat flaky cases a few times to see how much they vary.
- Do not tune the prompt on the same cases you report results on. Keep a held-out group.
- Save each run's outputs so you can diff them against the next run.
Exercise
You shrink MAX_SIDE from 1568 to 800 to cut token cost. Offline tests pass, and the model still returns valid JSON. What should you run before shipping, and what should you compare?
Show answer
Run the live tests on the test set. Compare per-field accuracy, especially fields from small text, and compare input tokens and latency with the previous run. Valid JSON and passing offline tests do not show that the model can still read the smaller images.