The boring half of an AI image pipeline
4 min read
QuickStaging takes a photo of an empty room and returns the same room, furnished. The call to the image model is one line. This post is about the other lines, which deal with what happens before the model sees a photo, while it works, and when it gets something wrong.
Before the model
Real estate photos come from phones, cameras and other tools, and every one of them sends in odd files: HEIC from iPhones, AVIF from editing software, PDFs of floor plans, PNGs with a transparent background. Then there are photos that look upright but are stored sideways, with an EXIF flag that says "rotate me".
The browser converts what it can before uploading. On the server, every image gets rotated upright, re-encoded and size-checked. None of that is interesting until it's missing. An AVIF upload once passed every check, failed silently further down and left a listing with zero photos, so now unusual formats are converted on the client before anything else happens.
My favourite bug from this layer came from an image library. One quality check runs edge detection on the result, and on images with an alpha channel the edge map came back all zeros, because the channel was only removed at the end of the pipeline, after the convolution. So the check passed every image that had transparency. Fixing it took one call in the right place, and I confirmed it changed nothing on 238 images without transparency.
While the model works
Generation takes anywhere from seconds to several minutes, far longer than an HTTP request should stay open, so every job is asynchronous. The API claims credits, starts the job and returns at once. When the provider is done, it calls a webhook. The page polls the database, not the provider.
Async jobs fail in more ways than sync calls, so errors are sorted by what to do about them. A permanent error, like bad input or a rejected request, stops the job and refunds it. Throttled and transient errors are retried within the request, with backoff and jitter, but never past the route's time limit. Out of capacity means the provider is busy. That job goes into a queue that retries over almost two hours, and the credit stays held until it lands or is refunded.
When there's a second provider, the fallback signs its webhook exactly the way the first one does. The completion code can't tell which provider did the work, and it doesn't need to.
When the webhook never comes
Webhooks get lost. A deploy restarts the server, or a request times out, or the network drops one. Early on, a lost webhook meant someone had paid for a render nobody ever saw.
Two things fix that. A scheduled job looks for anything stuck in "processing" for 15 minutes, and before refunding anything it asks the provider what actually happened, then settles the job through the same code the webhook would have run. Only jobs the provider has no record of get written off. The job also claims each record with a guarded update before it touches credits, so two runs can't refund the same image twice.
The clock starts when processing starts, not when the record was created. When it went by the creation date, it once reaped regenerations of old images that were working fine.
Checking the output
Image models fail confidently. The result looks like a photo, but a window has moved or a doorway is gone. So every result goes through two checks, cheap pixel measurements first and then a vision model comparing before and after.
A pixel check on its own can only mark a result "suspect". Holding a result back takes the vision model's confirmation. A rejected result gets regenerated for free a limited number of times, and if it still fails, the user gets a refund.
The checker fails open, because a broken checker shouldn't block every user. Every verdict also records why it passed, which I added after a usage-limit error was silently logged as a pass for a stretch of time.
The credits have their own rules around all this, covered in a separate post.
What I deleted
The pipeline got better mostly by getting smaller. A large prompt builder lost a blind comparison to something much shorter. I had a "structure lock" step that was supposed to protect the room, and on the same set of rooms it did worse than a plain retry. There's also a photo "look" pass that still sits in the code, though nothing calls it.
Each of those felt like progress when I wrote it, and measuring is what got rid of them. These days a new step has to beat a test set before it ships.