Skip to content

fix(agent-core): detect image format from bytes when reading media - #970

Merged
7Sageer merged 1 commit into
MoonshotAI:mainfrom
7Sageer:fix/read-media-mime-gate
Jun 22, 2026
Merged

fix(agent-core): detect image format from bytes when reading media#970
7Sageer merged 1 commit into
MoonshotAI:mainfrom
7Sageer:fix/read-media-mime-gate

Conversation

@7Sageer

@7Sageer 7Sageer commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No related issue.

Problem

When ReadMediaFile reads a file whose extension claims an image but whose bytes are not a recognised image (for example a .png that is actually plain text, or a truncated/renamed file), detectFileType fell back to the extension's MIME type and built a data: URL whose bytes did not match the declared format. The model API then rejects the request with 400 Invalid request: unsupported image format: application/octet-stream, failing the turn.

What changed

  • Detect the image MIME from the file bytes (magic-byte sniff) instead of trusting the extension, so a mismatched extension no longer yields a mismatched data URL.
  • Return unknown for an image extension whose bytes fail to sniff, so ReadMediaFile reports a clear "not a supported image or video file" error instead of shipping a bad data URL. Every image format the model accepts (PNG/JPEG/GIF/WebP) has a reliable magic signature, so a failed sniff means the bytes are not a supported image.
  • Keep the extension fallback for video containers with no magic signature (for example MPEG-PS .mpg). Beyond the sniff-failed image case, format acceptance is left to the provider — the tool does not gate on image format.
  • Added regression tests for the mismatched-extension, sniffed-but-unsupported-extension, and un-sniffable cases.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Jun 22, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6b61226

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

When ReadMediaFile reads a file whose bytes have no recognisable image magic (for example a `.png` that is actually plain text), detectFileType previously fell back to the extension MIME type and built a data URL whose bytes did not match the declared format, which the model API rejected.

Every image format the model accepts (PNG/JPEG/GIF/WebP) has a reliable magic signature, so a failed sniff means the bytes are not a supported image. Report such files as `unknown` so ReadMediaFile returns a clear error instead of a bad data URL. The extension fallback is kept for video containers with no magic signature (for example MPEG-PS `.mpg`); format acceptance beyond that is left to the provider.
@7Sageer
7Sageer force-pushed the fix/read-media-mime-gate branch from df0cd56 to 6b61226 Compare June 22, 2026 09:38
@pkg-pr-new

pkg-pr-new Bot commented Jun 22, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@6b61226
npx https://pkg.pr.new/@moonshot-ai/kimi-code@6b61226

commit: 6b61226

@7Sageer
7Sageer merged commit 2730079 into MoonshotAI:main Jun 22, 2026
9 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 22, 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.

1 participant