Skip to content

fix(art): detect actual image format and save with correct extension#639

Closed
VintLin wants to merge 1 commit intodanielmiessler:mainfrom
VintLin:fix/633-image-format-detection
Closed

fix(art): detect actual image format and save with correct extension#639
VintLin wants to merge 1 commit intodanielmiessler:mainfrom
VintLin:fix/633-image-format-detection

Conversation

@VintLin
Copy link
Copy Markdown

@VintLin VintLin commented Feb 11, 2026

Problem

generate may save image bytes with a mismatched extension (e.g., JPEG bytes written to a .png path). This can later cause MIME-type validation failures in downstream readers/tools that trust the extension.

Additionally, reference-image MIME type selection in nano-banana-pro was extension-based only, which can also break if file suffix and bytes diverge.

Changes

  • Added magic-byte image format detection (png, jpeg, webp, gif).
  • Added smart output writer that:
    • detects actual output format
    • rewrites filename extension when needed
    • returns the final output path
  • Updated all generation backends (flux, nano-banana, gpt-image-1, nano-banana-pro) to use smart writer.
  • Updated nano-banana-pro reference-image MIME resolution to:
    • prefer content-based detection
    • fallback to extension when detection is unavailable
  • Tracked the actual generated output path through post-processing (remove-bg, add-bg, thumbnail) so follow-up steps apply to the real file location.

Validation

  • Type/path-level validation by tracing all generation paths and ensuring post-processing uses generatedOutput returned by generator functions.
  • Verified no behavior change when format already matches requested extension.
  • Verified mismatch path now saves with correct extension and emits a warning.

Fixes #633

@kaimagnus
Copy link
Copy Markdown
Collaborator

Thank you for this excellent fix, @VintLin! You correctly identified the core issue — the API sometimes returns a different image format than requested.

We've addressed this in commit 0fc1931 with a similar approach (magic byte detection, automatic extension correction, and proper MIME type handling throughout the pipeline). Closing in favor of that fix. Your analysis of the problem was spot-on and really helped confirm the right approach!

@kaimagnus kaimagnus closed this Feb 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(pai-art-skill): Generate.ts saves JPEG data with .png extension, causing MIME type mismatch

2 participants