cd /news/artificial-intelligence/receipt-reader-with-vision · home › topics › artificial-intelligence › article
[ARTICLE · art-180400] src=dev.to ↗ pub= topic=artificial-intelligence verified=true sentiment=↑ positive

Receipt Reader with vision

A developer built a receipt-scanning pipeline that sends a photo to Gemini's vision model and returns validated structured data—store name, purchase date, payment method, subtotal, tax, total and line items—using a Pydantic schema passed as the response schema. The approach relies on field descriptions to steer extraction, a boolean 'receipt' flag to discard unpaid or future-dated receipts, temperature 0, and a separate check that recomputes item totals rather than trusting the model's arithmetic.

recommendation

Use response_schema with descriptive fields and temperature 0 for vision extraction, then recompute totals in code instead of trusting the model's arithmetic.

more in playbook: Verifying model output
by read3 min views14 publishedDec 2, 2026
Day Topic Example Status
1 Talking to an LLM from code Classifying emails by category and urgency ✅ Code
2 Roles, examples and delimiters in prompts Translating technical language into plain language ✅ Code
3 Structured output with JSON, validated with Pydantic Turning a chat order into a table ✅ Code
4 Images with vision models Scanning a receipt and returning a JSON with the products and the total 📍 You're here
5 Embeddings: meaning as numbers Grouping 20 news articles by topic ⏳ Coming soon

The goal: take a photo of a receipt and turn it into structured data: the store, the date, the payment method, the totals and every item purchased.

Vision models can read images, not just text. That means I can send a photo to Gemini and get the data back in the same validated format I used on Day 3.

The schema

I created two classes: Article describes each item the customer bought, and Receipts contains all the information about the purchase, including the list of articles.

class Receipts(BaseModel):
    name: str | None = Field(
        default=None, description="Name of the store where the purchase was made"
    )
    purchase_date: date | None = Field(default=None, description="Date where the purchase was done")
    address: str | None = Field(
        default=None, description="null if not mentioned the address of the store"
    )
    payment_method: str | None = Field(
        default=None, description="null if not mentioned the payment method"
    )
    subtotal: float | None = Field(
        default=None, description="The price of the products with no tax included"
    )
    tax: float | None = Field(
        default=None,
        description="amount in money, not the percentage",
    )
    total: float | None = Field(default=None, description='The grand total of the purchase')
    receipt: bool | None = Field(
        default=None,
        description="The receipt shows that the purchase is already paid. True only if appears any of these, the payment method, card number, amount charged, and False if the date of payment is in the future according to the date the image has",
    )
    articles: list[Article]

Two details make this schema work:

description guides the model.19 instead of $3.80 for the tax.receipt: bool acts as a filter.False, and my code discards it. Reading the image

def describe_receipt(path: str) -> Receipts | None:
    image_bytes = Path(path).read_bytes()
    image_part = types.Part.from_bytes(
        data=image_bytes,
        mime_type=MIME_TYPE[Path(path).suffix.lower()],
    )

    response = client.models.generate_content(
        model=model,
        contents=image_part,
        config=types.GenerateContentConfig(
            system_instruction="""Extract the products orders. Use only information present in the image""",
            temperature=0,
            response_mime_type="application/json",
            response_schema=Receipts,
        ),
    )

    return None if not response.parsed.receipt else response.parsed

The function reads the image as bytes and tells the model what kind of file it is (mime_type, based on the extension: .png, .jpg...). Then it sends the image together with the schema. response.parsed already returns a validated Receipts object, so there's no need to parse the JSON by hand.

Checking the math

def check_totals(receipt: Receipts) -> None:
    """Verify the model's numbers instead of trusting them blindly."""
    items_sum = 0
    for article in receipt.articles:
        items_sum += article.price * article.amount

    if round(items_sum, 2) == round(receipt.subtotal, 2):
        print(f"Check 1 OK: items sum {items_sum:.2f} = subtotal {receipt.subtotal:.2f}")
    else:
        print(f"Check 1 FAILED: items sum {items_sum:.2f} != subtotal {receipt.subtotal:.2f}")

    expected_total = receipt.subtotal + receipt.tax
    if round(expected_total, 2) == round(receipt.total, 2):
        print(f"Check 2 OK: subtotal + tax {expected_total:.2f} = total {receipt.total:.2f}")
    else:
        print(f"Check 2 FAILED: subtotal + tax {expected_total:.2f} != total {receipt.total:.2f}")

A model can misread a number in the image. This function does the math again in plain Python: the items must add up to the subtotal, and the subtotal plus tax must equal the total.

This is the receipt I used:

And this is the output:

mime_type). The rest works just like with text. None. receipt.tax is subtotal + tax crashes. Every field that can be empty needs a plan for when it is. 👉 Full code: day4_receipt_reader.py

── more in #artificial-intelligence 4 stories · sorted by recency
── more on @gemini 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/receipt-reader-with-…] indexed:0 read:3min 2026-12-02 · —