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. | Day | Topic | Example | Status | |---|---|---|---| | 1 | Talking to an LLM from code | Classifying emails by category and urgency | ✅ Code https://github.com/SantiagoAlvarze22/ai-engineering-lab/blob/main/week1/day1 clasificator.py | | 2 | Roles, examples and delimiters in prompts | Translating technical language into plain language | ✅ Code https://github.com/SantiagoAlvarze22/ai-engineering-lab/blob/main/week1/day2 jargon translator.py | | 3 | Structured output with JSON, validated with Pydantic | Turning a chat order into a table | ✅ Code https://github.com/SantiagoAlvarze22/ai-engineering-lab/blob/main/week1/day3 chat orders.py | | 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 php 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, , in case the receipt property is False 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 php 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 Check 1: items vs subtotal round to cents to avoid float errors 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}" Check 2: subtotal + tax vs total 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 https://github.com/SantiagoAlvarze22/ai-engineering-lab/blob/main/week1/day4 receipt reader.py