When the Photo Is Bad

retries and manual review

Part of: Receipt Scanner

Some receipts are blurry, or crumpled, or shot at an angle with half the total off the edge of the frame. On those, the model sometimes hands back malformed JSON, or JSON that parses cleanly but is missing the fields you need. A production scanner can't fall over on the first bad photo. It needs a plan for "this one didn't work" that doesn't end with a person reading a stack trace. The retry loop The fix is small. If the reply doesn't parse, ask again, up to a small capped number of attempts. A reworded follow-up like "Return ONLY the JSON object, nothing else" often works where the first try failed, because you're nudging the model off whatever formatting habit tripped it up the first time. The cap matters as much as the retry. Without max retries, a receipt that will never parse (a photo of a cat, say) retries forever, burning API calls and money on something that was doomed from the start. Returning None on purpose When every attempt fails, the function returns None rather than raising. That's deliberate. One bad receipt in a batch of fifty shouldn't take down the other forty-nine. None is a checkable signal that means "this one needs a human," and whatever calls scan receipt ca

Challenge: Give Up Gracefully