fix(file): convert unsupported image formats to PNG in ReadMediaFile#2382
Open
Pluviobyte wants to merge 1 commit into
Open
fix(file): convert unsupported image formats to PNG in ReadMediaFile#2382Pluviobyte wants to merge 1 commit into
Pluviobyte wants to merge 1 commit into
Conversation
Kimi (and Anthropic/Google) image input only accepts image/png, image/jpeg, image/gif, and image/webp. When the agent calls ReadMediaFile on a .ico file (mime image/x-icon), the resulting data URL was written straight into session history. The next provider request then crashed with `400 unsupported image format: image/x-icon`, and because the offending turn was already persisted, every subsequent resume hit the same error -- the conversation could never be continued. Re-encode any non-supported image format to PNG at the ReadMediaFile boundary so the persisted ImageURLPart is always model-compatible. Falls back to the original bytes when Pillow cannot decode the file, so genuine read failures keep their previous behaviour instead of turning into tool errors. Fixes MoonshotAI#2017 Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issue
Resolve #2017
Description
Kimi (and the other providers in this repo's
kosonglayer — Anthropic, Google) accept onlyimage/png,image/jpeg,image/gif, andimage/webpfor image input. When the agent callsReadMediaFileon a.icofile the resultingdata:image/x-icon;base64,...URL goes straight into session history. The next provider request then crashes with400 unsupported image format: image/x-icon, and because the offending turn is already persisted incontext.jsonl, every subsequent resume of that session hits the same error — the conversation becomes permanently unrecoverable (as described in #2017).Fix
_normalize_image_for_provider()insrc/kimi_cli/tools/file/read_media.pythat decodes any image whose MIME is outside the supported set and re-encodes it as PNG before thedata:URL is constructed.logger.warning) when Pillow can't decode the file, so genuinely corrupt input keeps its previous failure mode instead of turning into a tool error.Why at the
ReadMediaFileboundaryThe corruption is persisted, not transient. Normalizing at the read boundary means the recorded
ImageURLPartincontext.jsonlis already model-compatible, so the very next provider call — and every resume thereafter — sees a well-formed image part.Checklist
CHANGELOG.md(manual Unreleased entry, following the existing one-line-per-bullet style;make gen-changelognot run because the change is mechanical).make gen-docsnot run — N/A: no user-facing config / CLI /ReadMediaFiledescription change.Test plan
uv run ruff check src/kimi_cli/tools/file/read_media.py tests/tools/test_read_media_file.py→ cleanuv run ruff format --check src/kimi_cli/tools/file/read_media.py tests/tools/test_read_media_file.py→ cleanuv run pyright src/kimi_cli/tools/file/read_media.py tests/tools/test_read_media_file.py→0 errors, 0 warnings, 0 informationsuv run pytest tests/tools/test_read_media_file.py -vv→7 passed(1 new:test_read_ico_file_converts_to_png)Proof of fix
Pre-patch behaviour, on current
main:→ the data URL
data:image/x-icon;base64,...is written into history; the model gateway then rejects every following request.Post-patch:
test_read_ico_file_converts_to_pngconstructs a real.icovia Pillow, callsReadMediaFile, and asserts the resultingImageURLPartURL starts withdata:image/png;base64,. ✅Made with Cursor