[GH-ISSUE #1026] [Bug]: OpenRouter image generation throws unhelpful errors when response is not the expected chat envelope #499

Closed
opened 2026-10-20 17:29:31 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @etherious1804 on GitHub (May 20, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/1026

Summary

generateOpenRouter in packages/server/src/services/image/image-generation.ts assumes every successful response from OpenRouter's image-generation endpoint is a chat-completions JSON envelope with an extractable image URL. In practice there are at least two shapes that break the path today:

Failure modes

  1. Raw image bytes. Some OpenRouter image models (raw provider passthrough) return PNG / JPEG / GIF / WEBP bytes directly with an image/* content-type instead of a JSON envelope. The current await resp.json() call then throws SyntaxError: Unexpected token ... and the user sees a JSON parse crash instead of the image. There is no way to recover this in user space — the response is being thrown out before any image data is read.

  2. JSON envelope with no image (model refusal, text-only response, etc.). The current "no image" error message is just No image data in OpenRouter response: <first 200 chars> with no finish_reason, no shape hint, and no log line. There is no way to tell from this error whether the issue is a wrong API key, a model that doesn't support image output, a prompt the model refused, or a misconfigured modalities setting on the model itself.

  3. Gzipped responses. OpenRouter sometimes returns Content-Encoding: gzip from upstream providers. The shared imageFetch helper does not set decodeCompressedResponse: true, so the buffer fails the same JSON-parse path.

Proposed fix

  • Read the response into a Buffer, sniff the content-type + magic bytes, and short-circuit to a base64 image return when the bytes look like an image.
  • Otherwise parse as JSON; if JSON parse fails, surface the content-type and first 80 bytes (hex) in the error message so the user can self-diagnose.
  • If the JSON has a top-level error field, surface it directly.
  • If JSON parses but no image URL is extractable, log the full message shape via logger.warn (model, finish_reason, first 800 chars of the message body) and include finish_reason + message-object keys in the thrown error.
  • Enable decodeCompressedResponse: true on the shared imageFetch helper so gzipped responses are decoded transparently.

Repro

  1. Configure an OpenRouter image-generation connection.
  2. Use any model that doesn't return a chat-completions JSON envelope (e.g., a raw-passthrough image model).
  3. Run a generation. Observe the crash / unhelpful error.

PR coming.

Originally created by @etherious1804 on GitHub (May 20, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/1026 ## Summary `generateOpenRouter` in `packages/server/src/services/image/image-generation.ts` assumes every successful response from OpenRouter's image-generation endpoint is a chat-completions JSON envelope with an extractable image URL. In practice there are at least two shapes that break the path today: ## Failure modes 1. **Raw image bytes.** Some OpenRouter image models (raw provider passthrough) return PNG / JPEG / GIF / WEBP bytes directly with an `image/*` content-type instead of a JSON envelope. The current `await resp.json()` call then throws `SyntaxError: Unexpected token ...` and the user sees a JSON parse crash instead of the image. There is no way to recover this in user space — the response is being thrown out before any image data is read. 2. **JSON envelope with no image (model refusal, text-only response, etc.).** The current "no image" error message is just `No image data in OpenRouter response: <first 200 chars>` with no `finish_reason`, no shape hint, and no log line. There is no way to tell from this error whether the issue is a wrong API key, a model that doesn't support image output, a prompt the model refused, or a misconfigured `modalities` setting on the model itself. 3. **Gzipped responses.** OpenRouter sometimes returns `Content-Encoding: gzip` from upstream providers. The shared `imageFetch` helper does not set `decodeCompressedResponse: true`, so the buffer fails the same JSON-parse path. ## Proposed fix - Read the response into a `Buffer`, sniff the content-type + magic bytes, and short-circuit to a base64 image return when the bytes look like an image. - Otherwise parse as JSON; if JSON parse fails, surface the content-type and first 80 bytes (hex) in the error message so the user can self-diagnose. - If the JSON has a top-level `error` field, surface it directly. - If JSON parses but no image URL is extractable, log the full message shape via `logger.warn` (model, `finish_reason`, first 800 chars of the message body) and include `finish_reason` + message-object keys in the thrown error. - Enable `decodeCompressedResponse: true` on the shared `imageFetch` helper so gzipped responses are decoded transparently. ## Repro 1. Configure an OpenRouter image-generation connection. 2. Use any model that doesn't return a chat-completions JSON envelope (e.g., a raw-passthrough image model). 3. Run a generation. Observe the crash / unhelpful error. PR coming.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#499
No description provided.