[GH-ISSUE #3058] [Issue]: Chat-completions reference images hardcode image/png MIME, mis-encoding JPEG/WebP avatars for OpenRouter and Gemini #1626

Closed
opened 2026-10-21 05:41:02 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3058

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

buildChatImageMessageContent() wraps every reference image in a data URL with a hardcoded data:image/png;base64, prefix, regardless of the image's actual format. Reference images on the chat-completions path (used by OpenRouter, the gemini_image backend, and OpenAI-compatible proxies) are raw base64 of files read straight off disk — avatars and sprites stored in their true format (.jpg/.jpeg/.webp/.png). So a character whose avatar is a JPEG or WebP is sent as data:image/png;base64,<JPEG bytes>. Providers that validate the declared MIME against the bytes — notably Google's Gemini image API, reached via gemini_image — reject the request or silently drop the reference, so character likeness is not preserved. The sibling helpers (imageDataUrlFromReference, decodeReferenceImage) detect the real MIME via detectImageMimeType(); only this builder hardcodes png.

How to reproduce (try it yourself)

  1. Configure an image_generation connection that resolves to the chat-completions path (e.g. OpenRouter with a google/gemini-2.5-flash-image model, or a gemini_image / NovelAI-proxy connection).
  2. Use a character or persona whose avatar was uploaded as a JPEG or WebP (stored on disk as .jpg/.webp).
  3. Trigger an illustrator/selfie generation with avatar references enabled.

You'll see: the provider receives JPEG/WebP bytes labeled data:image/png;base64,... and either rejects the request (MIME/bytes mismatch) or ignores the reference, so the character likeness is lost.

Expected: each reference is labeled with its real MIME, so JPEG/WebP/GIF/PNG avatars are accepted and used for likeness.

Where it is

packages/server/src/services/image/image-generation.ts:

  • buildChatImageMessageContent lines 1610-1624 — maps every ref to image_url: { url: \data:image/png;base64,${b64}` }`, hardcoded png.
  • Reached via generateOpenRouter (line 1681) and generateViaChatCompletions (line 1785), dispatched from the generateImage switch cases "openrouter" (175) and "gemini_image" (203).
  • Raw-base64 refs sourced from routes/generate/illustrator-references.ts:131-133,226-237 (readAvatarBase64 in services/game/game-asset-generation.ts:206-231, readPreferredFullBodySpriteBase64 in services/game/sprite.service.ts:137-171) and passed through routes/generate/retry-agents-route.ts:2755,2784.
  • Existing helper to reuse: imageDataUrlFromReference lines 444-449.

What's happening

The data-URL MIME is hardcoded to image/png instead of detected from the bytes. The reference images are raw base64 of arbitrary on-disk formats (JPEG/WebP/PNG read via readFileSync(...).toString("base64") with no prefix). The other provider reference helpers call detectImageMimeType(); this builder does not.

Suggested fix

In buildChatImageMessageContent (image-generation.ts:1616-1619), replace the hardcoded png data URL with the existing detection helper:

const parts: Array<Record<string, unknown>> = refImages.map((b64) => ({
  type: "image_url",
  image_url: { url: imageDataUrlFromReference(b64) },
}));

imageDataUrlFromReference (lines 444-449) trims, returns existing data: URLs as-is, and otherwise builds data:${detectImageMimeType(base64) ?? "image/png"};base64,${base64}. This labels JPEG/WebP/GIF/PNG avatars correctly for OpenRouter and the gemini_image / OpenAI-compatible path, keeps png as the fallback when detection fails, and also fixes a latent double-prefix bug: the avatar-generation route (characters.routes.ts:384-387) admits data: URLs, and the current code would emit data:image/png;base64,data:image/jpeg;base64,... for those — imageDataUrlFromReference passes them through unchanged.

Regression avoided: the change is confined to this pure request-body builder (only callers are generateOpenRouter and generateViaChatCompletions); it does not touch response parsing, abort handling, aspect-ratio logic, or any local/self-hosted backend (ComfyUI/A1111/RunPod/NovelAI use separate reference paths). The refImages.length > 0 guard and the no-reference path are preserved, and imageDataUrlFromReference is already the production helper for these same values on the NanoGPT path.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3058 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** `buildChatImageMessageContent()` wraps every reference image in a data URL with a hardcoded `data:image/png;base64,` prefix, regardless of the image's actual format. Reference images on the chat-completions path (used by OpenRouter, the `gemini_image` backend, and OpenAI-compatible proxies) are raw base64 of files read straight off disk — avatars and sprites stored in their true format (.jpg/.jpeg/.webp/.png). So a character whose avatar is a JPEG or WebP is sent as `data:image/png;base64,<JPEG bytes>`. Providers that validate the declared MIME against the bytes — notably Google's Gemini image API, reached via `gemini_image` — reject the request or silently drop the reference, so character likeness is not preserved. The sibling helpers (`imageDataUrlFromReference`, `decodeReferenceImage`) detect the real MIME via `detectImageMimeType()`; only this builder hardcodes png. **How to reproduce (try it yourself)** 1. Configure an image_generation connection that resolves to the chat-completions path (e.g. OpenRouter with a `google/gemini-2.5-flash-image` model, or a `gemini_image` / NovelAI-proxy connection). 2. Use a character or persona whose avatar was uploaded as a JPEG or WebP (stored on disk as .jpg/.webp). 3. Trigger an illustrator/selfie generation with avatar references enabled. You'll see: the provider receives JPEG/WebP bytes labeled `data:image/png;base64,...` and either rejects the request (MIME/bytes mismatch) or ignores the reference, so the character likeness is lost. Expected: each reference is labeled with its real MIME, so JPEG/WebP/GIF/PNG avatars are accepted and used for likeness. **Where it is** `packages/server/src/services/image/image-generation.ts`: - `buildChatImageMessageContent` lines 1610-1624 — maps every ref to `image_url: { url: \`data:image/png;base64,${b64}\` }`, hardcoded png. - Reached via `generateOpenRouter` (line 1681) and `generateViaChatCompletions` (line 1785), dispatched from the `generateImage` switch cases `"openrouter"` (175) and `"gemini_image"` (203). - Raw-base64 refs sourced from `routes/generate/illustrator-references.ts:131-133,226-237` (`readAvatarBase64` in `services/game/game-asset-generation.ts:206-231`, `readPreferredFullBodySpriteBase64` in `services/game/sprite.service.ts:137-171`) and passed through `routes/generate/retry-agents-route.ts:2755,2784`. - Existing helper to reuse: `imageDataUrlFromReference` lines 444-449. **What's happening** The data-URL MIME is hardcoded to `image/png` instead of detected from the bytes. The reference images are raw base64 of arbitrary on-disk formats (JPEG/WebP/PNG read via `readFileSync(...).toString("base64")` with no prefix). The other provider reference helpers call `detectImageMimeType()`; this builder does not. **Suggested fix** In `buildChatImageMessageContent` (image-generation.ts:1616-1619), replace the hardcoded png data URL with the existing detection helper: ```ts const parts: Array<Record<string, unknown>> = refImages.map((b64) => ({ type: "image_url", image_url: { url: imageDataUrlFromReference(b64) }, })); ``` `imageDataUrlFromReference` (lines 444-449) trims, returns existing `data:` URLs as-is, and otherwise builds `data:${detectImageMimeType(base64) ?? "image/png"};base64,${base64}`. This labels JPEG/WebP/GIF/PNG avatars correctly for OpenRouter and the gemini_image / OpenAI-compatible path, keeps png as the fallback when detection fails, and also fixes a latent double-prefix bug: the avatar-generation route (characters.routes.ts:384-387) admits `data:` URLs, and the current code would emit `data:image/png;base64,data:image/jpeg;base64,...` for those — `imageDataUrlFromReference` passes them through unchanged. Regression avoided: the change is confined to this pure request-body builder (only callers are `generateOpenRouter` and `generateViaChatCompletions`); it does not touch response parsing, abort handling, aspect-ratio logic, or any local/self-hosted backend (ComfyUI/A1111/RunPod/NovelAI use separate reference paths). The `refImages.length > 0` guard and the no-reference path are preserved, and `imageDataUrlFromReference` is already the production helper for these same values on the NanoGPT path. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Claiming this for the current staging sweep in draft PR #3064: https://github.com/Pasta-Devs/Marinara-Engine/pull/3064

<!-- gh-comment-id:4835429184 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Claiming this for the current staging sweep in draft PR #3064: https://github.com/Pasta-Devs/Marinara-Engine/pull/3064
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in PR #3064 on branch codex/issue-sweep-3054-3063 (commit 3ae8ca37). Closing this issue as addressed; the PR remains open for staging review/merge.

<!-- gh-comment-id:4835578693 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in PR #3064 on branch `codex/issue-sweep-3054-3063` (commit 3ae8ca37). Closing this issue as addressed; the PR remains open for staging review/merge.
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#1626
No description provided.