MrPicasso
When an AI image stream fails, the error decides the retry
Nine gpt-image-2 renders in MrPicasso failed on one palette PNG. The lasting fix: classify each model failure before retrying, as some retries only cost money.
In short
Classify a model failure before you retry it. A stream that dies halfway is retried once without streaming; an outage or timeout moves to another model; a refusal, a bad key, an empty quota or a rate limit is never retried on the same model. A job that lost its stream never reopens one, which cuts the worst case from four provider calls to three.
MrPicasso is an AI advertising studio where I lead engineering. You describe the ad you want, maybe attach a reference image, and gpt-image-2 or one of Google's Gemini models renders it. With OpenAI the image streams in as it's drawn, so you watch it take shape instead of staring at a spinner.
In August, nine renders failed, and when I opened them the pattern was hard to miss. All of them were wide 3:1 ads on gpt-image-2, all died halfway through the stream with image_generation_server_error, and all used the same reference image: a PNG saved with an indexed colour palette, a compact format some design tools export.
So I tried that image without streaming, and it rendered. I re-saved it as a normal full-colour PNG and streamed it again, and that rendered too. The request had never been the problem; the stream was choking on that one file.
Fixing the file took one conversion. The harder question was what a render should do the next time a model call fails, because every retry costs money and some failures can't be cured by trying again.
Let the error decide the retry
The first fix was to convert palette PNGs to full colour before upload. That handles this file, but the next provider quirk will look different, so the render needed a rule for any failure, starting with working out what kind of failure it is.
A stream that dies halfway is a delivery problem rather than a problem with the request, so the render retries once without streaming. Network errors, timeouts and provider outages may pass on the next attempt, so the render tries another model, or the same one if no other model can do the job.
A refusal is different. The model's safety system said no, and asking again gets the same answer, only billed twice. A bad API key, an empty quota or a rate limit is similar, because the problem is the account rather than the request, so none of these is ever retried on the same model.
Another model can still try after a refusal, and that's on purpose: a refusal reflects one model's rules, not ours, and another model may accept the same prompt. The error tells the user exactly that and suggests switching models.
Streaming adds one more case, a stream that showed a partial image before it died. That partial was real output and the provider charges for it, so the render records its cost before retrying and then takes it off the screen, and the user never stares at a preview of an image that won't arrive.
Two retries that only cost money
Each retry made sense on its own, but looking at them together I found two ways they still wasted money.
When the only model that could do the job failed, the same-model retry ran the whole render again, starting with a fresh stream even though this job's stream had already died, so one render could make four provider calls: stream, plain, stream again, plain. Now a job remembers that its stream failed and every later attempt goes straight to non-streaming, which brings the worst case down to three calls, none of them reopening a stream that already broke.
A creative session can also be pinned to one model, and a pinned render used to retry that model after any error, refusals and rejected keys included, even though those retries could only fail and cost money. That decision now lives in one small function, recoveryFor, and a pinned session stops at a failure a retry can't fix, since it isn't allowed to switch models.
How I tested it
Unit tests throw fake provider errors at the render and check that:
- a dead stream retries once without streaming;
- a refusal, a bad key, an empty quota and a rate limit are never retried on the same model;
- a job never reopens a stream that already failed, so it makes three calls instead of four;
- a pinned session retries an outage but stops on a refusal;
- an unpinned render can still move to another model after a refusal.
The rest of the image-generation tests still pass, and the type check is clean.
Where it stands
The wide ad from that palette PNG renders now. More usefully, every failure gets the response that fits it: a broken stream is retried without streaming, an outage moves to another model, and a refusal or an account problem stops before it costs anything more. A partial preview is always paid for, and it's never left on screen for an image that isn't coming.
Further reading
- Timeouts, retries and backoff with jitter, Amazon Builders' Libraryaws.amazon.com
- OpenAI image generation guideplatform.openai.com
- Indexed colour, the palette format behind this bugen.wikipedia.org
More writing
-
Don't let the model convert the calendar: Jalali dates in an MCP server for a gold inventory
Why a gold inventory's MCP server converts Jalali dates itself, rejects days that don't exist, and returns every date in both calendars to AI agents.

-
A cold start that draws last launch: persisting the TanStack Query cache in a mobile word game
How ClueShot restores last launch's TanStack Query cache as stale, checks whose it is, and revalidates with ETags so a cold start never opens empty.

-
Two failure handlers, one refund: idempotent cleanup in an async AI image pipeline
How a BullMQ image worker, its failed event and a Telegram bot all reach one refund in MrPicasso, and how two Redis claims keep it to one credit.
