When the Upload or the Reply Breaks
error handling and retries
Part of: Caption & Alt-Text Generator
Lessons 1 through 5 assumed a perfect world: valid images and well-formed JSON. Production images are messy. Wrong formats, huge files, and models that occasionally reply with prose instead of JSON. This lesson adds the guardrails. Guard the upload before you spend a token Two checks that cost nothing and save a wasted, billed API call: Run this before you build the content block. An unsupported format or an oversized file fails on the API side too, but failing locally is instant and free. Failing remotely costs a network round trip and, if you're unlucky, a partial charge. Guard the reply after you get it back The JSON parser from the last lesson can still fail, because a model occasionally ignores the format instruction. Wrap it in a retry, the same pattern from the setup module: Fallback, not just failure If every retry fails, don't crash the tool. Hand back something safe. A generic alt text beats no alt text, and a missing caption beats a broken app. Reserve the fallback for a real exhaustion of retries. Reach for it too early and it hides real bugs. Why order matters: cheap checks first Validate the upload before you spend a call. Validate the reply after you receive one. Do
Challenge: Guard the Upload