mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #3058] [Issue]: Chat-completions reference images hardcode image/png MIME, mis-encoding JPEG/WebP avatars for OpenRouter and Gemini #1626
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1626
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 hardcodeddata:image/png;base64,prefix, regardless of the image's actual format. Reference images on the chat-completions path (used by OpenRouter, thegemini_imagebackend, 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 asdata:image/png;base64,<JPEG bytes>. Providers that validate the declared MIME against the bytes — notably Google's Gemini image API, reached viagemini_image— reject the request or silently drop the reference, so character likeness is not preserved. The sibling helpers (imageDataUrlFromReference,decodeReferenceImage) detect the real MIME viadetectImageMimeType(); only this builder hardcodes png.How to reproduce (try it yourself)
google/gemini-2.5-flash-imagemodel, or agemini_image/ NovelAI-proxy connection).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:buildChatImageMessageContentlines 1610-1624 — maps every ref toimage_url: { url: \data:image/png;base64,${b64}` }`, hardcoded png.generateOpenRouter(line 1681) andgenerateViaChatCompletions(line 1785), dispatched from thegenerateImageswitch cases"openrouter"(175) and"gemini_image"(203).routes/generate/illustrator-references.ts:131-133,226-237(readAvatarBase64inservices/game/game-asset-generation.ts:206-231,readPreferredFullBodySpriteBase64inservices/game/sprite.service.ts:137-171) and passed throughroutes/generate/retry-agents-route.ts:2755,2784.imageDataUrlFromReferencelines 444-449.What's happening
The data-URL MIME is hardcoded to
image/pnginstead of detected from the bytes. The reference images are raw base64 of arbitrary on-disk formats (JPEG/WebP/PNG read viareadFileSync(...).toString("base64")with no prefix). The other provider reference helpers calldetectImageMimeType(); this builder does not.Suggested fix
In
buildChatImageMessageContent(image-generation.ts:1616-1619), replace the hardcoded png data URL with the existing detection helper:imageDataUrlFromReference(lines 444-449) trims, returns existingdata:URLs as-is, and otherwise buildsdata:${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) admitsdata:URLs, and the current code would emitdata:image/png;base64,data:image/jpeg;base64,...for those —imageDataUrlFromReferencepasses them through unchanged.Regression avoided: the change is confined to this pure request-body builder (only callers are
generateOpenRouterandgenerateViaChatCompletions); 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). TherefImages.length > 0guard and the no-reference path are preserved, andimageDataUrlFromReferenceis 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.
@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
@SpicyMarinara commented on GitHub (Jun 29, 2026):
Fixed in PR #3064 on branch
codex/issue-sweep-3054-3063(commit3ae8ca37). Closing this issue as addressed; the PR remains open for staging review/merge.