Skip to content

fix: detect LocalImage MIME type across native gateways#430

Merged
pushpak1300 merged 7 commits into
laravel:0.xfrom
maherelgamil:fix/openai-localimage-null-mime
Apr 23, 2026
Merged

fix: detect LocalImage MIME type across native gateways#430
pushpak1300 merged 7 commits into
laravel:0.xfrom
maherelgamil:fix/openai-localimage-null-mime

Conversation

@maherelgamil
Copy link
Copy Markdown
Contributor

@maherelgamil maherelgamil commented Apr 20, 2026

Three native gateways (OpenAI, xAI, Gemini) read $attachment->mime directly when mapping a LocalImage. That property is nullable —
LocalImage::__construct accepts ?string $mimeType = null — so if you don't pass a MIME explicitly, the outgoing request is malformed:

  • OpenAI / xAI send data:;base64,<payload> (rejected as invalid input_image)
  • Gemini sends inlineData.mimeType: null

The other gateways (OpenRouter, Groq, Mistral, DeepSeek, Anthropic) already go through $attachment->mimeType(), which falls back to
Filesystem::mimeType($path) for auto-detection. This PR switches the three outliers to the same call, with an ?? 'image/png' guard for the edge
case where detection also returns null.

While here, I pulled that mimeType() ?? 'image/png' fallback up to Image::dataUriMimeType() since it was duplicated across every image arm in all
seven gateways.

Added one regression test per affected gateway — each constructs a LocalImage without a MIME and asserts the outgoing request carries image/png.
All three fail on main and pass with the fix.

Comment thread tests/Feature/Providers/Jina/EmbeddingTest.php
@maherelgamil maherelgamil marked this pull request as draft April 20, 2026 13:48
@maherelgamil maherelgamil force-pushed the fix/openai-localimage-null-mime branch from eff0168 to 017af53 Compare April 20, 2026 13:51
Mirror the OpenAI fix across the other native gateways that still read
$attachment->mime directly on LocalImage. Extract fakePngFile() into
tests/Helpers.php so the three provider tests share one PNG fixture.
@maherelgamil maherelgamil changed the title fix(openai): use detected mime type for LocalImage attachments fix(openai, xai, gemini): use detected mime type for LocalImage attachments Apr 20, 2026
@maherelgamil maherelgamil changed the title fix(openai, xai, gemini): use detected mime type for LocalImage attachments fix: detect LocalImage MIME type across native gateways Apr 20, 2026
Adds Image::dataUriMimeType() with a single DEFAULT_MIME_TYPE constant,
replacing 12 duplicated "$attachment->mimeType() ?? 'image/png'"
expressions across 7 gateways. Also covers the StoredImage arms in
OpenAI and Xai that previously hardcoded data:image/png;base64.
@maherelgamil
Copy link
Copy Markdown
Contributor Author

maherelgamil commented Apr 20, 2026

@pushpak1300 while I was in here I noticed 'data:'.$mime.';base64,'.$base64 is repeated across most of the gateway attachment traits. If you're open
to it, I'd be happy to do a small dedup pass (probably an asDataUri() on the file classes) in a follow-up PR after this lands. Let me know.

Or the slightly longer version if you want to mention the MIME fallback too:

While reviewing this I noticed the data:<mime>;base64,<payload> construction is duplicated across most gateway traits, and the mimeType() ?? 'image/png' fallback is repeated too (this PR collapses the image one). Happy to open a follow-up that pushes both down to the file classes if that
direction makes sense to you.

@maherelgamil maherelgamil marked this pull request as ready for review April 20, 2026 14:17
@pushpak1300 pushpak1300 merged commit 84575cd into laravel:0.x Apr 23, 2026
5 checks passed
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.

2 participants